Search code examples
rfixedeffectsinteraction

Interact fixed effects using feols from fixest


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)

Solution

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