使用ggplot2绘制loess和glm图形

4
我试图绘制二项选择glm模型的预测结果,将其与泰坦尼克号数据中的实际概率进行比较并显示在分类和性别方面的差异。我使用分面展示,但我有两个问题无法解决。第一个是我想限制loess曲线在0到1之间,但如果我在图的末尾添加选项ylim(c(0,1)),则围绕loess曲线的带状物会被切断,如果其一侧处于界限之外。第二件事是我想从每个细分中的最小x值(glm的预测概率)到该细分内的最大x值,以及y = 1绘制一条线,以显示glm预测的概率。

loess-titanic

#info on this data http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic3info.txt
load(url('http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic3.sav'))
titanic <- titanic3[ ,-c(3,8:14)]; rm(titanic3)
titanic <- na.omit(titanic) #probably missing completely at random
titanic$age <- as.numeric(titanic$age)
titanic$sibsp <- as.integer(titanic$sibsp)
titanic$survived <- as.integer(titanic$survived)

training.df <- titanic[sample(nrow(titanic), nrow(titanic) / 2), ]
validation.df <- titanic[!(row.names(titanic) %in% row.names(training.df)), ]


glm.fit <- glm(survived ~ sex + sibsp + age + I(age^2) + factor(pclass) + sibsp:sex,
               family = binomial(link = "probit"), data = training.df)

glm.predict <- predict(glm.fit, newdata = validation.df, se.fit = TRUE, type = "response")

plot.data <- data.frame(mean = glm.predict$fit, response = validation.df$survived,
                        class = validation.df$pclass, sex = validation.df$sex)

require(ggplot2)
ggplot(data = plot.data, aes(x = as.numeric(mean), y = as.integer(response))) + geom_point() +
       stat_smooth(method = "loess", formula = y ~ x) +
       facet_wrap( ~ class + sex, scale = "free") + ylim(c(0,1)) + 
       xlab("Predicted Probability of Survival") + ylab("Empirical Survival Rate")
1个回答

2
你的第一个问题的答案是使用coord_cartesian(ylim=c(0,1))而不是ylim(0,1);这是一个常见问题。
对于你的第二个问题,在ggplot内部可能有一种方法来解决它,但是对我来说更容易在外部总结数据。
g0 <- ggplot(data = plot.data, aes(x = mean, y = response)) + geom_point() +
             stat_smooth(method = "loess") +
             facet_wrap( ~ class + sex, scale = "free") + 
             coord_cartesian(ylim=c(0,1))+
             labs(x="Predicted Probability of Survival",
                  y="Empirical Survival Rate")

(我通过消除一些默认值并使用“labs”来稍微缩短了你的代码。)
ss <- ddply(plot.data,c("class","sex"),summarise,minx=min(mean),maxx=max(mean))
g0 + geom_segment(data=ss,aes(x=minx,y=minx,xend=maxx,yend=maxx),
                  colour="red",alpha=0.5)

谢谢,如果我用 mean 替换 x,那就可以了。非常感谢。 - Zach
也在分段部分(可能是因为我解释不清楚),分段的y坐标应该与x坐标相匹配,而不是从0到1。 - Zach

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接