Amelia插补后固定效应回归两类报错问题求助
问题解决步骤
错误1:不指定data参数报错的修复
Amelia的with()函数会自动将每个插补后的数据集传入表达式的运行环境,但plm和feols都需要明确的面板结构定义:
- 对于
plm:必须指定index参数定义个体(country)和时间(year)维度,同时设置model = "within"来运行固定效应模型,无需手动指定data(with()已自动传入当前插补数据集)。 - 对于
fixest:用| country语法指定个体固定效应,同样不需要手动指定data参数。
修正后的模型调用示例:
# plm固定效应回归 fits_plm <- with(a.out, plm(gdp_pc ~ infl * trade, index = c("country", "year"), model = "within")) # fixest固定效应回归 fits_fixest <- with(a.out, feols(gdp_pc ~ infl * trade | country))
错误2:mi.combine提示无tidy()方法的修复
mi.combine()依赖模型对象的tidy()方法提取系数信息,需做两点调整:
- 加载
broom包——plm模型的tidy()方法由该包提供; - 禁止在
with()中指定data = a.out,这会把Amelia对象而非单个插补数据集传入模型,导致结构异常。
完整可运行代码:
library(Amelia) library(clarify) library(fixest) library(plm) library(broom) # 提供plm模型的tidy方法 # 加载数据集 data("africa", package = "Amelia") # 多重插补 a.out <- amelia(x = africa, m = 10, cs = "country", ts = "year", logs = "gdp_pc", p2s = 0) # plm固定效应回归+结果合并 fits_plm <- with(a.out, plm(gdp_pc ~ infl * trade, index = c("country", "year"), model = "within")) mi.combine(fits_plm) # fixest固定效应回归+结果合并 fits_fixest <- with(a.out, feols(gdp_pc ~ infl * trade | country)) mi.combine(fits_fixest)
关键注意事项
with(a.out, expr)的运行环境是单个插补数据集,直接引用变量名即可,无需额外指定data;plm必须明确index和model = "within"才能正确拟合固定效应;fixest用| 个体变量的语法指定固定效应是其标准用法;fixest自身内置tidy()方法,无需额外依赖包。
内容的提问来源于stack exchange,提问作者flxflks
相关产品推荐
相关产品推荐

