3

I am trying to develop a fixed effect regression model for a panel data using the plm package in R. I want to get the correlation between fixed effects and the regressors. Something like the corr(u_i, Xb) that comes in the Stata output. How to get it in R? I have tried the following (using the in-built dataset in the plm package):-

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)

EDIT: I asked this question on cross validated first and I got this reply- "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." Since my question has more to do with a operation in a package, so I guess this is the right place!

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
brock
  • 181
  • 2
  • 10

1 Answers1

2

How about the following considering functions of 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
Edu
  • 903
  • 6
  • 17
  • Yes, this is close.... after the residuals command is run we get the "fitted_by_hand" vector, we can get a correlation between fix and fitted values. The corr(u_i,xb) in Stata basically finds the correlation between the fixed effects and the fitted values. For random effects, it is assumed to be zero. – brock Dec 28 '16 at 09:47