使用FixEst :: Feols和Modelsummary的几种固定效果模型的R2

发布于 2025-02-12 17:37:06 字数 7212 浏览 4 评论 0原文

我有一个数据集,

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))

我想通过累积的逐步添加控件来估算固定效果回归,并在modelsummary表中显示了结果。

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"))

不幸的是,这并不能显示每个相应模型的R2。 我知道我可以使用单个模型来计算R2,

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

但我希望它将其包含在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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深缘浅 2025-02-19 17:37:06

我相信此错误是在上游软件包中修复的。如果您安装了最新的开发版本,则应该能够自动获得R2:

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

完全重新启动R,然后::

library(fixest)
library(modelsummary)

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

modelsummary(mod, output = "markdown")
RHS:HPRHS:WT RHS:Drat
HP-0.024
(0.015)
wt-3.206
(1.188)
Drat1.793
(1.564)
num.Obs。323232
R20.7540.8370.745
R2 adj。0.7270.8200.718
R2在0.0800.3920.048
r2 in adj内。0.0470.3710.014
AIC163.9150.6165.0
BIC166.8166.8153.6 167.9
RMSE2.94 2.942.392.99
STD.ERRORSby by:cyl by:cyl by:cylby:cylfe
x x x x x x xx x x x x x x x x x x x:cyl x x x x x x x x x x x x x x xx x x x x x x

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: hprhs: wtrhs: drat
hp-0.024
(0.015)
wt-3.206
(1.188)
drat1.793
(1.564)
Num.Obs.323232
R20.7540.8370.745
R2 Adj.0.7270.8200.718
R2 Within0.0800.3920.048
R2 Within Adj.0.0470.3710.014
AIC163.9150.6165.0
BIC166.8153.6167.9
RMSE2.942.392.99
Std.Errorsby: cylby: cylby: cyl
FE: cylXXX
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文