r - 如何从 nls 计算 95% 的预测区间

标签 r predict nls non-linear-regression

从这里借用示例数据 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()

enter image description here

例如,我可以轻松地从这样的线性模型计算预测区间:
## 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()

enter image description here

最佳答案

根据链接的问题,它看起来像 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/

相关文章:

python - PySAL OLS 模型 : AttributeError: 'OLS' object has no attribute 'predict'

r - 如何在 R 中使用 multinom() 进行预测

r - 估计命令如何查找 R 公式中的变量名称?

r - 在 R CMD 检查中禁用构建 PDF

python - LightGBM多分类预测结果

r - Shiny :从 Shiny 的应用程序中打开新的浏览器标签

R_using nlsLM() 有约束

r - 使用 nls() 进行非线性拟合在初始参数估计时给了我奇异的梯度矩阵。为什么?

r - 使用 R 中的 xlsx 包更新 Excel 电子表格数据

R Shiny - cex 值不正确 - 上传文本文件,wordcloud 包