Convert data prepared for etwfe::etwfe() to the format required by fetwfe() and fetwfe::etwfe()
Source: R/convert_dfs.R
etwfeToFetwfeDf.RdetwfeToFetwfeDf() reshapes and renames a panel dataset that is already
formatted for etwfe::etwfe() (McDermott 2024) so that it can be
passed directly to fetwfe() or etwfe() from the fetwfe package. In
particular, it
creates an absorbing-state treatment dummy that equals 1 from the first treated period onward* and 0 otherwise,
(optionally) drops units that are already treated in the very first period of the sample (because
fetwfe()removes them internally), andreturns a tidy dataframe whose column names match the arguments that
fetwfe()/etwfe()expect.
Arguments
- data
A long-format data.frame that you could already feed to
etwfe().- yvar
Character. Column name of the outcome (left-hand side in your
fml).- tvar
Character. Column name of the time variable that you pass to
etwfe()astvar.- idvar
Character. Column name of the unit identifier (the variable you would cluster on, or pass to
etwfe(..., ivar = idvar)if you were using unit FEs).- gvar
Character. Column name of the "first treated" cohort variable passed to
etwfe()asgvar. Must be0(orInf, which is mapped to0) for never-treated units, or the (strictly positive) first treated period.- covars
Character vector of additional covariate columns to keep (default
character(0)).- drop_first_period_treated
Logical. Should units already treated in the very first sample period be removed? (
fetwfe()will drop them internally anyway, but doing it here keeps the returned dataframe clean.) DefaultTRUE.- out_names
Named list giving the column names that the returned dataframe should have. The default (
time_var,unit_var,treatment,response) matches the arguments usually supplied tofetwfe(). Do not change the names of this list – only the values – and keep all four.- verbose
Logical. If
TRUE, amessage()reports the count of first-period-treated unit-period rows dropped whendrop_first_period_treated = TRUE. DefaultFALSE(silent).
Value
A tidy data.frame with (in this order)
time_varinteger,unit_varcharacter,treatmentinteger 0/1 absorbing-state dummy,responsenumeric outcome,any covariates requested in
covars. Ready to pass straight tofetwfe()orfetwfe::etwfe().
References
McDermott G (2024). etwfe: Extended Two-Way Fixed Effects. doi:10.32614/CRAN.package.etwfe doi:10.32614/CRAN.package.etwfe , R package version 0.5.0, https://CRAN.R-project.org/package=etwfe.
Examples
## toy example ---------------------------------------------------------------
if (FALSE) { # \dontrun{
library(did) # provides the mpdta example dataframe
data(mpdta)
head(mpdta)
tidy_df <- etwfeToFetwfeDf(
data = mpdta,
yvar = "lemp",
tvar = "year",
idvar = "countyreal",
gvar = "first.treat",
covars = c("lpop"))
head(tidy_df)
} # }
## Now you can call fetwfe() ------------------------------------------------
# res <- fetwfe(
# pdata = tidy_df,
# time_var = "time_var",
# unit_var = "unit_var",
# treatment = "treatment",
# response = "response",
# covs = c("lpop"))