Simply put, I'd like to know how the plm package in R calculates the residuals of a random-effect regression.
I ask this because i'm getting some "weird" outputs. Let-me reproduce them here using the Grunfeld data for four firms, like Gujarati in his Basic Econometrics do:
require(plm)
require(foreign)
Grunfeld<-read.dta("Data.dta")
Grunfeld<-pdata.frame(Grunfeld,index = c("id","t"))
grun.re <- plm(Y~X2+X3,data=Grunfeld,model="random",index="id")
#Means by id
X2M<-tapply(Grunfeld$X2,Grunfeld$id,FUN = mean)
X3M<-tapply(Grunfeld$X3,Grunfeld$id,FUN = mean)
YM<-tapply(Grunfeld$Y,Grunfeld$id,FUN = mean)
#Random Effect: Fit the model and the calculate residuals "by hand"
fit.re<-grun.re$coefficients[1]+grun.re$coefficients[2]*Grunfeld$X2+grun.re$coefficients[3]*Grunfeld$X3
calcResid.re<-(Grunfeld$Y-fit.re)
#Random Effect:
head(cbind(grun.re$residuals,Grunfeld[,11:13],calcResid.re))
grun.re$residuals alphaRE eRE uRE calcResid.re
1 99.395803 -169.9282 116.23154 -53.69666 -53.69666
2 18.023715 -169.9282 34.85946 -135.06874 -135.06874
3 -39.256625 -169.9282 -22.42089 -192.34909 -192.34908
4 -2.857048 -169.9282 13.97869 -155.94951 -155.94951
5 -28.334107 -169.9282 -11.49837 -181.42656 -181.42656
6 6.475226 -169.9282 23.31096 -146.61723 -146.61723
In this table, uRE is the overall residual of the regression provided by Stata (which is identical to Gretl's) and calcResid.re is the manually calculated residuals from the fitted model. So, Stata, Gretl and I did the same. But what plm package do?
We can se that calcResid.re and uRE are equals. But the residuals provided by the plm estimation (grun.re$residuals) completely differs.
Here is a link to the dataset and results: https://github.com/rrremedio/shared_folder/blob/master/Data.dta
alphaRE, eRE, uRE
from Stata or gretl are. Do the coefficents match? Stata uses a slightly different RE estimator as far as I recall, plm usesrandom.method="swar"
as default. Note also, that you probably wanteffect="individual"
instead ofindex="id"
in the estimation. $\endgroup$ – Helix123 Dec 10 '15 at 18:16