【问题标题】:how to plot logistic regression on the log odd scale using ggplot2如何使用ggplot2在对数奇数尺度上绘制逻辑回归
【发布时间】:2017-09-28 03:50:04
【问题描述】:

我正在尝试以对数奇数标度绘制逻辑回归的结果。

load(url("https://github.com/bossaround/question/raw/master/logisticregressdata.RData"))

ggplot(D, aes(Year, as.numeric(Vote), color = as.factor(Male))) +
  stat_smooth( method="glm", method.args=list(family="binomial"), formula = y~x + I(x^2), alpha=0.5, size = 1, aes(fill=as.factor(Male))) +
  xlab("Year") 

但这个情节是在 0~1 的范围内。我想这是概率尺度(如果我错了,请纠正我)?

我真正想要的是在将其转换为概率之前将其绘制在对数奇数刻度上,就像逻辑回归报告一样。

理想情况下,我想在控制外国后,按男性绘制投票与年份之间的关系,如下所示:

   Model <-  glm(Vote ~ Year + I(Year^2) + Male + Foreign, family="binomial", data=D)

我可以根据summary(Model) 手动绘制线,但我也想绘制置信区间。

类似于我在网上找到的本文档第 44 页上的图片:http://www.datavis.ca/papers/CARME2015-2x2.pdf。我的会有一条二次曲线。

谢谢!

【问题讨论】:

  • predict(Model, type="link", se.fit=TRUE) 将为您提供对数赔率规模的预测。 se.fit=TRUE 将包括预测的标准误差。预测将针对用于拟合模型的观察结果。要获得对自变量 (IV) 其他值的预测,请使用 predict 的 newdata 参数并包含一个新数据框(您创建的)以及您希望预测结果的 IV 值(例如,@ 987654328@).
  • 如果你有预测,你可以在 ggplot 中使用geom_line 绘制它们。使用geom_ribbon 绘制置信区间。

标签: r ggplot2 logistic-regression


【解决方案1】:

要绘制具有多个变量的模型的预测,应该制作模型,预测新数据以生成预测并绘制该模型

Model <-  glm(Vote ~ Year + I(Year^2) + Male + Foreign, family="binomial", data=D)
for_pred = expand.grid(Year = seq(from = 2, to = 10, by = 0.1), Male = c(0,1), Foreign = c(0,1)) #generate data to get a smooth line

for_pred = cbind(for_pred, predict(Model, for_pred, type = "link", se.fit= T)) 
#if the probability scale was needed: `type = "response`

library(ggplot2)
ggplot(for_pred, aes(Year, fit, color = as.factor(Male))) +
  geom_line() +
  xlab("Year")+
  facet_wrap(~Foreign)  + #important step - check also how it looks without it
  geom_ribbon(aes(ymax = fit + se.fit, ymin = fit - se.fit, fill = as.factor(Male)), alpha = 0.2) 

#omit the color by `color = NA` or by `inherit.aes = F` (if like this, one should provide the data and full `aes` mapping for  geom_ribbon). 
#If geom_ribbon should not have a mapping, specify `fill` outside of `aes` like: `fill = grey80`.

查看库 sjPlot

【讨论】:

    【解决方案2】:

    使用augmented() 来自broom() 的进一步答案:

    Model <-  glm(Vote ~ Year + I(Year^2) + Male + Foreign, family="binomial", data=D)
    summary(Model)
    
    
    # augmented data frame
    
    model.df = augment(Model) %>% rename(log_odds = `.fitted`, 
                                           Sex = Male)
    
    glimpse(model.df.1)
    Observations: 46,398
    Variables: 13
    $ .rownames  <chr> "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21"...
    $ Vote       <dbl> 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, ...
    $ Year       <int> 2, 3, 4, 5, 2, 3, 2, 3, 4, 5, 6, 2, 2, 2, 3, 4, 2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5, 2, 3, 2, 3, 4, 5, 2, 3, 4, 5, ...
    $ I.Year.2.  <S3: AsIs>  4,  9, 16, 25,  4,  9,  4,  9, 16, 25, 36,  4,  4,  4,  9, 16,  4,  9, 16, 25, 36, 49, 64, 81,  4,  9, 16, 2...
    $ Sex        <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...
    $ Foreign    <int> 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
    $ log_odds   <dbl> -0.01910985, -0.68184753, -1.14317053, -1.40307885, 0.26930939, -0.39342829, -0.01910985, -0.68184753, -1.14317053...
    $ .se.fit    <dbl> 0.01675017, 0.01466136, 0.01790972, 0.02058514, 0.01931826, 0.01777691, 0.01675017, 0.01466136, 0.01790972, 0.0205...
    $ .resid     <dbl> -1.1693057, -0.9047053, -0.7439452, 1.8016037, -1.2937083, 1.3483961, -1.1693057, -0.9047053, -0.7439452, -0.66303...
    $ .hat       <dbl> 7.013561e-05, 4.794678e-05, 5.879536e-05, 6.711744e-05, 9.162739e-05, 7.602458e-05, 7.013561e-05, 4.794678e-05, 5....
    $ .sigma     <dbl> 1.124879, 1.124884, 1.124886, 1.124861, 1.124876, 1.124874, 1.124879, 1.124884, 1.124886, 1.124887, 1.124860, 1.12...
    $ .cooksd    <dbl> 1.376354e-05, 4.849628e-06, 3.749311e-06, 5.461011e-05, 2.399355e-05, 2.253792e-05, 1.376354e-05, 4.849628e-06, 3....
    $ .std.resid <dbl> -1.1693467, -0.9047270, -0.7439671, 1.8016642, -1.2937676, 1.3484474, -1.1693467, -0.9047270, -0.7439671, -0.66305...
    
    
    #visualise
    
            ggplot(model.df.1, aes(Year, log_odds, colour = Sex)) + 
            geom_line() + 
            geom_smooth(se = TRUE) +
           facet_wrap( ~ Foreign)
    

    这给出了:

    【讨论】:

      【解决方案3】:

      您的方法是正确的,但您需要使用您构建的模型来预测值,如下所示:

      ModelPredictions <- predict(Model , type="response")
      

      之后,您可以使用 ggplot 进行绘图:

      ggplot(D, aes(x=ModelPredictions , y=D$Vote )) +
        geom_point()  +  stat_smooth(method="glm", se=FALSE, method.args = list(family=binomial)) +  facet_wrap( ~ Foreign)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-05
        • 1970-01-01
        • 2017-11-05
        • 2015-05-15
        • 2014-04-22
        • 1970-01-01
        • 2021-11-23
        • 2019-10-22
        相关资源
        最近更新 更多