【发布时间】:2017-06-21 20:14:04
【问题描述】:
我目前正在尝试使用 glm 创建剂量反应曲线。我可以使用 glm 中的 bionominal family 和 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"))
【问题讨论】:
-
他们是不同的。并不意味着他们中的任何一个都是错误的。不同的方法和公式。
-
如果你想使用
ggplot中的概率链接,你需要使用family = "binomial"(link = "probit")之类的东西。您始终可以选择将模型预测添加到数据集并绘制它们,而不是依赖geom_smooth。另外,当您拟合概率回归时,为什么要使用逆 logit 来制作一条线? -
@aosmith 我尝试使用
family = "binomial"(link = "probit")但是在ggplot的当前版本下,必须通过method.args传递额外的模型参数,这会产生以下警告并且不会更改原始图形@ 987654328@。我可以将模型预测添加到数据集并从那里进行绘图,而不是依赖geom_smooth。最后,我被教导使用生存船,这将导致使用逆对数,而死亡率则仅使用对数。也许我被教错了。