代码之家  ›  专栏  ›  技术社区  ›  C.Robin

将marginaffects::predictions()的结果连接回main df?

  •  0
  • C.Robin  · 技术社区  · 1 年前

    我跑了一个 lm 建模然后运行 predictions() 从…起 marginaleffects 在输出上。我想通过我输入到 流明 )但我看不出在这种情况下使用什么选项是正确的。

    有人知道我在这里需要做什么吗?的输出 预测() 有一个 rowid 但加入一个指数(可能已经改变了顺序)似乎是一种冒险的前进方式。

    例如,采用以下代码(来自文档):

    mod <- lm(mpg ~ hp + factor(cyl), data = mtcars)
    pred <- predictions(mod)
    
    pred %>% head()
    
     Estimate Std. Error    z Pr(>|z|) 2.5 % 97.5 %
         20.0      1.204 16.6   <0.001  17.7   22.4
         20.0      1.204 16.6   <0.001  17.7   22.4
         26.4      0.962 27.5   <0.001  24.5   28.3
         20.0      1.204 16.6   <0.001  17.7   22.4
         15.9      0.992 16.0   <0.001  14.0   17.9
         20.2      1.219 16.5   <0.001  17.8   22.5
    
    Columns: rowid, estimate, std.error, statistic, p.value, conf.low, conf.high, mpg, hp, cyl 
    
    
    > mtcars %>% head()
                       mpg cyl disp  hp drat    wt  qsec vs am gear carb
    Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
    Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
    Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
    Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
    Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
    Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
    

    中有1个预测 pred 对于中的每一行 mtcars 但我该如何加入这些?

    1 回复  |  直到 1 年前
        1
  •  2
  •   Vincent    1 年前

    啊,我明白了!看来您只需要将原始df传递给 newdata 的论点 predictions 。例如。

    mod <- lm(mpg ~ hp + factor(cyl), data = mtcars)
    pred <- predictions(mod, newdata = mtcars)
    
    pred %>% head()
    
     Estimate Std. Error    z Pr(>|z|) 2.5 % 97.5 % cyl disp  hp drat   wt qsec vs am gear
         20.0      1.204 16.6   <0.001  17.7   22.4   6  160 110 3.90 2.62 16.5  0  1    4
         20.0      1.204 16.6   <0.001  17.7   22.4   6  160 110 3.90 2.88 17.0  0  1    4
         26.4      0.962 27.5   <0.001  24.5   28.3   4  108  93 3.85 2.32 18.6  1  1    4
         20.0      1.204 16.6   <0.001  17.7   22.4   6  258 110 3.08 3.21 19.4  1  0    3
         15.9      0.992 16.0   <0.001  14.0   17.9   8  360 175 3.15 3.44 17.0  0  0    3
         20.2      1.219 16.5   <0.001  17.8   22.5   6  225 105 2.76 3.46 20.2  1  0    3
     carb
        4
        4
        1
        1
        2
        1
    
    Columns: rowid, estimate, std.error, statistic, p.value, conf.low, conf.high, mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb