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

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

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

最佳答案

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

https://stackoverflow.com/questions/41318646/

相关文章:

C - do/while 循环和 switch(int)。输入 char 值会导致 inf。环形

.net - Android Xamarin - 带有 ObservableCollection 的

android - 安装失败 INSTALL_PARSE_FAILED_MANIFEST_MALFO

elixir - 编译 riak_core 项目时,Mix 在 deps.compile 上挂起

angularjs - 在 Controller 函数之外的脚本中读取 AngularJS 表达式

security - Webpack 样式加载器 appendChild 对 CSP 不友好

python - 在 Tkinter 中创建向导

apache-spark - Spark 溢出独立于分配的执行程序内存

angularjs - 在带有过滤的ng-repeat中反向计数

wpf - 在其 winforms 父级中拖动托管 WPF 控件吞噬的事件