如何在 R 中获取面板数据固定效应回归的 corr(u_i, Xb)

How to get the corr(u_i, Xb) for panel data fixed effects regression in R

我正在尝试使用 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)

编辑:我首先在交叉验证时问了这个问题,我得到了这个回复-"Questions that are solely about programming or carrying out an operation within a statistical package are off-topic for this site and may be closed."因为我的问题更多地与包中的操作有关,所以我想这是正确的地方!

考虑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