Reports different R2s for fixest estimations (e.g. feglm or feols).

r2(x, type = "all", full_names = FALSE)

Arguments

x

A fixest object, e.g. obtained with function feglm or feols.

type

A character vector representing the R2 to compute. The R2 codes are of the form: "wapr2" with letters "w" (within), "a" (adjusted) and "p" (pseudo) possibly missing. E.g. to get the regular R2: use type = "r2", the within adjusted R2: use type = "war2", the pseudo R2: use type = "pr2", etc. Use "cor2" for the squared correlation. By default, all R2s are computed.

full_names

Logical scalar, default is FALSE. If TRUE then names of the vector in output will have full names instead of keywords (e.g. Squared Correlation instead of cor2, etc).

Value

Returns a named vector.

Details

For R2s with no theoretical justification, like e.g. regular R2s for maximum likelihood models -- or within R2s for models without fixed-effects, NA is returned. The single measure to possibly compare all kinds of models is the squared correlation between the dependent variable and the expected predictor.

The pseudo-R2 is also returned in the OLS case, it corresponds to the pseudo-R2 of the equivalent GLM model with a Gaussian family.

For the adjusted within-R2s, the adjustment factor is (n - nb_fe) / (n - nb_fe - K) with n the number of observations, nb_fe the number of fixed-effects and K the number of variables.

Author

Laurent Berge

Examples


# Load trade data
data(trade)

# We estimate the effect of distance on trade (with 3 fixed-effects)
est = feols(log(Euros) ~ log(dist_km)|Origin+Destination+Product, trade)

# Squared correlation:
r2(est, "cor2")
#>      cor2 
#> 0.7040186 

# "regular" r2:
r2(est, "r2")
#>        r2 
#> 0.7040186 

# pseudo r2 (equivalent to GLM with Gaussian family)
r2(est, "pr2")
#>       pr2 
#> 0.2353827 

# adjusted within r2
r2(est, "war2")
#>      war2 
#> 0.2182526 

# all four at once
r2(est, c("cor2", "r2", "pr2", "war2"))
#>      cor2        r2       pr2      war2 
#> 0.7040186 0.7040186 0.2353827 0.2182526 

# same with full names instead of codes
r2(est, c("cor2", "r2", "pr2", "war2"), full_names = TRUE)
#> Squared Correlation                  R2           Pseudo R2  Adjusted Within R2 
#>           0.7040186           0.7040186           0.2353827           0.2182526