How to get the corr(u_i, Xb) for panel data fixed effects regression in R

912 views Asked by At

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!

1

There are 1 answers

1
Edu On BEST ANSWER

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