我在预测r中的nls的置信区间时遇到麻烦。
pl <- ggplot(data) + geom_point(aes(x=date, y=cases),size=2, colour="black") + xlab("Date") + ylab("Cases")
model = nls(cases ~ SSlogis(log(date), Asym, xmid, scal), data= data )
new.data = data.frame(date=c(1:100))
interval <- predict(model, newdata = new.data, se.fit = TRUE, interval = "confidence", level= 0.9)
new.data[c("fit","lwr.conf", "upr.conf")] <- interval
pl +
geom_ribbon(data=new.data, aes(x=date, ymin=lwr.pred, ymax=upr.pred), alpha=0.05, inherit.aes=F, fill="blue")
[当我运行它时,它没有显示任何错误,但是我得到的间隔只是一个带有拟合的向量,没有上下置信区间。
非线性置信区间可以通过使用包propagate进行仿真来获得:
library("propagate")
x <- c(25, 25, 10, 10, 5, 5, 2.5, 2.5, 1.25, 1.25)
y <- c(0.0998, 0.0948, 0.076, 0.0724, 0.0557,
0.0575, 0.0399, 0.0381, 0.017, 0.0253)
m <- nls(y ~ SSmicmen(x, Vm, K), trace = TRUE)
x1 <- seq(0, 25, length = 100)
plot(x, y, xlim = c(0, 25), ylim = c(0, 0.1))
lines(x1, predict(m, data.frame(S = x1)), col = "red")
y.conf <- predictNLS(m, newdata=data.frame(x=x1), interval="confidence", alpha=0.05, nsim=10000)$summary
y.pred <- predictNLS(m, newdata=data.frame(x=x1), interval="prediction", alpha=0.05, nsim=10000)$summary
matlines(x1, y.conf[,c("Sim.2.5%", "Sim.97.5%")], col="red", lty="dashed")
matlines(x1, y.pred[,c("Sim.2.5%", "Sim.97.5%")], col="blue", lty="solid")