Constant depvar Error when following fixest simulation
Closed this issue · 1 comments
apoorvalal commented
Nice work with the package. I was trying to follow along with the fixest
simulation to compare the 2-step procedure with 2wfe and Abraham and Sun and got a somewhat cryptic error
Error in fixest::feols(formula, data = data, warn = FALSE): The dependent variable is a constant. The estimation cannot be done. If you really want to carry on, please remove the intercept first.
which looks like an error from the underlying fixest call in your package. Any idea what might be going on?
library(did2s)
library(fixest)
# %% ####################################################
# Generate some (fake) staggered DiD data
set.seed(1)
n_group = 20
n_per_group = 5
id_i = paste0((1:n_group), ":", rep(1:n_per_group, each = n_group))
id_t = 1:10
base = expand.grid(id = id_i, year = id_t)
base$group = as.numeric(gsub(":.+", "", base$id))
base$year_treated = base$group
base$year_treated[base$group > 10] = 10000
base$treat_post = (base$year >= base$year_treated) * 1
base$time_to_treatment = pmax(base$year - base$year_treated, -1000)
base$treated = (base$year_treated < 10000) * 1
# The effect of the treatment is cohort specific and increases with time
base$y_true = base$treat_post * (1 + 1 * base$time_to_treatment - 1 * base$group)
base$y = base$y_true + rnorm(nrow(base))
# Note that the time_to_treatment for controls is set to -1000
# We need to drop the always treated individuals (just a data generation artifact)
base = base[base$group > 1,]
head(base)
# %%
# "Naive" TWFE DiD
res_twfe = feols(y ~ i(treated, time_to_treatment, ref = -1, drop = -1000) | id + year, base)
# With cohort x time-to-treatment dummies
res_cohort = feols(y ~ i(time_to_treatment, f2 = group, drop = c(-1, -1000)) | id + year, base)
# 2step did
es <- did2s(base,
yname = "y", first_stage_formula = ~i(id) + i(year),
treat_formula = ~i(time_to_treatment), treat_var = "treated")
# %%
kylebutts commented
Ahhh, thank you for pointing this out, I have been testing with id's that have all been numeric. Yours is a factor, so it was causing var == val to give a vector of FALSEs. It should be fixed now