How to plot logit and probit in ggplot2

前端 未结 2 718
别跟我提以往
别跟我提以往 2020-12-08 11:59

This is almost surely a newbish question/

For the dataset below I have been trying to plot both the logit and the probit curves in ggplot2 without success.

2条回答
  •  生来不讨喜
    2020-12-08 12:31

    These two functions that you use in stat_smooth overlap. This is why you think you can't see those two on the same graph. Running the below will make this clear having a blue colour for the second line.

    library(ggplot2)
    TD<-mydata$TD
    Temp<-mydata$Temp
    g <- qplot(Temp,TD)+geom_point()+stat_smooth(method="glm",family="binomial",formula=y~x,col="red")
    g1<-g+labs(x="Temperature",y="Thermal Distress")
    g1
    g2<-g1+stat_smooth(method="glm",family="binomial",link="probit",formula=y~x,add=T,col='blue')
    g2
    

    If you run a different family on the second stat_smooth, for example a poisson glm:

    library(ggplot2)
    TD<-mydata$TD
    Temp<-mydata$Temp
    g <- qplot(Temp,TD)+geom_point()+stat_smooth(method="glm",family="binomial",formula=y~x,col="red")
    g1<-g+labs(x="Temperature",y="Thermal Distress")
    g1
    g2<-g1+stat_smooth(method="glm",family="poisson",link="log",formula=y~x,add=T,col='blue')
    g2
    

    Then you can see that indeed two lines are plotted:

    enter image description here

提交回复
热议问题