如何在ggplot2中绘制logit和概率

约翰·克

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

对于下面的数据集,我一直试图在ggplot2中绘制logit和概率曲线,但均未成功。

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

我一直在使用的代码是

    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

您能告诉我如何改善代码,以便在同一张图上绘制这两条曲线吗?

谢谢

安德鲁

一种替代方法是生成自己的预测值并使用ggplot对其进行绘制-然后,您可以更好地控制最终图(而不是依赖于stat_smooth计算;这在使用多个协变量并且需要在绘制时保持其手段或方式的恒定)。

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

更好的单行

奖励,只是为了好玩:如果您使用自己的预测函数,那么您会为协变量着迷,例如显示模型如何适应以下不同级别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") 

更好的多行

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

ggplot2和GLM:绘制预测概率

改善ggplot2中的概率密度函数的绘制

如何在ggplot2中绘制组合的条形图和折线图

如何在ggplot2中的同一图上绘制线和点?

如何在ggplot2中使用纬度和箭头绘制风向

如何在ggplot2中绘制Gamma分布

如何在ggplot2中绘制函数族

如何在ggplot2或R中绘制雷达图

如何在ggplot2中绘制自举斜率的向量?

如何在ggplot2中绘制参数曲线

GGPLOT2:如何在ggplot()脚本中绘制特定选择

R:在ggplot2中绘制线性判别分析的后验分类概率

绘制预测概率(logit)

如何在R中的ggplot2中绘制混合效果模型估计?

如何在python中绘制概率图?

如何在ggplot2的线段之间绘制角度弧?

如何在R ggplot2中绘制3个data.frames

如何在ggplot2中正确绘制投影的网格化数据?

如何在ggplot2中绘制平滑的摘要统计信息

如何在ggplot2中绘制裁剪的密度图而不会丢失任何部分

如何在ggplot2中将标签绘制到对应的线(在此绘图上)?

如何在ggplot2中绘制具有不同因子的一维点的密度

如何在 R 中的 ggplot2 的条形图中绘制多个变量(即类别)

当同时绘制两者时,如何在ggplot2中显示间隔的`geom_boxplot`和`geom_dotplot`?

如何调整ggplot2中绘制轴的程度?

如何使用ggplot2在R中绘制菱形?

使用metafor和ggplot2时如何在散点图上绘制拟合的元回归线

如何在R中的geom_segment / ggplot2中绘制定向蜘蛛网?

如何在ggplot2中将.csv文件中的两个变量绘制为两行?