integrate partial area under curve in R -
i've fit gaussian curve below data, , calculate area under curve between values of x (e.g., x=6 x=18). below code produces following error: "error in f(x, c(m, s, a, b)) : unused argument (c(m, s, a, b))". also, have few negative values , need subtract negative area positive because i'm interested in 'net' value.
x <- c(12.88, 12.9, 8.35, 10.52, 10.45, 8.44, 9.01, 9.96, 9.82, 9.83, 10.65, 10.69, 15.3, 15.33, 12.41, 12.43, 8.36, 8.43, 9.29, 9.25, 14.78, 14.87, 16.17, 16.23, 3.59, 4.37, 3.88, 19.88, 20.71, 20.33, 21.25, 22.09) y<-c(10.8, 9.62, 11.76, 5.12, 9.63, 4.80, 11.09, 7.42, 7.79, 9.76, 9.71, 8.13, 14.4, 14.85, 12.84, 11.59, 7.0, 6.49, 5.94, 4.93, 6.43, 7.8, 3.81, 2.6, -0.93, 5.3, 1.08, 0.39, -0.59, 2.77, 3.5, -2.08) df<-data.frame(x, y) #define gaussian function (y=amplitude*exp(-0.5*((x-mean)/sd)^2) + baseline) f<-function(x, theta) { m<-theta[1]; s<-theta[2]; a<-theta[3]; b<-theta[4]; a*exp(-0.5*((x-m)/s)^2) + b } fit<-nls(y~f(x,c(m,s,a,b)), data.frame(x,y), start=list(m=12, s=5, a=12, b=-2)) xs<-seq(0,24,l=1000) f<-function(x) predict(fit,newdata=data.frame(x=xs)) integrate(f,6,18)
assuming data in variables timeofday
, y
, should work (i assuming loess curve work needs. may (will) need change model fitting based on data. depending on model, integrand change , result!!)
df <- data.frame(timeofday,y) df <- df[order(df$timeofday),] l <- loess(y ~ timeofday, df, control=loess.control(surface="direct")) f <- function(x) predict(l,newdata = x) integrate(f,2,3) #you can specify between times want integrate here plot(df) lines(l)
Comments
Post a Comment