从这里借用示例数据 question ,如果我有以下数据并且我拟合了以下非线性模型,我如何计算 95% 预测 间隔我的曲线?
library(broom)
library(tidyverse)
x <- seq(0, 4, 0.1)
y1 <- (x * 2 / (0.2 + x))
y <- y1 + rnorm(length(y1), 0, 0.2)
d <- data.frame(x, y)
mymodel <- nls(y ~ v * x / (k + x),
start = list(v = 1.9, k = 0.19),
data = d)
mymodel_aug <- augment(mymodel)
ggplot(mymodel_aug, aes(x, y)) +
geom_point() +
geom_line(aes(y = .fitted), color = "red") +
theme_minimal()
例如,我可以轻松地从这样的线性模型计算预测区间:
## linear example
d2 <- d %>%
filter(x > 1)
mylinear <- lm(y ~ x, data = d2)
mypredictions <-
predict(mylinear, interval = "prediction", level = 0.95) %>%
as_tibble()
d3 <- bind_cols(d2, mypredictions)
ggplot(d3, aes(x, y)) +
geom_point() +
geom_line(aes(y = fit)) +
geom_ribbon(aes(ymin = lwr, ymax = upr), alpha = .15) +
theme_minimal()
最佳答案
根据链接的问题,它看起来像 investr::predFit
函数会做你想做的。
investr::predFit(mymodel,interval="prediction")
?predFit
没有解释间隔是如何计算的,但是 ?plotFit
说:Confidence/prediction bands for nonlinear regression (i.e., objects of class ‘nls’) are based on a linear approximation as described in Bates & Watts (2007). This fun[c]tion was in[s]pired by the ‘plotfit’ function from the ‘nlstools’ package.
也称为 Delta method (例如,见
emdbook::deltavar
)。
关于r - 如何从 nls 计算 95% 的预测区间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52973626/