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, e.g., by specifying an environment?
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
Also posted as a GitHub issue.
Update: The solution seems to be forcing an early evaluation of the expression that refers to the dataset. Another way is to specify the dataset again within update()
:
update(mod2, . ~ . + log(Year), data = trade)