I am writing a code to model the inactivation of bacteria. I was able to get the model using nls(). Now, when I am trying to calculate and plot the confidence and prediction intervals, I am unable to get a proper fit. The lower CI and PI are being plotted awry and I do not know how to rectify this issue.
My code:
Xo <- c(0, 0.461, 0.978, 2.087)
Y <- c(0, -2.891, -4.197, -4.489)
dat <- data.frame(Xo, Y, colClass="numeric")
f <- function(Xo, n, k) {-k*(Xo)^n}
m <- nls(Y~f(Xo, n, k), data=dat, start=c(n=2, k=20), trace=TRUE)
summary(m)
yfitted <- predict(m)
plot(Xo, yfitted)
lines(Xo, yfitted)
plot(dat$Xo, yfitted, xlim=c(0, 5), ylim=c(-5, 0))
y.conf <- predictNLS(m, interval="confidence", alpha =0.05, nsim=10000)$summary
y.pred <- predictNLS(m, interval="prediction", alpha=0.05, nsim=10000)$summary
matlines(Xo, y.conf[, c("Sim.2.5%", "Sim.97.5%")], col="red", lty="dashed")
matlines(Xo, y.pred[, c("Sim.2.5%", "Sim.97.5%")], col="blue", lty="solid")