2016-12-25 96 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

相關問題