使用FIXEST的FEOL进行固定效果的固定效果

发布于 2025-02-12 02:42:27 字数 6360 浏览 2 评论 0 原文

我想采用一个固定效果模型,其中包括国家和年份固定效果以及国家与年份之间的互动效果。我尝试了以下方法:

library(fixest)
feols(y ~ x | country*year, data)
feols(y ~ x | country + year + country:year, data)
feols(y ~ x | country + year + I(country*year), data)

但是全部产生了错误:

Error in feols(y ~ x | country*: Error in res[[2]] : subscript out of bounds This error was unforeseen by the author of the function feols. If you think your call to the function is legitimate, could you report?

我该如何正确处理?

以下是我实际数据的前60行:

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", "Garissa", 
"Isiolo", "Kajiado", "Kakamega", "Kericho", "Kiambu", "Kilifi", 
"Kirinyaga", "Kisumu", "Kitui"), 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", "Kenya", 
"Kenya", "Kenya", "Kenya", "Kenya", "Kenya", "Kenya", "Kenya", 
"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, 
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, 0, 0, 29874625.2288818, 
    0, 0, 0, 0, 0, 3119730.26764258, 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, 38.1211875, 46.1263392857143, 38.1707857142857, 
    30.9757380952381, 34.6969267857143, 46.7413571428571, 31.9214107142857, 
    41.9071845238095, 29.2133482142857, 41.2855535714286)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -60L), 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, 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", "Kenya", "Kenya", "Kenya", "Kenya", "Kenya", "Kenya", 
    "Kenya", "Kenya", "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", "Garissa", "Isiolo", 
    "Kajiado", "Kakamega", "Kericho", "Kiambu", "Kilifi", "Kirinyaga", 
    "Kisumu", "Kitui"), .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, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -60L), .drop = TRUE))

我的回归没有国家和年份固定效果之间的相互作用

feols(sngq ~ yearvalue | country + year, df)

I would like to employ a fixed effects model that includes country and year fixed effects as well as fixed effects for the interaction between country and year. I tried the following approaches:

library(fixest)
feols(y ~ x | country*year, data)
feols(y ~ x | country + year + country:year, data)
feols(y ~ x | country + year + I(country*year), data)

but all yield the error:

Error in feols(y ~ x | country*: Error in res[[2]] : subscript out of bounds This error was unforeseen by the author of the function feols. If you think your call to the function is legitimate, could you report?

How would I approach this correctly?

The following are the first 60 rows of my actual data:

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", "Garissa", 
"Isiolo", "Kajiado", "Kakamega", "Kericho", "Kiambu", "Kilifi", 
"Kirinyaga", "Kisumu", "Kitui"), 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", "Kenya", 
"Kenya", "Kenya", "Kenya", "Kenya", "Kenya", "Kenya", "Kenya", 
"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, 
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, 0, 0, 29874625.2288818, 
    0, 0, 0, 0, 0, 3119730.26764258, 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, 38.1211875, 46.1263392857143, 38.1707857142857, 
    30.9757380952381, 34.6969267857143, 46.7413571428571, 31.9214107142857, 
    41.9071845238095, 29.2133482142857, 41.2855535714286)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -60L), 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, 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", "Kenya", "Kenya", "Kenya", "Kenya", "Kenya", "Kenya", 
    "Kenya", "Kenya", "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", "Garissa", "Isiolo", 
    "Kajiado", "Kakamega", "Kericho", "Kiambu", "Kilifi", "Kirinyaga", 
    "Kisumu", "Kitui"), .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, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -60L), .drop = TRUE))

My regression without the interaction between country and year fixed effects is

feols(sngq ~ yearvalue | country + year, df)

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

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

发布评论

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

评论(1

素年丶 2025-02-19 02:42:28

您应该查看所需的特定语法,如文档在这里 。也许您希望这样:

library(fixest)
#> Warning: package 'fixest' was built under R version 4.1.2
feols(sngq ~ yearvalue | country^year, data)
#> OLS estimation, Dep. Var.: sngq
#> Observations: 60 
#> Fixed-effects: country^year: 5
#> Standard-errors: Clustered (country^year) 
#>            Estimate Std. Error       t value  Pr(>|t|)    
#> yearvalue -2.26e-08   1.55e-23 -1.453754e+15 < 2.2e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> RMSE: 5.38506     Adj. R2: 0.375872
#>                 Within R2: 2.377e-4
feols(sngq ~ yearvalue | country + year + c(country,year), data)
#> OLS estimation, Dep. Var.: sngq
#> Observations: 60 
#> Fixed-effects: country: 5,  year: 1,  c(country, year): 6
#> Standard-errors: Clustered (country) 
#>            Estimate Std. Error      t value  Pr(>|t|)    
#> yearvalue -2.26e-08   2.17e-23 -1.04226e+15 < 2.2e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> RMSE: 5.38506     Adj. R2: 0.312185
#>                 Within R2: 2.377e-4
feols(sngq ~ yearvalue | country + year + I(c(country,year)), data)
#> OLS estimation, Dep. Var.: sngq
#> Observations: 60 
#> Fixed-effects: country: 5,  year: 1,  I(c(country, year)): 6
#> Standard-errors: Clustered (country) 
#>            Estimate Std. Error      t value  Pr(>|t|)    
#> yearvalue -2.26e-08   2.17e-23 -1.04226e+15 < 2.2e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> RMSE: 5.38506     Adj. R2: 0.312185
#>                 Within R2: 2.377e-4

You should have a look at the specific syntax you want as described in the documentation here. Maybe you want it like this:

library(fixest)
#> Warning: package 'fixest' was built under R version 4.1.2
feols(sngq ~ yearvalue | country^year, data)
#> OLS estimation, Dep. Var.: sngq
#> Observations: 60 
#> Fixed-effects: country^year: 5
#> Standard-errors: Clustered (country^year) 
#>            Estimate Std. Error       t value  Pr(>|t|)    
#> yearvalue -2.26e-08   1.55e-23 -1.453754e+15 < 2.2e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> RMSE: 5.38506     Adj. R2: 0.375872
#>                 Within R2: 2.377e-4
feols(sngq ~ yearvalue | country + year + c(country,year), data)
#> OLS estimation, Dep. Var.: sngq
#> Observations: 60 
#> Fixed-effects: country: 5,  year: 1,  c(country, year): 6
#> Standard-errors: Clustered (country) 
#>            Estimate Std. Error      t value  Pr(>|t|)    
#> yearvalue -2.26e-08   2.17e-23 -1.04226e+15 < 2.2e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> RMSE: 5.38506     Adj. R2: 0.312185
#>                 Within R2: 2.377e-4
feols(sngq ~ yearvalue | country + year + I(c(country,year)), data)
#> OLS estimation, Dep. Var.: sngq
#> Observations: 60 
#> Fixed-effects: country: 5,  year: 1,  I(c(country, year)): 6
#> Standard-errors: Clustered (country) 
#>            Estimate Std. Error      t value  Pr(>|t|)    
#> yearvalue -2.26e-08   2.17e-23 -1.04226e+15 < 2.2e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> RMSE: 5.38506     Adj. R2: 0.312185
#>                 Within R2: 2.377e-4

Created on 2022-07-01 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文