gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:18:56 31 4
gpt4 key购买 nike

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

关于r - 如何在 R 中获取面板数据固定效应回归的 corr(u_i, Xb),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41318646/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com