xuyiqing / fect

fixed effect counterfactual estimators
Other
57 stars 20 forks source link

error with gsyth when selected model has r=0 and se=TRUE: Error in one.nonpara(boot.seq[j]) : object 'boot' not found #44

Open MatthieuStigler opened 2 weeks ago

MatthieuStigler commented 2 weeks ago

Running method="gsynth" when selected model has r=0 and se=TRUE leads to the error:

Error in one.nonpara(boot.seq[j]) : object 'boot' not found

reprex

This reprex simulates a dataset with two-way FE so that the resulting model chosen has r=0 so fails. I guess the problem is due to r=0 since setting r=1:5 the model will run fine.

library(fect)
#> Registered S3 method overwritten by 'GGally':
#>   method from   
#>   +.gg   ggplot2
packageVersion("fect")
#> [1] '1.0.0'

## simulate data with two-way FE
set.seed(123)

N <- 100
T <- 10
unit_fe <- rnorm(N, mean = 0, sd = 1)
time_fe <- rnorm(T, mean = 0, sd = 0.5)
panel_data <- data.frame(
  id = rep(1:N, each = T),
  time = rep(1:T, times = N),
  unit_fe = rep(unit_fe, each = T),
  time_fe = rep(time_fe, times = N)
)
panel_data$treat <- ifelse(panel_data$id > 50 & panel_data$time>5,1,0)
panel_data$Y <- 0.8 * panel_data$treat + panel_data$unit_fe + panel_data$time_fe + rnorm(N*T, mean = 0, sd = 0.5)

## estimate
out_fect <- fect(Y ~ treat, data = panel_data, 
                 index = c("id","time"), force = "two-way", method="gsynth",
                 CV=TRUE, r=0:5, nboots=2, se=TRUE, parallel=FALSE) 
#> Factor number should not be greater than 4
#> Cross-validating ...
#> Criterion: Mean Squared Prediction Error
#> Interactive fixed effects model...
#> Cross-validating ...
#> 
#>  r = 0; sigma2 = 0.24800; IC = -0.66099; PC = 0.21874; MSPE = 0.34707
#> *
#> 
#>  r = 1; sigma2 = 0.23029; IC = -0.02661; PC = 0.59874; MSPE = 0.48403
#> 
#>  r = 2; sigma2 = 0.21668; IC = 0.59604; PC = 0.93643; MSPE = 0.89489
#> 
#>  r = 3; sigma2 = 0.19865; IC = 1.16796; PC = 1.20141; MSPE = 132.94558
#> 
#> 
#>  r* = 0
#> 
#> Bootstrapping for uncertainties ...
#> Error in one.nonpara(boot.seq[j]): object 'boot' not found
out_fect <- fect(Y ~ treat, data = panel_data, 
                 index = c("id","time"), force = "two-way", method="gsynth",
                 CV=TRUE, r=1:5, nboots=2, se=TRUE, parallel=FALSE) 
#> Factor number should not be greater than 4
#> Cross-validating ...
#> Criterion: Mean Squared Prediction Error
#> Interactive fixed effects model...
#> Cross-validating ...
#> 
#>  r = 1; sigma2 = 0.23029; IC = -0.02661; PC = 0.59874; MSPE = 0.48403
#> *
#> 
#>  r = 2; sigma2 = 0.21668; IC = 0.59604; PC = 0.93643; MSPE = 0.89489
#> 
#>  r = 3; sigma2 = 0.19865; IC = 1.16796; PC = 1.20141; MSPE = 132.94558
#> 
#> 
#>  r* = 1
#> 
#> Bootstrapping for uncertainties ...
#> 2 runs
#> Cannot use full pre-treatment periods in the F test. The first period is removed.
#> 
#> The estimated covariance matrix is irreversible.
#> 

Created on 2024-10-15 with reprex v2.1.1

MatthieuStigler commented 1 week ago

Hi @xuyiqing

I believe the error is because when CV-selected r is 0, method is set to fe, but this case is not covered by one.nonpara(), i.e. ?

To see this, run: fixInNamespace("fect.boot", "fect") and add browser() at line 758, you will see that method=fe and that hence boot is not defined, explaining the message

one.nonpara(boot.seq[j])

It seems also that one.nonpara() does not cover either the case of method=fe for vartype="parametric"?

Thanks!