fixest 和 marginaleffects 软件包都是最近发布的
更改以提高互操作性。下一个官方 CRAN 发布
将能够做到这一点,但从 2021 年 12 月 8 日起,您可以使用
开发版本。安装:
library(remotes)
install_github("lrberge/fixest")
install_github("vincentarelbundock/marginaleffects")
我建议您先将固定效应变量转换为因子
拟合你的模型:
library(fixest)
library(marginaleffects)
dat <- mtcars
dat$gear <- as.factor(dat$gear)
mod <- feglm(am ~ mpg + mpg^2 + hp + hp^3| gear,
family = binomial(link = "logit"),
data = dat)
然后,您可以使用marginaleffects 和summary 来计算平均值
边际效应:
mfx <- marginaleffects(mod, variables = "mpg")
summary(mfx)
## Average marginal effects
## type Term Effect Std. Error z value Pr(>|z|) 2.5 % 97.5 %
## 1 response mpg 0.3352 40 0.008381 0.99331 -78.06 78.73
##
## Model type: fixest
## Prediction type: response
请注意,计算平均边际效应需要计算
数据集的每一行都有明显的边际效应。这个可以
当您的数据包含数百万
观察。
相反,您可以计算特定值的边际效应
使用 newdata 参数和 typical 函数的回归器。
有关详细信息,请参阅marginaleffects 文档
那些:
marginaleffects(mod,
variables = "mpg",
newdata = typical(mpg = 22, gear = 4))
## rowid type term dydx std.error hp mpg gear predicted
## 1 1 response mpg 1.068844 50.7849 146.6875 22 4 0.4167502