【问题标题】:How to calculate 95% prediction interval from nls如何从 nls 计算 95% 的预测区间
【发布时间】:2019-03-29 03:25:00
【问题描述】:

借用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()

【问题讨论】:

    标签: r predict nls non-linear-regression


    【解决方案1】:

    根据链接的问题,investr::predFit 函数似乎可以满足您的需求。

    investr::predFit(mymodel,interval="prediction")
    

    ?predFit 没有解释如何计算间隔,但?plotFit 说:

    非线性回归的置信/预测带(即, “nls”类的对象)基于线性近似 在 Bates & Watts (2007) 中描述。这个功能 [c]tion 是由 “nlstools”包中的“plotfit”函数。

    也称为Delta method(例如参见emdbook::deltavar)。

    【讨论】:

    • 感谢您的回答...根据您的评论,我最终检查了investr:::predFit.nls 中的源代码。从那里我得到了程序,现在我编写了自己的函数..谢谢!!!
    猜你喜欢
    • 1970-01-01
    • 2021-05-18
    • 2020-06-06
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多