lrberge / fixest

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

Crash with fixef.rm = "singleton" #486

Open marco-t3 opened 2 months ago

marco-t3 commented 2 months ago

Hi, I've found a small bug that (still?) causes R to crash with version 0.11.2 when using the argument fixef.rm = "singleton" with two LHS variables.

Here's a working example:

library(fixest)

set.seed(123)
data <- data.frame(x1 = rnorm(100),
                   y1 = rnorm(100),
                   y2 = c(NA, rnorm(99)),
                   id = rep(1:50, 2))

#Works
feols(y1 ~ x1 | id,
      vcov = ~ id,
      panel.id = ~ id,
      data = data,
      fixef.rm = "singleton")
#Works
feols(y2 ~ x1 | id,
      vcov = ~ id,
      panel.id = ~ id,
      data = data,
      fixef.rm = "singleton")
#Works
feols(c(y1, y2) ~ x1 | id,
      vcov = ~ id,
      panel.id = ~ id,
      data = data)
#Crashes
feols(c(y1, y2) ~ x1 | id,
      vcov = ~ id,
      panel.id = ~ id,
      data = data,
      fixef.rm = "singleton")

The code above causes a crash with the latest version of fixest and throws an error code with an older version.

Aside from this small issue, the package is being a really great tool to work with, thank you :)

etiennebacher commented 1 month ago

With the current github version, it no longer crashes (but still errors):

library(fixest)

set.seed(123)
data <- data.frame(x1 = rnorm(100),
                   y1 = rnorm(100),
                   y2 = c(NA, rnorm(99)),
                   id = rep(1:50, 2))

#Works
feols(y1 ~ x1 | id,
      vcov = ~ id,
      panel.id = ~ id,
      data = data,
      fixef.rm = "singleton")
#> OLS estimation, Dep. Var.: y1
#> Observations: 100
#> Fixed-effects: id: 50
#> Standard-errors: Clustered (id) 
#>    Estimate Std. Error  t value Pr(>|t|) 
#> x1 0.099256   0.135875 0.730491  0.46857 
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> RMSE: 0.68763     Adj. R2: -0.031984
#>                 Within R2:  0.008854
#Works
feols(y2 ~ x1 | id,
      vcov = ~ id,
      panel.id = ~ id,
      data = data,
      fixef.rm = "singleton")
#> NOTES: 1 observation removed because of NA values (LHS: 1).
#>        1 fixed-effect singleton was removed (1 observation, breakup: 1).
#> OLS estimation, Dep. Var.: y2
#> Observations: 98
#> Fixed-effects: id: 49
#> Standard-errors: Clustered (id) 
#>    Estimate Std. Error  t value Pr(>|t|) 
#> x1  0.11272   0.140264 0.803628  0.42557 
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> RMSE: 0.691846     Adj. R2: -0.078502
#>                  Within R2:  0.011393
#Works
feols(c(y1, y2) ~ x1 | id,
      vcov = ~ id,
      panel.id = ~ id,
      data = data)
#> Standard-errors: Clustered (id) 
#> Dep. var.: y1
#>    Estimate Std. Error  t value Pr(>|t|) 
#> x1 0.099256   0.135875 0.730491  0.46857 
#> ---
#> Dep. var.: y2
#>    Estimate Std. Error  t value Pr(>|t|) 
#> x1  0.11272   0.140227 0.803838  0.42537
#Crashes
feols(c(y1, y2) ~ x1 | id,
      vcov = ~ id,
      panel.id = ~ id,
      data = data,
      fixef.rm = "singleton")
#> Error in eval(expr, envir, enclos): Index out of bounds: [index=98; extent=98].