估计三重相互作用的边际效应
我有一个具有三重互动的模型,类似于以下方式:
m1 <- lm(mpg ~ am*cyl*hp, mtcars)
我试图根据am
的效果如何根据cyl
和hp <
am
更改。 /代码>。使用效果()
函数从效果
库中,我可以显示mpg
的预测值。与我的数据集相当快,可以很好地工作。但是,我想显示每种情况下点之间差距的大小。
library(effects)
p1 <- as.data.frame(Effect(m1, focal.predictors = c("am", "cyl", "hp"), xlevels = list(am=c(0, 1), cyl=c(4,8), hp = c(100, 200))))
library(ggplot2)
ggplot(p1, aes(cyl, fit, color = as.factor(am))) +
geom_point(position = position_dodge(0.5)) +
geom_errorbar(aes(ymin=lower, ymax=upper), width = 0, position = position_dodge(0.5)) +
facet_grid(~hp)
我尝试使用margins()
函数从margins
库中使用。如下所示。这显示了平均边缘效应(AME),我想这是我要显示的。但是,我的数据集需要大量的时间,因为我控制了与年份相互作用的国家固定效应和一个自变量之一。
p2 <- margins(m1, at=list(cyl = c(4, 8), hp = c(100, 200)), variables = "am")
p2 <- summary(p2)
ggplot(p2, aes(cyl, AME, color = as.factor(hp))) +
geom_point(position = position_dodge(0.5)) +
geom_errorbar(aes(ymin=lower, ymax=upper), width = 0, position = position_dodge(0.5))
有没有一种方法可以使用feform()
显示预测值之间的估计差距?
I have a model with a triple interaction, similar to this:
m1 <- lm(mpg ~ am*cyl*hp, mtcars)
I am trying to show how the effect of am
changes based on the conditions of cyl
and hp
. Using the Effect()
function from the effects
library, I can show the predicted values of mpg
. This works well and fairly quickly with my dataset. However, I want to show the size of the gap between the points in each case.
library(effects)
p1 <- as.data.frame(Effect(m1, focal.predictors = c("am", "cyl", "hp"), xlevels = list(am=c(0, 1), cyl=c(4,8), hp = c(100, 200))))
library(ggplot2)
ggplot(p1, aes(cyl, fit, color = as.factor(am))) +
geom_point(position = position_dodge(0.5)) +
geom_errorbar(aes(ymin=lower, ymax=upper), width = 0, position = position_dodge(0.5)) +
facet_grid(~hp)
I have tried using the margins()
function from the margins
library. As shown below. This shows the average marginal effect (AME), which I suppose is what I am trying to show. However, it takes an exorbitant amount of time with my dataset because I control for country fixed effects interacted with year and one of the independent variables.
p2 <- margins(m1, at=list(cyl = c(4, 8), hp = c(100, 200)), variables = "am")
p2 <- summary(p2)
ggplot(p2, aes(cyl, AME, color = as.factor(hp))) +
geom_point(position = position_dodge(0.5)) +
geom_errorbar(aes(ymin=lower, ymax=upper), width = 0, position = position_dodge(0.5))
Is there a way I can use Effect()
to show the estimated gap between predicted values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使您想要的绘图的所有相关信息都来自
feform()
函数的输出。首先,让我们运行模型并生成效应对象。现在,要获得
am == 0
和am == 1
之间的区别,我们需要在数据框架中识别这些值p1
。然后,我们可以制作一个矩阵,我们将用来生成效果差异。我们将其初始化为所有零值,并且需要具有
nrow(p1)
行和length(w0)
列:现在,每个列对应于<<<代码> am == 0 和
am == 1
预测hp
和cal
的特定值。对于am == 0
的值,我们需要矩阵具有-1值,对于am == 1
,我们需要它的值1。完成此操作如下:然后,我们可以通过这种方式获得差异:
为了确保我们获得正确的数字,让我们看一下
diffs
的前两个值:您可以看到
diffs
与我们从p1
中计算的相同。现在,我们需要计算差异的差异,我们可以如下这样做:接下来,我们制作一个数据集,使我们能够绘制这些差异。我们将数据保留在
am == 0
的位置,这样我们就没有复制的行进行每个比较。然后,我们增加了差异,标准错误和置信界。然后,我们可以制作情节。现在,每个点代表
am == 0
和am == 1
之间的差异,hp
和cyl < /code>:
在2022-06-01创建的
All the relevant information to make the plot you want is in the output from the
Effect()
function. First, let's run the model and generate the effect object.Now, to get the difference between
am == 0
andam == 1
, we'll need to identify those values in the data framep1
.We can then make a matrix that we will use to generate the differences in effects. We initialize it to have all zero values and it needs to have
nrow(p1)
rows andlength(w0)
columns:Now, each column corresponds to a difference between the
am == 0
andam == 1
prediction for a particular value ofhp
andcal
. For the values wheream == 0
, we need the matrix to have -1 values and foram == 1
, we need it to have values of 1. We can accomplish this as follows:We can then get the differences this way:
Just to make sure we got the right numbers, let's look at the first two values of
diffs
:You can see that the first two values of
diffs
are the same as the ones we would calculate fromp1
. Now, we need to calculate the variance of the differences, we can do this as follows:Next, we make a dataset that will allow us to plot these differences. We keep the data where
am == 0
just so we don't have replicated rows for each comparison. Then, we add in the differences, standard errors and confidence bounds.Then, we can make the plot. Now, each point represents the difference between
am==0
andam==1
for each different value ofhp
andcyl
:Created on 2022-06-01 by the reprex package (v2.0.1)
这是使用
marginaleffects
package 的替代方案。 “继任者”到Margins
,具有更高的灵活性,更受支持的模型类型,并且通常更快地 。 (免责声明:我是作者。)>自定义图
用
ggplot2
:仅数字结果:
Here is an alternative using the
marginaleffects
package, which was designed as a “successor” tomargins
, with more flexibility, more supported model types, and is often much faster. (Disclaimer: I am the author.)Customize the plot with
ggplot2
:Just the numeric results: