如何在felm()函数之后绘制交互作用的边际效应 [英] How to plot marginal effect of an interaction after felm() function

查看:372
本文介绍了如何在felm()函数之后绘制交互作用的边际效应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基于带有一组单位固定效果的巨型"面板数据进行了回归.因此,我从软件包"lfe"中使用了函数"felm()".另外,回归中有两个连续变量的交互项.但是,当绘制x对y的边际效应如何随x2变化时,似乎"felm()"生成的对象通常与大多数绘制函数(例如"ggplot","interplot()"和"meplot")不兼容.但是我必须使用"felm()",因为我需要控制大量单位固定效果(就像人们在Stata中使用"reghdfe"所做的那样).那么,我该如何解决R中的这个问题呢?随时让我知道一些出路.谢谢!

I ran a regression based on a "giant" panel data with a bunch of unit fixed effects. So I employed function "felm()" from package "lfe". In addition, I have an interaction term of two continuous variables in the regression. But when plotting how the marginal effects of x on y vary with x2, it seems that the objects produced by "felm()" are often incompatible to most plotting functions like "ggplot", "interplot()" and "meplot". But I have to use "felm()" because I need to control for a large amount of unit fixed effects (like people do by "reghdfe" in Stata). So, how could I address this issues in R? Feel free to let me know some ways out. Thanks!

这是一个有关interplot()如何与felm()一起使用的示例:

Here is an example about how interplot() does not work with felm():

# An example data:
library(lfe)
library(ggplot2)
library(interplot)
oldopts <- options(lfe.threads=1)
x <- rnorm(1000)
x2 <- rnorm(length(x))
id <- factor(sample(10,length(x),replace=TRUE))
firm <- factor(sample(3,length(x),replace=TRUE,prob=c(2,1.5,1)))
year <- factor(sample(10,length(x),replace=TRUE,prob=c(2,1.5,rep(1,8))))
id.eff <- rnorm(nlevels(id))
firm.eff <- rnorm(nlevels(firm))
year.eff <- rnorm(nlevels(year))
y <- x + 0.25*x2 + id.eff[id] + firm.eff[firm] +
  year.eff[year] + rnorm(length(x))
mydata <- data.frame(cbind(y, x, x2, id, firm, year))

# Regression using felm():
reg1 <- felm(y ~ x + x2 + x:x2|id+firm+year|0|id, data=mydata)
summary(reg1)

# Using interplot() to plot marginal effects
interplot(m=reg1, var1="x", var2="x2", ci=0.9)

然后出现错误:

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘sim’ for signature ‘"felm"’

我也尝试过meplot(),但仍然无法正常工作:

Also I tried meplot() but it still does not work:

# Using meplot() to plot marginal effects
library(evir)
meplot(model=reg1, var1="x", var2="x2", int="x:x2", vcov=vcov(reg1), data=mydata)

我遇到一个错误:

Error in meplot(model = reg1, var1 = "x", var2 = "x2", int = "x:x2", vcov = vcov(reg1),  : 
  (list) object cannot be coerced to type 'double'

推荐答案

我已经使用ggplot2coef()vcov()来实现我想要的功能,并手动绘制了边际效应.

I have used ggplot2, coef() and vcov() to realize what I want, plotting the marginal effects by hands.

library(ggplot2)
beta.hat <- coef(reg1)
vcov1 <- vcov(reg1)
z0 <- seq(min(x2), max(x2), length.out = 1000)
dy.dx <- beta.hat["x"] + beta.hat["x:x2"]*z0
se.dy.dx <- sqrt(vcov1["x", "x"] + (z0^2)*vcov1["x2", "x2"] + 2*z0*vcov1["x", "x2"])
upr <- dy.dx + 1.96*se.dy.dx
lwr <- dy.dx - 1.96*se.dy.dx
ggplot(data=NULL, aes(x=z0, y=dy.dx)) +
  labs(x="x2", y="Marginal Effects",
       title=paste("Marginal Effects of x on y vary with x2"), 
       cex=4) +
  geom_line(aes(z0, dy.dx),size = 1) +
  geom_line(aes(z0, lwr), size = 1, linetype = 2, color="blue") +
  geom_line(aes(z0, upr), size = 1, linetype = 2, color="blue") +
  geom_hline(yintercept=0, size = 1, linetype=3) +
  geom_ribbon(aes(ymin=lwr, ymax=upr), alpha=0.3)

这篇关于如何在felm()函数之后绘制交互作用的边际效应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆