如何解决创建预测概率图
我正在尝试创建一个预测概率图,其中两条线显示二元因变量的概率如何与我的自变量相关。代码如下:
#create binary predictor variable
survey <- survey %>%
mutate(unhappy_predict = ifelse(happiness < (mean(survey$happiness,na.rm = TRUE)
- sd(survey$happiness,na.rm = TRUE)),1,0))
#model
logmodel <- glm(unhappy_indic ~ choccon + sex + sec + ethnicity + age,family = binomial(link = "logit"),data = survey)
#predicted probabilities
predict_input2 <- expand.grid(choccon = seq(0,5000,50),sex = levels(survey$sex)[1],sec = levels(survey$sec)[1],ethnicity = levels(survey$ethnicity)[1],age = c(30,60))
pp <- predict(logmodel,predict_input2,type = "response")
predict_output2 <- bind_cols(logmodel_predict = pp,predict_input2)
#graph
ggplot(predict_output2) +
aes(x = choccon,y = logmodel_predict) +
geom_line() +
ylim(0,1)
当我运行所有这些时,我得到一个带有单个 L 形曲线的图表,如附图所示,而不是预测的概率图表。非常感谢您的帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。