I have been looking for the three types of R-squared of the Fixed Effects model outputs in R as well.
Thanks to the help of @paqmo, I was able to manually calculate and reproduce lfe's full and proj R-squared using the model fit from the standard lm package. That said, I am quite certain that the full R-sq is straightforward, meaning R-sq of all pairs of predicted values and original values. At the same time, their proj R-squared is also identical to the so-called within R-squared (definitions from STATA), which is the default reported R-squared in the plm package.
After reading STATA manual Page 10 briefly, I think the full R-sq in lfe and overall R-sq in STATA are the same idea. I see some people said overall R-sq is a weighted average of within and between R-sq, but I did not see any supporting evidence for this statement. I only see that both overall and full R-sq are directly calculated from the pairs of predicted y and original y.
Below are my own calculations for full and proj R-sq.
fe_lm_mod <- lm(formula = "y ~ x1 + x2 + entity - 1",
data = dataframe)
## Calculate prediction
y_predict <- predict(fe_lm_mod, newdata = dataframe)
y_original <- dataframe$y
# Get the valid values indices
notmiss <- which((!is.na(y_predict)) & (!is.na(y_original)))
# Residiual sum of squares
SSres <- sum((y_original[notmiss] - y_predict[notmiss])**2)
# Calculate full R2
SStot_full <- sum((y_original[notmiss] -
mean(y_original[notmiss]))**2)
### get the demean. The within finds the total sum of
### squares on the demeaned outcome variable.
### References
# https://stats.stackexchange.com/questions/262246/difference-of-r2-between-ols-with-individual-dummies-to-panel-fixed-effect-mo
demeaned_y <- y_original[notmiss] -
tapply(y_original[notmiss], dataframe$entity[notmiss],
mean)[dataframe$entity][notmiss]
# Calculate within R2
SStot_within <- sum((demeaned_y-mean(demeaned_y))^2)
print(paste("calculated full R2", 1 - SSres/SStot_full))
print(paste("calculated within R2", 1 - SSres/SStot_within))
For between R-sq, I think the plm package with model="between" may produce between R-sq, but I am not very sure. One can try to calculate it based on the STATA manual, like what I did for full and within R-sq.
So far I made a summary for the R-sq outputs (to be continued):
- lm R-sq: not good for Fixed Effects model, cannot reproduce
- lfe "full" R-sq: R-sq for all pairs predicted y and original y, may also be called as "overall" R-sq
- lfe "proj" R-sq: "within" R-sq: how much of the variation in the dependent variable within each entity group is captured by the model
- plm model="within" R-sq: same as 3.
- plm model="between" R-sq: "between" R-sq: how much of the variation in the dependent variable between each entity group is captured by the model
- plm model="pooling" R-sq: not good for Fixed Effects model. This is the standard OLS R-sq. It is not a Fixed Effects model R-sq.