2016-12-25 755 views
2

我正在尝试使用R中的plm包开发面板数据的固定效应回归模型。我想获得固定效果和回归者。类似于Stata输出中的corr(u_i,Xb)。 如何获得它在R? 我曾尝试以下(使用内置数据集中在PLM包): -如何获得corr(u_i,Xb)中的面板数据固定效应回归R

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) 

编辑:我问跨这个问题,验证第一,我得到这个reply-“问题,这仅仅是对编程或在统计软件包内进行操作对于本网站来说是无关紧要的,可能会被关闭。“由于我的问题更多的是与包中的操作有关,所以我想这是正确的地方!

+0

什么是预期的结果? –

+0

一个相关值,即-1到1之间 – brock

回答

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 
+0

是的,这是接近....在运行残差命令之后,我们得到“拟合向量”向量,我们可以得到修正和拟合值之间的相关性。 Stata中的corr(u_i,xb)基本上找到了固定效应和拟合值之间的相关性。对于随机效应,它被假定为零。 – brock