ggpredict()和predictions()有什么区别?

问题描述 投票:0回答:1

我想生成一些预测概率,有人推荐我使用两个命令:

ggeffects::ggpredict()
margin effects::predictions()
。然而,一些结果看起来相似(conf.high),而其他结果看起来不同(conf.low)。这里发生了什么?任何见解都会有帮助。

这是一个例子:

logit <- glm(vs ~ mpg + cyl + disp + hp + drat + wt + qsec + am + gear + carb, mtcars, family = "poisson")

library(ggeffects)
ggpredict(logit, terms=c("mpg")) %>% as_tibble()

library(marginaleffects)
predictions(logit, by=c("mpg")) %>% as_tibble()
r predict
1个回答
0
投票

这两个函数默认执行不同的操作。

    带有术语参数的
  1. ggpredict()
    计算数据帧每一行的预测,其中
    mpg
    取 10 到 32 之间的值,所有其他变量均保持其平均值,包括
    am
    ,甚至设置为 0.41如果它可能是二元/分类的。
  2. 带有
  3. predictions()
     参数的 
    by
    计算实际观察到的数据帧中每一行的预测,然后对
    mpg
    的每个唯一值取这些预测的平均值。

换句话说,

ggpredict()
报告对“合成”数据的预测——假设单位在除一维之外的所有维度上精确平均。
predictions()
报告根据实际观察到的数据做出的预测的平均值。

通过使用

ggpredict()
参数和
predictions()
函数,可以使用
newdata
重现与
datagrid()
相同的结果:

library(marginaleffects)
library(ggeffects)
logit <- glm(vs ~ mpg + cyl + disp + hp + drat + wt + qsec + am + gear + carb, mtcars, family = "poisson")

ggpredict(logit,
  terms = c("mpg")) |>
  data.frame() |> head()
#    x predicted std.error   conf.low conf.high group
# 1 10 0.2243430 1.2888961 0.01793916 2.8055810     1
# 2 12 0.2018065 1.0826924 0.02417384 1.6847077     1
# 3 14 0.1815340 0.9117794 0.03039834 1.0840915     1
# 4 16 0.1632979 0.7991309 0.03410037 0.7819915     1
# 5 18 0.1468937 0.7707321 0.03243060 0.6653523     1
# 6 20 0.1321375 0.8352217 0.02570893 0.6791536     1

predictions(logit,
  newdata = datagrid(mpg = seq(10, 34, by = 2), am = mean)) |>
  head()
# 
#  mpg    am Estimate Pr(>|z|)   S  2.5 % 97.5 %  cyl disp  hp drat   wt qsec
#   10 0.406    0.224   0.2462 2.0 0.0179  2.806 6.19  231 147  3.6 3.22 17.8
#   12 0.406    0.202   0.1394 2.8 0.0242  1.685 6.19  231 147  3.6 3.22 17.8
#   14 0.406    0.182   0.0613 4.0 0.0304  1.084 6.19  231 147  3.6 3.22 17.8
#   16 0.406    0.163   0.0233 5.4 0.0341  0.782 6.19  231 147  3.6 3.22 17.8
#   18 0.406    0.147   0.0128 6.3 0.0324  0.665 6.19  231 147  3.6 3.22 17.8
#   20 0.406    0.132   0.0154 6.0 0.0257  0.679 6.19  231 147  3.6 3.22 17.8
# 
# Columns: rowid, estimate, p.value, s.value, conf.low, conf.high, vs, cyl, disp, hp, drat, wt, qsec, gear, carb, mpg, am 
# Type:  invlink(link)

PS:这不是 Logit 模型。

© www.soinside.com 2019 - 2024. All rights reserved.