lrberge / fixest

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

Update forumula with stepwise function and fixed effects #479

Closed Simon-Schulten closed 3 months ago

Simon-Schulten commented 3 months ago

Hi there, when I try to update a base model that includes stepwise function and fixed effects, I get an error that I can't combine stepwise functions with any other element. I do not quite understand why that is, but this usage does not seem "too far out there" to me.

Reproducible example below:

library(tidyverse)
library(fixest)
set.seed(12345)
n <- 10000
df <- tibble(x1 = runif(n),
             x2 = runif(n),
             x3 = runif(n),
             x4 = runif(n),
             f = rep(c("a","b"), n/2),
             u = rnorm(n)) %>%
  mutate(y1 = 1 + x1 + 2*x2 + 5*x3 + 0.5*x4 + u,
         y2 = 2 + x1 + 2*x2 + 5*x3 + 0.5*x4 + u)

# Define your base model formula
base_model_works <- formula(c(y1, y2) ~ x1 + sw(x2, x3))

base_model_doesnt <- formula(c(y1, y2) ~ x1 + sw(x2, x3) | f)

# Example of running regressions with additional regressors inside the feols call
model_1 <- feols(base_model_works, data = df)

model_2 <- feols(update(base_model_works, . ~ x4 + .), data = df)

model_3 <- feols(update(base_model_doesnt, . ~ x4 + .), data = df)

etable(model_2,
       signif.code = c("***"=0.01, "**"=0.05, "*"=0.10))
lrberge commented 3 months ago

Hi, this is normal. Let's look at the formula you're throwing into the model:

update(base_model_doesnt, . ~ x4 + .)
#> c(y1, y2) ~ x4 + (x1 + sw(x2, x3) | f)

This is simply not valid.

What you want is:

c(y1, y2) ~ x4 + x1 + sw(x2, x3) | f

The update.formula method does not work here, and it is not due to fixest. I'm afraid you have to do differently.

Simon-Schulten commented 3 months ago

Thanks for confirming this is indeed invalid usage. Will find a different way.

lrberge commented 3 months ago

I've added the method update.fixest_multi. You can update the estimation directly and not the formula:

est_1 = feols(c(y1, y2) ~ x1 + sw(x2, x3) | f, df)
est_2 = update(est_1, . ~ x4 + .)
etable(est_2)
#>                           est_2.1          est_2.2           est_2.3          est_2.4
#> Dependent Var.:                y1               y1                y2               y2
#> 
#> x4              0.4712** (0.0061) 0.4788* (0.0313) 0.4712** (0.0061) 0.4788* (0.0313)
#> x1               1.124** (0.0114) 0.9525. (0.0807)  1.124** (0.0114) 0.9525. (0.0807)
#> x2                1.999* (0.0663)                    1.999* (0.0663)
#> x3                                4.942** (0.0291)                   4.942** (0.0291)
#> Fixed-Effects:  ----------------- ---------------- ----------------- ----------------
#> f                             Yes              Yes               Yes              Yes
#> _______________ _________________ ________________ _________________ ________________
#> S.E.: Clustered             by: f            by: f             by: f            by: f
#> Observations               10,000           10,000            10,000           10,000
#> R2                        0.12849          0.61462           0.12849          0.61462
#> Within R2                 0.12843          0.61459           0.12843          0.61459
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1