【问题标题】:Marginal effects plot of PLMPLM 的边际效应图
【发布时间】:2022-08-20 21:37:33
【问题描述】:

我使用 plm-package 在 R 中运行了一个单独的固定效果面板模型。我现在想绘制边际效应。 但是,plot_model() 和 effect_plot() 都不适用于 plm 对象。 plot_model() 适用于 type = “est” 但不适用于 type = “pred”。 到目前为止,我的在线搜索只建议使用 ggplot(但它只显示 OLS 回归,而不是固定效果)或过时的函数(即 sjp.lm())

有人对我如何可视化 plm 对象的效果有任何建议吗?

IFE_Aut_uc <- plm(LoC_Authorities_rec ~ Compassion_rec, index = c(\"id\",\"wave\"), model = \"within\", effect = \"individual\", data = D3_long2)
summary(IFE_Aut_uc)

plot_model(IFE_Aut_uc, type = \"pred”)

Error in data.frame(..., check.names = FALSE) : 
  arguments imply differing number of rows: 50238, 82308

和:

effect_plot(IFE_Pol_uc, pred = Compassion_rec)

Error in `stop_wrap()`:
! ~does not appear to be a one- or two-sided formula.
LoC_Politicians_recdoes not appear to be a one- or two-sided formula.
Compassion_recdoes not appear to be a one- or two-sided formula.
Backtrace:
 1. jtools::effect_plot(IFE_Pol_uc, pred = Compassion_rec)
 2. jtools::get_data(model, warn = FALSE)
 4. jtools:::get_lhs(formula)
  • 您能否分享一些您尝试过的代码和使用dput 的数据?

标签: r plot plm marginal-effects


【解决方案1】:

编辑 2022-08-20:CRAN 上的最新版本 plm 现在包括用于模型内部的 predict() 方法。原则上,下面使用fixest 说明的命令现在也应该与plm 一起使用。

以我的经验,plm 模型处理起来有点棘手,许多专门从事“后处理”的包都无法正确处理这些对象。

一种替代方法是使用the fixest package 估计您的“内部”模型,并使用the marginaleffects package. 绘制结果(免责声明:我是marginaleffects 的作者。)

请注意,plm 估计的许多模型都得到了marginaleffects 的官方支持和测试(例如,随机效应、Amemiya、Swaymy-Arora)。但是,这种特定的“内部”模型并非如此,它比其他模型更难支持。

首先,我们估计两个模型以表明plmfixest 版本是等效的:

library(plm)
library(fixest)
library(marginaleffects)
library(modelsummary)
data("EmplUK")

mod1 <- plm(
    emp ~ wage * capital,
    index = c("firm", "year"),
    model = "within",
    effect = "individual",
    data = EmplUK)

mod2 <- feols(
    emp ~ wage * capital | firm,
    se = "standard",
    data = EmplUK)

models <- list("PLM" = mod1, "FIXEST" = mod2)

modelsummary(models)
PLM FIXEST
wage 0.000 0.000
(0.034) (0.034)
capital 2.014 2.014
(0.126) (0.126)
wage × capital -0.043 -0.043
(0.004) (0.004)
Num.Obs. 1031 1031
R2 0.263 0.986
R2 Adj. 0.145 0.984
R2 Within 0.263
R2 Within Adj. 0.260
AIC 4253.9 4253.9
BIC 4273.7 4273.7
RMSE 1.90 1.90
Std.Errors IID
FE: firm X

现在,我们使用marginaleffects 包来绘制结果。有两个主要功能:

  1. plot_cap():绘制条件调整预测。作为协变量的函数,我的预测结果如何变化?
  2. plot_cme():绘制条件边际效应。如何我的模型相对于一个变量(即导数或“边际效应”)相对于另一个变量的变化?

    有关定义和详细信息,请参见网站:https://vincentarelbundock.github.io/marginaleffects/

    plot_cap(mod2, condition = "capital")
    

    
    plot_cme(mod2, effect = "wage", condition = "capital")
    

【讨论】:

  • 当相同的模型支持 fixst 时,为什么很难在模型内支持 plm?模型对象中缺少什么?
  • IIRC,plm 包提供的predict() 方法要么根本不起作用,要么不支持对新数据集进行预测(newdata 参数)。 fixest 制作了非常不同的对象,即使它是相同的底层统计模型,所以一个 predict 方法不适用于另一种。
  • 这对于绘制模型非常有帮助!您对我如何编辑绘图有任何指导(例如,更改轴标签、y 轴范围、添加标题?)
  • 也许我们都是对的:就我而言,对于当前 CRAN 版本 0.6.0 of marginaleffects 的 fixst 模型是不可重现的,而它是当前开发版本 0.6.0.9000 (e10acc2) 的(我刚试过这个) )
  • 不用担心!顺便说一句:plm 的 2.6-2 版,包括。 predict 功能几天前就在 CRAN 上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-08
  • 2021-07-03
  • 1970-01-01
  • 2020-05-07
  • 1970-01-01
  • 2020-04-19
  • 1970-01-01
相关资源
最近更新 更多