计算边缘效应:为什么ggeffect和ggemeans给出不同的答案?
示例
library(glmmTMB)
library(ggeffects)
## Zero-inflated negative binomial model
(m <- glmmTMB(count ~ spp + mined + (1|site),
ziformula=~spp + mined,
family=nbinom2,
data=Salamanders,
na.action = "na.fail"))
summary(m)
ggemmeans(m, terms="spp")
spp | Predicted | 95% CI
--------------------------------
GP | 1.11 | [0.66, 1.86]
PR | 0.42 | [0.11, 1.59]
DM | 1.32 | [0.81, 2.13]
EC-A | 0.75 | [0.37, 1.53]
EC-L | 1.81 | [1.09, 3.00]
DES-L | 2.00 | [1.25, 3.21]
DF | 0.99 | [0.61, 1.62]
ggeffects::ggeffect(m, terms="spp")
spp | Predicted | 95% CI
--------------------------------
GP | 1.14 | [0.69, 1.90]
PR | 0.44 | [0.12, 1.63]
DM | 1.36 | [0.85, 2.18]
EC-A | 0.78 | [0.39, 1.57]
EC-L | 1.87 | [1.13, 3.07]
DES-L | 2.06 | [1.30, 3.28]
DF | 1.02 | [0.63, 1.65]
问题
为什么Ggeffect和Ggemeans对边缘效应产生不同的结果?它只是内部的包装和效果如何计算它们的内部内容吗?另外,有人知道如何在示例中的模型中从头开始计算边际效应的一些资源吗?
Example
library(glmmTMB)
library(ggeffects)
## Zero-inflated negative binomial model
(m <- glmmTMB(count ~ spp + mined + (1|site),
ziformula=~spp + mined,
family=nbinom2,
data=Salamanders,
na.action = "na.fail"))
summary(m)
ggemmeans(m, terms="spp")
spp | Predicted | 95% CI
--------------------------------
GP | 1.11 | [0.66, 1.86]
PR | 0.42 | [0.11, 1.59]
DM | 1.32 | [0.81, 2.13]
EC-A | 0.75 | [0.37, 1.53]
EC-L | 1.81 | [1.09, 3.00]
DES-L | 2.00 | [1.25, 3.21]
DF | 0.99 | [0.61, 1.62]
ggeffects::ggeffect(m, terms="spp")
spp | Predicted | 95% CI
--------------------------------
GP | 1.14 | [0.69, 1.90]
PR | 0.44 | [0.12, 1.63]
DM | 1.36 | [0.85, 2.18]
EC-A | 0.78 | [0.39, 1.57]
EC-L | 1.87 | [1.13, 3.07]
DES-L | 2.06 | [1.30, 3.28]
DF | 1.02 | [0.63, 1.65]
Questions
Why are ggeffect and ggemmeans giving different results for the marginal effects? Is it simply something internal with how the packages emmeans and effects are computing them? Also, does anyone know of some resources on how to compute marginal effects from scratch for a model like that in the example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您拟合一个复杂的模型:具有随机效应的零泄漏的负二项式模型。
您观察到的内容与模型规范无关。让我们通过拟合更简单的模型:仅具有固定效果的泊松来展示这一点。
该文档解释说,内部
ggemmeans()
调用emmeans :: emmeans()
while ggeffect(ggeffect() 。emmeans 和效果计算边缘效应,但它们做出了不同的(默认)选择,如何将其边缘化(即平均上升) 按顺序进行要获得 spp 的效果。
开采是一个具有两个级别的分类变量:“是”和“否”。关键位是两个级别没有平衡:比“是” S多一点“否”。
直观地,这意味着开采的加权平均 [想想(44×Yes + 48×no)/92 ]与开采 [想想(是 +否)/2 ]。
让我们通过指定如何直接调用 emmeans :: emmeans()()时指定如何边缘化挖掘来检查直觉。
第二个选项返回使用
ggeffects :: ggeffect
计算出的边际效果。update
@daniel指出
ggeffects
接受权重
参数,并将其传递给emmeans
。这样,您可以继续使用ggeffects
,并仍然控制预测的平均值以计算边际效果。亲自尝试:
You fit a complex model: zero-inflated negative binomial model with random effects.
What you observe has little to do with the model specification. Let's show this by fitting a simpler model: Poisson with fixed effects only.
The documentation explains that internally
ggemmeans()
callsemmeans::emmeans()
whileggeffect()
callseffects::Effect()
.Both emmeans and effects compute marginal effects but they make a different (default) choice how to marginalize out (ie. average over) mined in order to get the effect of spp.
mined is a categorical variable with two levels: "yes" and "no". The crucial bit is that the two levels are not balanced: there are slightly more "no"s than "yes"s.
Intuitively, this means that the weighted average over mined [think of (44 × yes + 48 × no) / 92] is not the same as the simple average over mined [think of (yes + no) / 2].
Let's check the intuition by specifying how to marginalize out mined when we call
emmeans::emmeans()
directly.The second option returns the marginal effects computed with
ggeffects::ggeffect
.Update
@Daniel points out that
ggeffects
accepts theweights
argument and will pass it toemmeans
. This way you can keep usingggeffects
and still control how predictions are averaged to compute marginal effects.Try it out for yourself with: