lrberge / fixest

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

Update method does not work when estimation is wrapped in function #394

Closed turbanisch closed 5 months ago

turbanisch commented 1 year ago

Apparently the update() method cannot retrieve the dataset the estimation was based on if I wrap the estimation function in another function. Is there any way around this?

library(fixest)
data(trade)

# fit model directly and wrapped into function
mod1 <- fepois(Euros ~ log(dist_km) | Origin + Destination, trade)

fit_model <- function(df) {
  fepois(Euros ~ log(dist_km) | Origin + Destination, data = df)
}

mod2 <- fit_model(trade)

# try to update
update(mod1, . ~ . + log(Year))
#> Poisson estimation, Dep. Var.: Euros
#> Observations: 38,325 
#> Fixed-effects: Origin: 15,  Destination: 15
#> Standard-errors: Clustered (Origin) 
#>              Estimate Std. Error  t value  Pr(>|t|)    
#> log(dist_km) -1.51756   0.113171 -13.4095 < 2.2e-16 ***
#> log(Year)    72.36888   6.899699  10.4887 < 2.2e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> Log-Likelihood: -1.212e+12   Adj. Pseudo R2: 0.592897
#>            BIC:  2.424e+12     Squared Cor.: 0.384441
update(mod2, . ~ . + log(Year))
#> Error in fepois(fml = Euros ~ log(dist_km) + log(Year) | Origin + Destination, : Argument 'data' must be either: i) a matrix, or ii) a data.frame.
#> Problem: it is not a matrix nor a data.frame (instead it is a function).

Created on 2023-02-26 with reprex v2.0.2

lrberge commented 5 months ago

Hi, actually this is perfectly normal since, once out of the function, the data set does not exist anymore. You can try with lm you would get the same error.

That said, with the new argument data.save, it will work:

fit_model <- function(df) {
  fepois(Euros ~ log(dist_km) | Origin + Destination, 
         data = df, data.save = TRUE)
}

mod2 <- fit_model(trade)

# try to update
update(mod2, . ~ . + log(Year))
#> Poisson estimation, Dep. Var.: Euros
#> Observations: 38,325
#> Fixed-effects: Origin: 15,  Destination: 15
#> Standard-errors: Clustered (Origin)
#>              Estimate Std. Error  z value  Pr(>|z|)
#> log(dist_km) -1.51756   0.113171 -13.4095 < 2.2e-16 ***
#> log(Year)    72.36888   6.899699  10.4887 < 2.2e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> Log-Likelihood: -1.212e+12   Adj. Pseudo R2: 0.592897
#>            BIC:  2.424e+12     Squared Cor.: 0.384441