微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

绘制预测值的数据

如何解决绘制预测值的数据

我估计了一个关于年龄和性别因素的就诊医生的二元选择模型(使用 MCMC)。在下一步中,我想为具有 x=age 的男性和女性创建预测值图(我选择了 25 到 60 年的间隔),并且 >y=预测值与 R。我想显示 99% 可信区间

情节应该是这样的:

enter image description here

我已经为这个图定义了数据。您会看到我定义了年龄区间和 99% 可信区间:

enter image description here

我使用 R 代码来定义数据:

datamen <- data.frame(age = seq(25,60,1),female = 0) #interval for women
datawomen <- data.frame (age = seq(25,female = 1) #interval for men
newdata <-rbind(datamen,datawomen) 
p_preds <- posterior_linpred (m1,newdata=newdata,transform=TRUE) #m1 is the data of my logit model
p_pred <- apply(p_preds,2,mean)
p_pred_lb <- apply (p_preds,quantile,probs=0.005) #credible interval lower end
p_pred_ub <- apply (p_preds,probs=0.995) #credible interval upper end
newdata <- cbind(newdata,p_pred,p_pred_lb,p_pred_ub) #new data is the data shown above

如果有人能帮助我,我将不胜感激。

解决方法

试试这个:

newdata %>% 
  mutate(female = factor(female,levels=c(0,1),labels=c("Male","Female"))) %>% 
  ggplot(aes(x=age)) + 
  geom_ribbon(aes(ymin = p_pred_lb,ymax = p_pred_ub,group=female),alpha=.25) + 
  geom_line(aes(y=p_pred,linetype=female)) + 
  geom_point(aes(y=p_pred,group=female)) + 
  labs(x="Age",y="Predicted Probability (99% Credible Intervals)",linetype="") + 
  theme_bw() + 
  theme(legend.position="bottom")

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。