![](/img/trans.png)
[英]R : Robust nonlinear least squares fitting of three-phase linear model with confidence & prediction intervals
[英]How to calculate confidence intervals for Nonlinear Least Squares in r?
我在預測 r 中的置信區間 ros 和 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")
當我運行它時,它沒有顯示錯誤,但我得到的區間只是一個適合的向量,沒有上下置信區間。
我知道如何執行此操作的方法有 3 種,其中一種方法在另一個答案中進行了描述。 這里有一些其他選項。 第一個使用 nls() 擬合 model 和investr::predFit 進行預測和CI:
library(tidyverse)
library(investr)
data <- tibble(date = 1:7,
cases = c(0, 0, 1, 4, 7, 8.5, 8.5))
model <- nls(cases ~ SSlogis(log(date), Asym, xmid, scal), data= data )
new.data <- data.frame(date=seq(1, 10, by = 0.1))
interval <- as_tibble(predFit(model, newdata = new.data, interval = "confidence", level= 0.9)) %>%
mutate(date = new.data$date)
p1 <- ggplot(data) + geom_point(aes(x=date, y=cases),size=2, colour="black") + xlab("Date") + ylab("Cases")
p1+
geom_line(data=interval, aes(x = date, y = fit ))+
geom_ribbon(data=interval, aes(x=date, ymin=lwr, ymax=upr), alpha=0.5, inherit.aes=F, fill="blue")+
theme_classic()
另一種選擇是使用“drc”包(又名劑量反應曲線)進行 model 擬合和預測。 這個 package 使用需要使用(或創建)的內置啟動函數,但是 class 的 object 的 object 'drc' 僅用於預測有許多有用的方法可以利用一些有用的方法來支持置信區間。內置自啟動器)。 以 package 'drc' 為例:
library(drc)
model_drc <- drm(cases~date, data = data, fct=LL.4())
predict_drc <- as_tibble(predict(model_drc, newdata = new.data, interval = "confidence", level = 0.9)) %>%
mutate(date = new.data$date)
p1+
geom_line(data=predict_drc, aes(x = date, y = Prediction ))+
geom_ribbon(data=predict_drc, aes(x=date, ymin=Lower, ymax=Upper), alpha=0.5, inherit.aes=F, fill="red")+
ggtitle("with package 'drc'")+
theme_classic()
有關“drc”package 的更多信息: 期刊論文、 描述 drc 的自定義自啟動的博客文章和 package文檔
非線性置信區間可以通過使用package模擬來獲得:
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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.