【问题标题】:graphing confidence intervals nls r绘制置信区间 nls r
【发布时间】:2015-04-07 16:45:39
【问题描述】:

我正在将一些发生率数据汇总在一起以提出建议。我知道数据整体呈 sigmoid 形状,所以我在 R 中使用 NLS 对其进行拟合。我试图获得一些置信区间来绘制,所以我使用引导参数,制作了三行,这就是我有的地方我的问题。自举 CI 给了我三组值,但由于方程,它们交叉的线。

Picture of Current Plot with "Ideal" Lines in Black

NLS 不是我的强项,所以也许我的做法不对。到目前为止,我主要使用自启动功能只是为了在情节上有所收获。第二个 NLS 方程将给出相同的输出,但我现在把它记下来,以便以后可以根据需要进行更改。

到目前为止,这是我的代码:

data <- readRDS(file = "Incidence.RDS")

inc <- nls(y ~ SSlogis(x, beta1, beta2, beta3), 
           data = data, 
           control = list(maxiter = 100))

b1 <- summary(inc)$coefficients[1,1]
b2 <- summary(inc)$coefficients[2,1]
b3 <- summary(inc)$coefficients[3,1]

inc2 <- nls(y ~ phi1 / (1 + exp(-(x - phi2) / phi3)), 
            data = data, 
            start = list(phi1 = b1, phi2 = b2, phi3 = b3), 
            control = list(maxiter = 100))

inc2.boot <- nlsBoot(inc2, niter = 1000)    

phi1 <- summary(inc2)$coefficients[1,1]
phi2 <- summary(inc2)$coefficients[2,1]
phi3 <- summary(inc2)$coefficients[3,1]

phi1_L <- inc2.boot$bootCI[1,2]
phi2_L <- inc2.boot$bootCI[2,2]
phi3_L <- inc2.boot$bootCI[3,2]

phi1_U <- inc2.boot$bootCI[1,3]
phi2_U <- inc2.boot$bootCI[2,3]
phi3_U <- inc2.boot$bootCI[3,3]

#plot lines

age <- c(20:95)
mean_incidence <- phi1 / (1 + exp(-(age - phi2) / phi3))
lower_incidence <- phi1_L / (1 + exp(-(age - phi2_L) / phi3_L))
upper_incidence <- phi1_U / (1 + exp(-(age - phi2_U) / phi3_U))

inc_line <- data.frame(age, mean_incidence, lower_incidence, upper_incidence)


p <- ggplot()
p <- (p
      + geom_point(data = data, aes(x = x, y = y), color = "darkgreen")
      + geom_line(data = inc_line, 
                  aes(x = age, y = mean_incidence), 
                  color = "blue", 
                  linetype = "solid")

      + geom_line(data = inc_line, 
                  aes(x = age, y = lower_incidence), 
                  color = "blue", 
                  linetype = "dashed")

      + geom_line(data = inc_line, 
                  aes(x = age, y = upper_incidence), 
                  color = "blue", 
                  linetype = "dashed")

      + geom_ribbon(data = inc_line, 
                    aes(x = age, ymin = lower_incidence, ymax = upper_incidence), 
                    fill = "blue", alpha = 0.20)

      + labs(x = "\nAge", y = "Incidence (per 1,000 person years)\n")
      )

print(p)

这是数据的a link

任何关于下一步做什么的帮助,或者考虑到我目前的设置是否有可能,我们将不胜感激。

谢谢

【问题讨论】:

  • 参数的置信区间和预测的置信区间是有区别的。您不能像您尝试做的那样从前者计算后者,例如,因为参数通常是相关的。我建议你引导预测。
  • predict.nls 的作者显然计划提供 SE 的围绕预测,但 se.fit 参数目前被忽略。 stackoverflow.com/questions/12425920/…
  • 罗兰建议代码:r-bloggers.com/…
  • 非常感谢你们。这更有意义。干杯

标签: r ggplot2 confidence-interval nls


【解决方案1】:

在 drc 包中尝试plot.drc

library(drc)

fm <- drm(y ~ x, data = data, fct = LL.3())
plot(fm, type = "bars")

附:请在您的问题中包含library 电话,以便代码是独立且完整的。对于这里的问题:library(ggplot2); library(nlstools)

【讨论】:

  • 感谢您的回答。没有听说过 drc 包,但看起来这在未来也很有用。
猜你喜欢
  • 2014-09-03
  • 1970-01-01
  • 1970-01-01
  • 2021-12-21
  • 1970-01-01
  • 1970-01-01
  • 2015-07-12
  • 2019-06-28
  • 1970-01-01
相关资源
最近更新 更多