Anton
Anton

Reputation: 254

R2 for several fixed effects models using fixest::feols and modelsummary

I have a dataset

structure(list(NAME_1.y = c("Alibori", "Atakora", "Atlantique", 
"Borgou", "Collines", "Donga", "Kouffo", "Littoral", "Mono", 
"Oueme", "Plateau", "Zou", "Central", "Chobe", "Francistown", 
"Gaborone", "Ghanzi", "Jwaneng", "Kgalagadi", "Kgatleng", "Kweneng", 
"Lobatse", "North-East", "Selibe Phikwe", "South-East", "Southern", 
"Mosteiros", "Paúl", "Porto Novo", "Praia", "Ribeira Grande", 
"Santa Catarina", "Santa Cruz", "Sao Domingos", "Sao Filipe", 
"Sao Miguel", "Sao Vicente", "Tarrafal", "Ashanti", "Brong Ahafo", 
"Central", "Eastern", "Greater Accra", "Northern", "Upper East", 
"Upper West", "Volta", "Western", "Bomet", "Bungoma"), country = c("Benin", 
"Benin", "Benin", "Benin", "Benin", "Benin", "Benin", "Benin", 
"Benin", "Benin", "Benin", "Benin", "Botswana", "Botswana", "Botswana", 
"Botswana", "Botswana", "Botswana", "Botswana", "Botswana", "Botswana", 
"Botswana", "Botswana", "Botswana", "Botswana", "Botswana", "Cape Verde", 
"Cape Verde", "Cape Verde", "Cape Verde", "Cape Verde", "Cape Verde", 
"Cape Verde", "Cape Verde", "Cape Verde", "Cape Verde", "Cape Verde", 
"Cape Verde", "Ghana", "Ghana", "Ghana", "Ghana", "Ghana", "Ghana", 
"Ghana", "Ghana", "Ghana", "Ghana", "Kenya", "Kenya"), year = c(2005, 
2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 
2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 
2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 
2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 
2005, 2005, 2005, 2005, 2005), yearvalue = c(0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0), sngq = c(40.3350714285714, 41.8501465773809, 32.1462559523809, 
53.3881378348214, 51.3143125, 45.3290479910714, 30.9222321428571, 
31.8178055555556, 29.6696517857143, 32.6099506302521, 30.4012254464286, 
31.2331401098901, 48.3522208850932, 46.2751339285714, 51.8882346938775, 
49.0520504201681, 49.9850803571429, 49.7569285714286, 48.6707276785714, 
55.5698392857143, 49.1367147108843, 49.6709583333333, 47.7482008928571, 
47.4158928571429, 47.1282232142857, 47.5937851190476, 45.5919585253456, 
39.4586860119048, 34.0096227106227, 37.4234026227679, 35.2635037202381, 
36.663, 39.3566339285714, 39.9424339285714, 38.4452915543576, 
41.0157392857143, 46.5320122818358, 45.8783482142857, 45.1649102484472, 
55.3813660714286, 40.4874038461538, 43.992073015873, 39.0866451990632, 
54.0252091836735, 48.2392232142857, 45.3506232142857, 43.0489608516484, 
36.9757994047619, 38.9841964285714, 36.4050892857143), fac = c(0.365909090909091, 
0.39375, 0.609722222222222, 0.525, 0.664583333333333, 0.546875, 
0.322727272727273, 0.574603174603175, 0.5, 0.633823529411765, 
0.5, 0.428846153846154, 0.344836956521739, 0.3, 0.246428571428571, 
0.169117647058824, 0.733333333333333, 1, 0.7125, 0.428571428571429, 
0.35952380952381, 0.0833333333333333, 0.4, 0, 0.24, 0.381666666666667, 
0.341935483870968, 0.466666666666667, 0.302564102564103, 0.1890625, 
0.35, 0.48, 0.354545454545455, 0.48, 0.323270440251572, 0.35, 
0.323076923076923, 0.233333333333333, 0.508188405797101, 0.641071428571429, 
0.686057692307692, 0.759012345679012, 0.460326086956522, 0.475, 
0.476190476190476, 0.50125, 0.459615384615385, 0.65875, 0.1, 
0.6), svc = c(0.0909090909090909, 0.0833333333333333, 0.44212962962963, 
0.270833333333333, 0.25, 0.166666666666667, 0.299242424242424, 
0.682539682539683, 0.333333333333333, 0.436274509803922, 0.333333333333333, 
0.384615384615385, 0.560688405797101, 0.833333333333333, 0.973214285714286, 
0.742647058823529, 0.555555555555556, 0.666666666666667, 0.666666666666667, 
0.797619047619048, 0.507936507936508, 0.666666666666667, 0.666666666666667, 
0.833333333333333, 0.933333333333333, 0.561111111111111, 0.301075268817204, 
0.833333333333333, 0.534188034188034, 0.657552083333333, 0.638888888888889, 
0.355555555555556, 0.424242424242424, 0.466666666666667, 0.30188679245283, 
0.333333333333333, 0.933634992458522, 0.666666666666667, 0.515942028985507, 
0.523809523809524, 0.756410256410256, 0.511111111111111, 0.947463768115942, 
0.37797619047619, 0.541666666666667, 0.391666666666667, 0.5, 
0.733333333333333, 0.166666666666667, 0.191666666666667)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -50L), groups = structure(list(
    year = c(2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 
    2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 
    2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 
    2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 
    2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 
    2005, 2005), country = c("Benin", "Benin", "Benin", "Benin", 
    "Benin", "Benin", "Benin", "Benin", "Benin", "Benin", "Benin", 
    "Benin", "Botswana", "Botswana", "Botswana", "Botswana", 
    "Botswana", "Botswana", "Botswana", "Botswana", "Botswana", 
    "Botswana", "Botswana", "Botswana", "Botswana", "Botswana", 
    "Cape Verde", "Cape Verde", "Cape Verde", "Cape Verde", "Cape Verde", 
    "Cape Verde", "Cape Verde", "Cape Verde", "Cape Verde", "Cape Verde", 
    "Cape Verde", "Cape Verde", "Ghana", "Ghana", "Ghana", "Ghana", 
    "Ghana", "Ghana", "Ghana", "Ghana", "Ghana", "Ghana", "Kenya", 
    "Kenya"), NAME_1.y = c("Alibori", "Atakora", "Atlantique", 
    "Borgou", "Collines", "Donga", "Kouffo", "Littoral", "Mono", 
    "Oueme", "Plateau", "Zou", "Central", "Chobe", "Francistown", 
    "Gaborone", "Ghanzi", "Jwaneng", "Kgalagadi", "Kgatleng", 
    "Kweneng", "Lobatse", "North-East", "Selibe Phikwe", "South-East", 
    "Southern", "Mosteiros", "Paúl", "Porto Novo", "Praia", 
    "Ribeira Grande", "Santa Catarina", "Santa Cruz", "Sao Domingos", 
    "Sao Filipe", "Sao Miguel", "Sao Vicente", "Tarrafal", "Ashanti", 
    "Brong Ahafo", "Central", "Eastern", "Greater Accra", "Northern", 
    "Upper East", "Upper West", "Volta", "Western", "Bomet", 
    "Bungoma"), .rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 
        7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
        19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 
        30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 
        41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -50L), .drop = TRUE))

I want to estimate a fixed effects regression with cumulative stepwise addition of controls and have the results show in a modelsummary table.

library(fixest)

model <- feols(sngq ~ csw(yearvalue, fac, svc) | country + year + country^year, df) 

names(model) <- c("Model 1", "Model 2", "Model 3", "Model 4")

modelsummary(model, coef_omit = "Int", stars = c("***" = 0.01, "**" = 0.05, "*" = 0.1), gof_omit = "IC$|Lik.|RMSE|FE", coef_rename = c("yearvalue" = "Extraction Value", "fac" = "Nearby Facilities", "svc" = "Available Services"))

Unfortunately, this does not show the R2 for each respective model. I know I can calculate the R2 for a single model using

r2(feols(sngq ~ yearvalue + fac + svc | country + year + country^year, df), "r2")

but I would like it included in the modelsummary table for multiple models as a normal goodness of fit item, is there a way to achieve this?

Upvotes: 1

Views: 687

Answers (1)

Vincent
Vincent

Reputation: 17725

I believe that this bug was fixed in upstream packages. If you install the latest development versions you should be able to get R2 automatically:

library(remotes)
install_github("lrberge/fixest")
install_github("easystats/performance")
install_github("vincentarelbundock/modelsummary")

Restart R completely, then:

library(fixest)
library(modelsummary)

mod <- feols(mpg ~ sw(hp, wt, drat) | cyl, data = mtcars)

modelsummary(mod, output = "markdown")
rhs: hp rhs: wt rhs: drat
hp -0.024
(0.015)
wt -3.206
(1.188)
drat 1.793
(1.564)
Num.Obs. 32 32 32
R2 0.754 0.837 0.745
R2 Adj. 0.727 0.820 0.718
R2 Within 0.080 0.392 0.048
R2 Within Adj. 0.047 0.371 0.014
AIC 163.9 150.6 165.0
BIC 166.8 153.6 167.9
RMSE 2.94 2.39 2.99
Std.Errors by: cyl by: cyl by: cyl
FE: cyl X X X

Upvotes: 3

Related Questions