r - Confidence Interval for mle2 Regression Line -
i doing non-linear regression mle2 in r , want generate 95% point-wise confidence band around curve of best fit. simplified example of trying shown below, try use predict on mle2 fit object. suggestions on how this?
library(bbmle) # fabricated data e.u <- function( x, k ) { exp(-k * x) } n <- 40 t.bio <- 1:n bio <- 10*e.u(t.bio,log(2)/10) + rnorm(n,0,sqrt(e.u(t.bio,log(2)/10))) #use mle2 estimate parameters intake.guess <- 10 rc.guess <- 0.07 n.log.like <- function(intake,k) { sum.y <- 0 (i in 1:length(bio)) { x <- intake * e.u(t.bio[i],k) y <- bio[i] sum.y <- sum.y + log( dnorm(y,x,0.1*sqrt(y)) ) } return(-sum.y) } b <- mle2(n.log.like, start=list( intake=intake.guess, k=rc.guess), data=list( t.bio=t.bio, bio=bio), method="nelder-mead", skip.hessian=false) intake <- coef(summary(b))[1,1] rc <- coef(summary(b))[2,1] summary(b) #scatter plot bio.p <- numeric(n) x <- 1:n (i in 1:n) { bio.p[i] <- intake * e.u(x[i],rc) } plot(x,bio.p,type="l",log="xy",main="", xlab="days after intake",ylab="excretion") points(t.bio,bio) #i want generate confidence interval on regression line bio.hat <- predict(b)
you may want try resource:
https://stat.ethz.ch/r-manual/r-patched/library/stats/html/predict.lm.html
there se.fit parameter passed predict(). if true, calculate se. have not tried it.
another way use library(ggplot2), 1 of parameters can confidence level overlayed on plot automatically. example,
c <- ggplot(mtcars, aes(qsec, wt)) c + stat_smooth(se = true) + geom_point() this put band around plot indicate confidence interval. se set true default.
reference: http://svitsrv25.epfl.ch/r-doc/library/ggplot2/html/stat_smooth.html
third, may calculate confidence interval , overlay plot setting par(new=true). may cumbersome still feasible.
Comments
Post a Comment