I currently trying to use a glm to create a dose response curve. I am able to create the curve using the bio nominal family and probit function within glm but would like to plot the curves using ggplot and not R's basic plotting functions. When comparing the basic plot to the ggplot the curve produced by ggplot is not correct and I am unsure how make it the same as the basic plot. Additional the confidence intervals are not correct when plotting the curve with ggplot. Thanks for any help.
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
, you'll need to use something likefamily = "binomial"(link = "probit")
. You always have the option of adding the model predictions to the dataset and plotting those rather than relying ongeom_smooth
. Also, why are you using the inverse logit to make a line when you fit a probit regression? – aosmithfamily = "binomial"(link = "probit")
however under the current version ofggplot
additional model arguments have to be passed throughmethod.args
which produces the following warning and doesn't change the original graphIn eval(expr, envir, enclos) : non-integer #successes in a binomial glm!
. I can add the model predictions to the dataset and plot from there instead of relying ongeom_smooth
. Lastly, I was taught to use survivalship which will result in using the inverse logit compared to using mortality which will just use the logit. Maybe I was taught wrong. – Benjamin Hlina