使用 ggplot 和 glm 绘制剂量反应曲线的问题

Ben*_*ina 2 r ggplot2

我目前尝试使用 glm 来创建剂量反应曲线。我能够使用 glm 中的 bio 名义族和 probit 函数创建曲线,但想使用 ggplot 而不是 R 的基本绘图函数绘制曲线。将基本图与 ggplot 进行比较时,ggplot 生成的曲线不正确,我不确定如何使其与基本图相同。此外,使用 ggplot 绘制曲线时,置信区间不正确。谢谢你的帮助。

library(ggplot2)
library(Hmisc)
library(plyr)
library(MASS)



create dataframe: 
#1) column is exposure concentration
#2) column is the total number of organism died over 12 h of exposure to the 
    corresponding concentration 
#3) column is the total number that survived over 12 h to the corresponding 
    concentration
#4) column is the total number of organism exposed to the corresponding 
    concentration
#5) fifth is the percentage of organism that survived exposure at the 
    corresponding concentration 

 conc <- c(0.02, 0.45, 0.46, 0.50, 0.78, 0.80, 0.80, 0.92, 0.93, 1.00, 1.16, 
   1.17, 1.17, 1.48,1.51, 1.55, 1.88, 1.90, 2.02)

 dead <- c(0, 0,  0,  0,  0,  0,  0,  0,  0,  1,  7, 11, 4, 14, 14, 12, 12, 18, 17)

 survive <- c(15, 16, 15, 15, 15, 16, 14, 14, 10, 15, 12,  5, 12,  0,  1,  3,  0,  0,  0)

 total <- c(15, 16, 15, 15, 15, 16, 14, 14, 10, 16, 19, 16, 16, 14, 15, 15, 12, 18, 17)

 perc <- c(1.00, 1.00, 1.00, 1.00, 1.00,1.00, 1.00, 1.00, 1.00, 0.94,0.63, 
      0.31,0.75,0.00, 0.07, 0.20, 0.00, 0.00,0.00)

 data<-data.frame(conc,dead,survive,total,perc)
 head(data)
 attach(data)
 #create matrix of dead and survival
 y = cbind(dead,survive)

 #create binomial glm (probit model)
 model.results = glm(data = data, y ~ conc,binomial(link="probit"))
 summary(model.results)



 #use function from MASS to calculate LC
 dose.p(model.results,p=0.5)
 dose.p(model.results,p=c(0.1,0.25,0.5,0.99))

 #plot curve 
 plot(conc,(survive/(survive+dead)), ylab = "Percent Survival", 
 xlab="Concentration ")


 #To make function use the Estimate parameters from the binomial glm 
  used above
  logisticline <- function(z) {eta = -6.7421 + 5.4468 * z;1 / (1 + 
  exp(eta))}
  x <- seq(0,200.02,0.01)
  lines(x,logisticline(x),new = TRUE)




  #plot using ggplot

  ggplot(data, aes(x = conc, y = perc)) +
  geom_point() +
  geom_smooth(method="glm",method.args = list(family = "binomial"))
Run Code Online (Sandbox Code Playgroud)

aos*_*ith 5

您可以使用ggplot2绘制拟合线,方法是根据模型进行预测或直接使用 拟合模型geom_smooth。要执行后者,您需要将死亡比例作为响应变量并total作为权重来拟合模型,而不是使用成功和失败的矩阵作为响应变量。

使用glm,拟合具有比例加权重的模型如下所示:

# Calculate proportion
data$prop = with(data, dead/total)

# create binomial glm (probit model)
model.results2 = glm(data = data, prop ~ conc, 
                    family = binomial(link="probit"), weights = total)
Run Code Online (Sandbox Code Playgroud)

您可以使用您拥有的数据集进行预测,或者为了使线条更平滑,您可以创建一个新的数据集进行预测,该数据集具有更多的值conc

preddat = data.frame(conc = seq(0, 2.02, .01) )
Run Code Online (Sandbox Code Playgroud)

现在,您可以通过predict使用此 data.frame 作为 来从模型中进行预测newdata。如果您使用type = "response",您将通过反向链接获得数据规模的预测。因为您拟合了概率模型,所以这将使用逆概率。在您的示例中,您使用逆 logit 进行预测。

# Predictions with inverse probit
preddat$pred = predict(model.results2, newdata = preddat, type = "response")
# Predictions with inverse logit (?)
preddat$pred2 = plogis( predict(model.results2, newdata = preddat) )
Run Code Online (Sandbox Code Playgroud)

要在 中拟合 probit 模型ggplot,您需要使用比例作为 的y变量weight = total。在这里,我添加了模型预测中的线,以便您可以看到拟合的概率模型ggplot给出了与拟合概率模型相同的估计线。使用逆 logit 会给你一些不同的东西,这并不奇怪。

ggplot(data, aes(conc, prop) ) +
     geom_smooth(method = "glm", method.args = list(family = binomial(link = "probit") ), 
                 aes(weight = total, color = "geom_smooth line"), se = FALSE) +
     geom_line(data = preddat, aes(y = pred, color = "Inverse probit") ) +
     geom_line(data = preddat, aes(y = pred2, color = "Inverse logit" ) )
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明