绘制置信区间nls r

时间:2015-04-07 16:45:39

标签: r ggplot2 confidence-interval nls

我正在为提案提供一些发生率数据。我知道数据总体上呈现S形状,所以我在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

有关下一步该怎么做的任何帮助,或者如果我目前的设置,这甚至是可能的,将不胜感激。

由于

1 个答案:

答案 0 :(得分:1)

在drc包中尝试plot.drc

library(drc)

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

screenshot

P.S。请在问题中加入library来电,以便代码自包含且完整。对于这里的问题:library(ggplot2); library(nlstools)