【问题标题】:How to get the corr(u_i, Xb) for panel data fixed effects regression in R如何获得 R 中面板数据固定效应回归的 corr(u_i, Xb)
【发布时间】:2017-05-10 04:11:42
【问题描述】:

我正在尝试使用 R 中的 plm 包为面板数据开发一个固定效应回归模型。我想获得固定效应和回归量之间的相关性。类似于 Stata 输出中的 corr(u_i, Xb) 。 如何在 R 中获得它? 我尝试了以下方法(使用 plm 包中的内置数据集):-

data("Grunfeld", package = "plm") 
library(plm)

# build the model 
gi <- plm(inv ~ value + capital, data = Grunfeld, model = "within")

# extract the fixed effects fixef(gi) 
summary(fixef(gi))

fixefs <- fixef(gi)[index(gi, which = "id")] ## get the fixed effects
newdata <- as.data.frame(cbind(fixefs, Grunfeld$value, Grunfeld$capital))  
colnames(newdata) <- c("fixed_effects", "value", "capital") 

cor(newdata)

编辑:我首先在交叉验证时问了这个问题,然后我得到了这个答复——“仅关于编程或在统计包中执行操作的问题对于本网站来说是题外话,可能会被关闭。”由于我的问题更多地与包中的操作有关,所以我想这是正确的地方!

【问题讨论】:

  • 预期结果是什么?
  • 一个相关值,即-1到1之间

标签: r regression stata panel-data


【解决方案1】:

考虑plm的功能如下:

# Run the model       
gi <- plm(inv ~ value + capital, data = Grunfeld, model = "within")

# Get the residuals (res) and fixed effects (fix)
  res = residuals(gi)
  fix = fixef(gi)

  # Aggregate residuals and fixed effects 
  newdata = cbind(res, fix)

  # Correlation

  cor(newdata)
           res        fix
res 1.00000000 0.05171279
fix 0.05171279 1.00000000

【讨论】:

  • 是的,这很接近.... 运行残差命令后,我们得到“fitted_by_hand”向量,我们可以得到修复值和拟合值之间的相关性。 Stata 中的 corr(u_i,xb) 基本上找到了固定效应和拟合值之间的相关性。对于随机效应,假定为零。
猜你喜欢
  • 2019-11-24
  • 2016-09-23
  • 1970-01-01
  • 2021-08-05
  • 1970-01-01
  • 2020-03-20
  • 2018-08-27
  • 2021-07-04
  • 2015-04-22
相关资源
最近更新 更多