R

时间:2017-03-15 06:04:52

标签: r linear-regression interaction marginal-effects

我有以下普通最小二乘模型(OLS)交互模型,我想提取离散边际效应(即跨越离散类别的速度和外国构建汽车的重量的平均边际效应),用于三重交互{{1 }}:

weight* speed*foreign

可以看出,模型中有三种相互作用。变量指定如下:

mpg ~ cost + foreign + weight + speed + foreign + cost*foreign + weight*speed + weight*speed*foreign 是一个以市场上所有汽车的平均值为中心的连续变量,负值表示低于平均值的度数,正值表示更昂贵的汽车。

cost是二元变量,1个国外/ 0国内

foreign是一个连续变量

weight是一个因子变量,表示汽车在三个类别中的速度有多快,低 - 中 - 高。低是基线类别(省略类别)。

感兴趣的数量是模型中的三重互动:speed。我使用以下代码来估计模型并提取相关系数和方差协方差矩阵。我想使用Berry等人的以下程序。 (2016)文章:“改进理论测试理论”,使用下面的图表:Marginal Effects Formulation

使用以下代码提取导出边际效应和方差 - 协方差矩阵所需的相关系数:

weight*speed*foreign

最终,我想要一个数据框,其中包含m <- lm(mpg ~ cost + foreign + weight + speed + foreign + cost*foreign + weight*speed + weight*speed*foreign, data=x) beta.hat <- coef(m) cov <- vcov(m) 的不同离散类别weight的平均边际效应。 speed,最终目标是显示离散边际效应的点图。在图像之后,我知道如何导出因子变量(foreign)的两个非省略类别的边际效应。例如,我认为这对于“高速”来说是正确的:

speed

我的主要问题是如何为省略的速度类别推导出重量的平均边际效应?我理解这是由z0 <- seq(0,1,1) #This captures the two-categories of the Z conditioning variable of foreign dy.dx <- beta.hat["weight"] + beta.hat["weight*speed=High"] + beta.hat["weight*foreign*speed=High"]*z0 # Discrete Marginal Effect se.dy.dx <- sqrt(cov["weight", "weight"] + z0^2*cov["weight * speed=High", "weight * speed=High"] + z0^2*cov["foreign * weight", "foreign * weight"] + z0^2*z0^2*cov["foreign * weight * speed=High", "foreign * weight * speed=High"] + 2*z0*cov["weight","weight * speed=High"] + 2*z0*cov["weight","foreign * weight"] + 2*z0*z0*cov["weight","foreign * weight * speed=High"] + 2*z0*z0*cov["weight * speed=High","foreign * weight"] + 2*z0*z0^2*cov["weight * speed=High","foreign * weight * speed=High"] + 2*z0*z0^2*cov["foreign * weight","foreign * weight * speed=High"]) #Compute Standard Errors for MEs of foreign and domestic cars 构成术语捕获的,但在计算标准误差weight*speed时,我是否需要考虑三重相互作用?鉴于Berry等人在第3行中的表格,这是一个适当的解决方案吗?

1 个答案:

答案 0 :(得分:0)

我不确定您使用的是哪个数据集,因此我将使用内置的mtcars数据集提供略微修改的版本。为了获得您想要的数量,我将使用margins

# three variables
## wt # continuous (your `weight` variable)
## vs # 0/1 (your `foreign` variable)
## cyl # categorical (your `speed` variable)

# note simplified formula construction:
m <- lm(mpg ~ wt*vs*cyl, data = mtcars)

library("margins")
(mar <- margins(m, at=list(vs = 0:1, cyl = c(4,6,8))))
Average marginal effects at specified values
## lm(formula = mpg ~ wt * vs * cyl, data = mtcars)
## 
##  at(vs) at(cyl)      wt      vs      cyl
##       0       4  -4.083 -0.0502 -1.18447
##       1       4  -5.721 -0.0502 -0.05289
##       0       6  -3.129  2.2130 -1.18447
##       1       6 -13.139  2.2130 -0.05289
##       0       8  -2.176  4.4761 -1.18447
##       1       8 -20.557  4.4761 -0.05289

上面的wt列显示了连续项与它相互作用的两个变量的水平之间的边际效应。要绘制这个,你有几个选择。一个简单的就是:

plot(mar)

marginsplot

了解结果。另一个可能是采用summary()对象(这只是一个数据框)并绘制你想要的图(base,ggplot等):

summary(mar)
##  factor vs cyl      AME      SE       z      p    lower   upper
##     cyl  0   4  -1.1845  1.5304 -0.7740 0.4389  -4.1839  1.8150
##     cyl  0   6  -1.1845  1.5304 -0.7740 0.4389  -4.1839  1.8150
##     cyl  0   8  -1.1845  1.5304 -0.7740 0.4389  -4.1839  1.8150
##     cyl  1   4  -0.0529  1.4410 -0.0367 0.9707  -2.8772  2.7714
##     cyl  1   6  -0.0529  1.4398 -0.0367 0.9707  -2.8748  2.7690
##     cyl  1   8  -0.0529  1.4405 -0.0367 0.9707  -2.8761  2.7703
##      vs  0   4  -0.0502  5.8858 -0.0085 0.9932 -11.5861 11.4857
##      vs  0   6   2.2130  3.6550  0.6055 0.5449  -4.9508  9.3767
##      vs  0   8   4.4761  5.2348  0.8551 0.3925  -5.7839 14.7361
##      vs  1   4  -0.0502  5.8896 -0.0085 0.9932 -11.5936 11.4932
##      vs  1   6   2.2130  3.6611  0.6045 0.5455  -4.9626  9.3886
##      vs  1   8   4.4761  5.2397  0.8543 0.3930  -5.7935 14.7457
##      wt  0   4  -4.0826  6.3551 -0.6424 0.5206 -16.5383  8.3731
##      wt  0   6  -3.1291  3.3271 -0.9405 0.3470  -9.6502  3.3920
##      wt  0   8  -2.1756  0.9056 -2.4023 0.0163  -3.9507 -0.4006
##      wt  1   4  -5.7210  1.3883 -4.1209 0.0000  -8.4420 -3.0000
##      wt  1   6 -13.1390 12.3771 -1.0616 0.2884 -37.3977 11.1198
##      wt  1   8 -20.5569 24.7939 -0.8291 0.4070 -69.1522 28.0383