如何在ggplot2中绘制logit和probit [英] How to plot logit and probit in ggplot2

查看:164
本文介绍了如何在ggplot2中绘制logit和probit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这几乎肯定是一个新问题/ b

对于下面的数据集,我一直试图在ggplot2中绘制logit和probit曲线而没有成功。

  Ft Temp TD 

1 66 0
6 72 0
11 70 1
16 75 0
21 75 1
2 70 1
7 73 0
12 78 0
17 70 0
22 76 0
3 69 0
8 70 0
13 67 0
18 81 0
23 58 1
4 68 0
9 57 1
14 53 1
19 76 0
5 67 0
10 63 1
15 67 0
20 79 0

我一直天真地使用的代码是

 <$ c (temp,TD)+ geom_point()+ stat_smooth(method =value)。 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)
g2

请问我可以如何改进我的代码如何将这两条曲线绘制在同一个图上?

谢谢

解决方案

另一种方法是生成自己的预测值并用ggplot绘制它们 - 然后您可以对最终图进行更多控制(而不是依赖 stat_smooth 进行计算;这是特别有用的,如果你使用多个协变量,并且在绘图时需要保持一些不变的方式或模式)。

 库(ggplot2)

#生成数据
mydata< - data.frame(Ft = c(1,6,11,16,21,2,7,12,17,22 ,3,8,
13,18,23,4,9,14,19,5,10,15,20),
Temp = c(66,72,70,75,75, 70,73,78,70,76,69,70,
67,81,58,68,57,53,76,67,63,67,79),
TD = c(0 ,0,1,0,1,1,0,0,0,0,0,0,
0,0,1,0,1,1,0,0,1,0,0))的情况下,

#运行逻辑回归模型
模型< - glm(TD〜Temp,data = mydata,family = binomial(link =logit))

#创建假设值的临时数据框
temp.data< - data.frame(Temp = seq(53,81,0.5))

#预测给定模型的拟合值和假设数据
predict.data < - as.data.frame(预测(model,newdata = temp.data,
type =link,se = TRUE))

#合并假设数据和预测值
new.data< - cbind(temp.data,predicted.data)

#计算置信区间
std < - qnorm(0.95 / 2 + 0.5)
new.data $ ymin< - model $ family $ linkinv(new.data $ fit - std * new.data $ se)
new.data $ ymax< - model $ family $ linkinv(new。数据$ fit + std * new.data $ se)
new.data $ fit< - model $ family $ linkinv(new.data $ fit)#重新调整为0-1

#绘制所有
p <-ggplot(mydata,aes(x = Temp,y = TD))
p + geom_point()+
geom_ribbon(data = new.data,aes(y = y = ymax,ymax = ymax),alpha = 0.5)+
geom_line(data = new.data,aes(y = fit))+
labs(x =Temperature,y = Thermal Distress)



奖金,只是为了好玩:If你可以使用你自己的预测函数,你可以对协变量感到疯狂,例如显示模型如何适用于 Ft 的不同级别:

 #另一种方式,如果您想疯狂
#运行具有两个协变量的逻辑回归模型
模型< - glm(TD〜Temp + Ft, data = mydata,family = binomial(link =logit))

#创建假设值的临时数据框
temp.data< - data.frame(Temp = rep seq(53,81,0.5),2),
Ft = c(rep(3,57),rep(18,57)))

#预测给定的拟合值模型和假设数据
predict.data< - as.data.frame(预测(model,newdata = temp.data,
type =link,se = TRUE))

#合并假设数据和预测值
new.data < - cbind(temp.data,predicted.data)

#计算置信区间
std< - qnorm(0.95 / 2 + 0.5)
new.data $ ymin< - model $ family $ linkinv(new.data $ fit - std * new.data $ se)
new.data $ ymax< - model $ family $ linkinv(new.data $ fit + std * new.data $ se)
new.data $ fit< - model $ family $ linkinv(new.data $ fit)#重新调整为0-1

#绘制所有
p< - ggplot(mydata, aes(x = Temp,y = TD))
p + geom_point()+
geom_ribbon(data = new.data,aes(y = fit,ymin = ymin,ymax = ymax,
fill = as.factor(Ft)),alpha = 0.5)+
geom_line(data = new.data,aes(y = fit,color = as.factor(Ft)))+
labs x =温度,y =热障碍)


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.

Ft Temp TD

    1  66 0
    6  72 0
    11 70 1
    16 75 0
    21 75 1
    2   70 1
    7   73 0
    12 78 0
    17 70 0
    22 76 0
    3   69 0
    8   70 0
    13 67 0
    18 81 0
    23 58 1
    4   68 0
    9   57 1
    14 53 1
    19 76 0
    5   67 0
    10 63 1
    15 67 0
    20 79 0

The code I have been naively using is

    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)
    g2

Could you please tell me how I could improve my code so as to plot these two curves on the same graph?

Thank you

解决方案

An alternative approach would be to generate your own predicted values and plot them with ggplot—then you can have more control over the final plot (rather than relying on stat_smooth for the calculations; this is especially useful if you're using multiple covariates and need to hold some constant at their means or modes when plotting).

library(ggplot2)

# Generate data
mydata <- data.frame(Ft = c(1, 6, 11, 16, 21, 2, 7, 12, 17, 22, 3, 8, 
                            13, 18, 23, 4, 9, 14, 19, 5, 10, 15, 20),
                     Temp = c(66, 72, 70, 75, 75, 70, 73, 78, 70, 76, 69, 70, 
                              67, 81, 58, 68, 57, 53, 76, 67, 63, 67, 79),
                     TD = c(0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 
                            0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0))

# Run logistic regression model
model <- glm(TD ~ Temp, data=mydata, family=binomial(link="logit"))

# Create a temporary data frame of hypothetical values
temp.data <- data.frame(Temp = seq(53, 81, 0.5))

# Predict the fitted values given the model and hypothetical data
predicted.data <- as.data.frame(predict(model, newdata = temp.data, 
                                        type="link", se=TRUE))

# Combine the hypothetical data and predicted values
new.data <- cbind(temp.data, predicted.data)

# Calculate confidence intervals
std <- qnorm(0.95 / 2 + 0.5)
new.data$ymin <- model$family$linkinv(new.data$fit - std * new.data$se)
new.data$ymax <- model$family$linkinv(new.data$fit + std * new.data$se)
new.data$fit <- model$family$linkinv(new.data$fit)  # Rescale to 0-1

# Plot everything
p <- ggplot(mydata, aes(x=Temp, y=TD)) 
p + geom_point() + 
  geom_ribbon(data=new.data, aes(y=fit, ymin=ymin, ymax=ymax), alpha=0.5) + 
  geom_line(data=new.data, aes(y=fit)) + 
  labs(x="Temperature", y="Thermal Distress") 

Bonus, just for fun: If you use your own prediction function, you can go crazy with covariates, like showing how the model fits at different levels of Ft:

# Alternative, if you want to go crazy
# Run logistic regression model with two covariates
model <- glm(TD ~ Temp + Ft, data=mydata, family=binomial(link="logit"))

# Create a temporary data frame of hypothetical values
temp.data <- data.frame(Temp = rep(seq(53, 81, 0.5), 2),
                        Ft = c(rep(3, 57), rep(18, 57)))

# Predict the fitted values given the model and hypothetical data
predicted.data <- as.data.frame(predict(model, newdata = temp.data, 
                                        type="link", se=TRUE))

# Combine the hypothetical data and predicted values
new.data <- cbind(temp.data, predicted.data)

# Calculate confidence intervals
std <- qnorm(0.95 / 2 + 0.5)
new.data$ymin <- model$family$linkinv(new.data$fit - std * new.data$se)
new.data$ymax <- model$family$linkinv(new.data$fit + std * new.data$se)
new.data$fit <- model$family$linkinv(new.data$fit)  # Rescale to 0-1

# Plot everything
p <- ggplot(mydata, aes(x=Temp, y=TD)) 
p + geom_point() + 
  geom_ribbon(data=new.data, aes(y=fit, ymin=ymin, ymax=ymax, 
                                       fill=as.factor(Ft)), alpha=0.5) + 
  geom_line(data=new.data, aes(y=fit, colour=as.factor(Ft))) + 
  labs(x="Temperature", y="Thermal Distress") 

这篇关于如何在ggplot2中绘制logit和probit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆