lrberge / fixest

Fixed-effects estimations
https://lrberge.github.io/fixest/
380 stars 61 forks source link

Error in `feols` with 'subset' and 'fixef' parameters #503

Open MaelAstruc opened 5 months ago

MaelAstruc commented 5 months ago

Hello,

I have encountered a bug while trying some subset analyses.

Here is a reproducible example:

N <- 10000
periods <- 10
indivs <- N/periods
countries <- 100

df <- data.frame(
  id = rep(1:indivs, each = periods),
  year = rep(1:periods, times = indivs),
  country = floor(runif(N, 0, countries + 1))
)

df <- df |>
  dplyr::group_by(id) |>
  dplyr::mutate(indiv_FE = runif(1) * 10) |>
  dplyr::group_by(year) |>
  dplyr::mutate(year_FE = runif(1) * 10) |>
  dplyr::group_by(country) |>
  dplyr::mutate(country_FE = runif(1) * 10) |>
  dplyr::ungroup() |>
  dplyr::mutate(
    e = rnorm(N),
    x = runif(N) + indiv_FE + year_FE + country_FE,
    y = 1 + 2 * x + indiv_FE + year_FE + country_FE + e
  )

fixest::feols(y ~ x, data = df)
fixest::feols(y ~ x, data = df, fixef = c("id", "year", "country"))
fixest::feols(y ~ x, data = df, subset = ~country > 50)
fixest::feols(y ~ x, data = df, subset = df$country > 50)
fixest::feols(y ~ x, data = df, subset = ~country > 50, fixef = c("id", "year", "country"))
fixest::feols(y ~ x, data = df, subset = df$country > 50, fixef = c("id", "year", "country"))

The last two give this error:

# Error in eval(all_vars_call, data) : objet 'id' introuvable
# Erreur dans fixest::feols(y ~ x, data = df, subset = ~country > 50, fixef = c("id",  : 
#   Error in res[[2]] : indice hors limites
# This error was unforeseen by the author of the function feols. If you think your call to the function is legitimate, could you report?