The_Last_Halibut
The_Last_Halibut

Reputation: 43

How to plot multiple logistic regression curves on one plot in Ggplot 2

A sample of my data frame is below:

ent corp smb  fit  se.fit   UL    LL  PredictedProb 
  1   0   0  -2.54   0.10  0.087 0.06         0.072   
  0   0   1  -3.71   0.05  0.026 0.02         0.023 
  0   1   0  -3.60   0.05  0.029 0.02         0.026      
  1   0   0  -2.54   0.10  0.087 0.060        0.072      
  0   0   1  -3.71   0.05  0.026 0.021        0.023      

I'd like to make 3 plots, a best fit line for each binary(sent,corp,smb) based on the predicted probability--and if possible I'd also like to add points for the predicted probabilities. So far, I've been able to create 3 separate plots but I would like to place all three on one plot. Below is what I have so far:

Here is the code for the Corp plot:

corp.line <- ggplot(newdata3, aes(corp,PredictedProb)) corp.line <- corp.line + stat_smooth(method = "glm") corp.line

Here is the code for the SMB plot:

smb.line <- ggplot(newdata3, aes(smb,PredictedProb)) smb.line <- smb.line + stat_smooth(method = "glm") smb.line

Here is the code for the Ent plot:

ent.line <- ggplot(newdata3, aes(enterprise,PredictedProb)) ent.line <- ent.line + stat_smooth(method="glm",family= binomial(link="logit")) ent.line

Also, in the previous plot, I was unable to plot the smooth curve around the best fit line using stat_smooth(method = "glm"). I had to also add family = binomial(link="logit"). Does anyone know why this may be the case.

To reiterate, my main question is how can I plot all three of these on on one plot and not have to split them up. Also, I'dlike to add points for the predicted probabilities.

Please excuse any improprieties on my behalf. I'm still new to stack exchange and ggplot2.

Upvotes: 0

Views: 2524

Answers (1)

Jake
Jake

Reputation: 520

You're not going to be able to plot the "S" shaped curves that you get with logistic regression because you do not have a continuous variable to plot over. Instead you're only going to be able to plot predicted values and CIs around those predicted values.

Create a column in your data frame that contains ent, corp, and smb.

newdata3<-read.table("clipboard", header=T)
newdata4<-unique(newdata3)[-4,] #different lower limits for smb... removing the second smb LL


newdata4$NewVar<-rep("",length(newdata[,1]))
newdata4$NewVar[which(newdata3$ent==1)]<-"ent"
newdata4$NewVar[which(newdata3$corp==1)]<-"corp"
newdata4$NewVar[which(newdata3$smb==1)]<-"smb"

windows(5,5)
ggplot(newdata4, aes(NewVar, PredictedProb, colour=NewVar)) + geom_point() +
    geom_errorbar(aes(ymin=LL, ymax=UL), width=.1, size=1)

enter image description here

Upvotes: 0

Related Questions