This function runs the bridge-penalized extended two-way fixed effects estimator (betwfe()) on
simulated data. It is simply a wrapper for betwfe(): it accepts an object of class
"FETWFE_simulated" (produced by simulateData()) and unpacks the necessary
components to pass to betwfe(). So the outputs match betwfe(), and the needed inputs
match their counterparts in betwfe().
Usage
betwfeWithSimulatedData(
simulated_obj,
lambda.max = NA,
lambda.min = NA,
nlambda = 100,
q = 0.5,
verbose = FALSE,
alpha = 0.05,
add_ridge = FALSE,
allow_no_never_treated = TRUE,
se_type = "default",
lambda_selection = "cv",
cv_folds = 10L,
cv_seed = NULL,
ci_type = c("simultaneous", "pointwise")
)Arguments
- simulated_obj
An object of class
"FETWFE_simulated"containing the simulated panel data and design matrix.- lambda.max
(Optional.) Numeric. A penalty parameter
lambdawill be selected over a grid search by BIC in order to select a single model. The largestlambdain the grid will belambda.max. If nolambda.maxis provided, one will be selected automatically. Forlambda <= 1, the model will be sparse, and ideally all of the following are true at once: the smallest model (the one corresponding tolambda.max) selects close to 0 features, the largest model (the one corresponding tolambda.min) selects close topfeatures,nlambdais large enough so that models are considered at every feasible model size, andnlambdais small enough so that the computation doesn't become infeasible. You may want to manually tweaklambda.max,lambda.min, andnlambdato try to achieve these goals, particularly if the selected model size is very close to the model corresponding tolambda.maxorlambda.min, which could indicate that the range oflambdavalues was too narrow. You can use the function outputslambda.max_model_size,lambda.min_model_size, andlambda_star_model_sizeto try to assess this. Default is NA.- lambda.min
(Optional.) Numeric. The smallest
lambdapenalty parameter that will be considered. See the description oflambda.maxfor details. Default is NA.- nlambda
(Optional.) Integer. The total number of
lambdapenalty parameters that will be considered. See the description oflambda.maxfor details. Default is 100.- q
(Optional.) Numeric; determines what
L_qpenalty is used for the fusion regularization.q= 1 is the lasso, and for 0 <q< 1, it is possible to get standard errors and confidence intervals.q= 2 is ridge regression. See Faletto (2025) for details. Default is 0.5.- verbose
Logical; if TRUE, more details on the progress of the function will be printed as the function executes. Default is FALSE.
- alpha
Numeric; function will calculate (1 -
alpha) confidence intervals for the cohort average treatment effects that will be returned incatt_df.- add_ridge
(Optional.) Logical; if TRUE, adds a small amount of ridge regularization to the (untransformed) coefficients to stabilize estimation. Default is FALSE.
- allow_no_never_treated
(Optional.) Logical; if
TRUE(default) and the input panel contains no never-treated units, the panel is auto-truncated by dropping time periods at and after the latest cohort's start time — the units in that latest cohort then serve as the never-treated comparison group in the retained sub-panel — with a warning naming the dropped periods. IfFALSE, the estimator stops with an error in this case (the package's behavior prior to version 1.5.6). The argument has no effect when the input already contains never-treated units. Default isTRUE.- se_type
Character; one of
"default","conservative", or"cluster"."default"returns the tight Gaussian variancesqrt(att_var_1 + att_var_2)from Theorem (c$'$) under Assumption (Psi-IF); this is asymptotically exact for the package's default cohort sample-proportions estimator and for every standard propensity-score estimator that satisfies (Psi-IF) (multinomial logit, any GLM onW | X, kernel/series regression of1{W = g}onX)."conservative"returns the Cauchy-Schwarz upper bound from Theorem (c); use only if the propensity-score estimator violates (Psi-IF)."cluster"is an experimental unit-clustered Liang-Zeger sandwich SE on the bridge-selected support (see the companion vignetteinference_vignettefor details)."cluster"is only meaningful whenq < 1(the bridge oracle property is required); forq >= 1the SE will beNAregardless ofse_type. The default"default"was the conservative Cauchy-Schwarz formula in versions <= 1.11.7; v1.12.0 switched the default to the tight Gaussian variance. Default is"default".- lambda_selection
Character; method for selecting the bridge penalty parameter
lambda. Either"cv"(10-fold cross-validation oncv.grpreg; the v1.13.0+ default) or"bic"(BIC over thegrpreglambda grid; the prior default for v1.12.0 and earlier). The default changed in v1.13.0 to address a finite-sample bias issue documented in simulation studies (see issue #164). Passlambda_selection = "bic"to recover the prior behavior. See the inference vignette section "Choosing the bridge penalty parameter" for details.- cv_folds
Integer; number of folds for the CV path. Ignored when
lambda_selection = "bic". Default is 10.- cv_seed
Integer or
NULL; the seed passed toset.seed()immediately before thecv.grpreg()call. IfNULL(the default), the seed defaults internally toas.integer(N * T). Ignored whenlambda_selection = "bic".- ci_type
Character; one of
"simultaneous"(default) or"pointwise". Controls the confidence-interval bounds reported for the cohort-specific ATTs (incatt_df) and the event-study effects (fromeventStudy(), shown byprint/summary/plot, and surfaced bybroom::tidy()on the fitted object and on theeventStudy()/cohortStudy()outputs)."simultaneous"reports parametric simultaneous (family-wise, uniform) bands computed viasimultaneousCIs(): each family's band covers all of its effects jointly with probability1 - alpha, matching the default presentation ofdid::aggte(cband = TRUE)."pointwise"reports per-effect Wald intervals (each covers its own effect with probability1 - alpha, no joint guarantee — the behavior of versions <= 1.15.1). Both the interval bounds and the per-cohort p-values (p_value) followci_type: under"simultaneous"thep_valueis the single-step max-T multiplicity- adjusted p-value matching the band, under"pointwise"the per-cohort Wald p-value (#200). The standard errors (se) and selection flags (selected) are identical under both settings, and the overall-ATT confidence interval (a single scalar) is unaffected. When standard errors are unavailable (q >= 1, or a rank-deficient design) the bounds areNAunder both settings. Default is"simultaneous".
Value
An object of class betwfe containing the following elements:
- att_hat
The estimated overall average treatment effect for a randomly selected treated unit.
- att_se
If
q < 1, a standard error for the ATT. Ifindep_countswas provided, this standard error is asymptotically exact; if not, it is asymptotically conservative. Ifq >= 1, this will be NA.- att_p_value
A two-sided p-value for the overall ATT against the null
H_0: tau = 0, computed as2 * pnorm(-|att_hat / att_se|).NAifatt_seis zero orNA(e.g., under the bridge solver's selected-out fallback).- att_selected
Logical scalar;
TRUEifatt_hatis not exactly zero,FALSEotherwise. BETWFE uses bridge regression directly on the coefficients (rather than on the fused restrictions used by FETWFE); under the bridge oracle property of Kock (2013),att_selected = FALSEis an analogous asymptotic statement that the truth is zero under a sparsity assumption different from the one Theorem 6.2 establishes for FETWFE. For ridge (q = 2) the bridge solver does not zero coefficients, so this will typically beTRUE.- catt_hats
A named vector containing the estimated average treatment effects for each cohort.
- catt_ses
If
q < 1, a named vector containing the (asymptotically exact, non-conservative) standard errors for the estimated average treatment effects within each cohort.- cohort_probs
A vector of the estimated probabilities of being in each cohort conditional on being treated, which was used in calculating
att_hat. Ifindep_countswas provided,cohort_probswas calculated from that; otherwise, it was calculated from the counts of units in each treated cohort inpdata.- catt_df
A data frame (with S3 class
c("catt_df", "data.frame")) displaying the cohort names (cohort), average treatment effects (estimate), standard errors (se),1 - alphaconfidence interval bounds (ci_low,ci_high), per-cohort p-values (p_value), and aselectedlogical flag (TRUEwhen the bridge penalty left the cohort's CATT nonzero). For selected-out cohorts (selected = FALSE),p_valueisNA. Thecatt_dfS3 class makes[[/$/[access on the pre-1.11.0 Title-Case column names (Cohort,Estimated TE,SE,ConfIntLow,ConfIntHigh,P_value)stop()with a migration message pointing to the new name. SeeNEWS.mdfor the rename table.- beta_hat
The full vector of estimated coefficients.
- treat_inds
The indices of
beta_hatcorresponding to the treatment effects for each cohort at each time.- treat_int_inds
The indices of
beta_hatcorresponding to the interactions between the treatment effects for each cohort at each time and the covariates.- sig_eps_sq
Either the provided
sig_eps_sqor the estimated one, if a value wasn't provided.- sig_eps_c_sq
Either the provided
sig_eps_c_sqor the estimated one, if a value wasn't provided.- lambda.max
Either the provided
lambda.maxor the one that was used, if a value wasn't provided. (This is returned to help with getting a reasonable range oflambdavalues for grid search.)- lambda.max_model_size
The number of selected features (excluding the always-present intercept) at
lambda.max(forq <= 1, the smallest model). As mentioned above, forq <= 1ideally this value is close to 0.- lambda.min
Either the provided
lambda.minor the one that was used, if a value wasn't provided.- lambda.min_model_size
The number of selected features (excluding the always-present intercept) at
lambda.min(forq <= 1, the largest model). As mentioned above, forq <= 1ideally this value is close top.- lambda_star
The value of
lambdachosen by the method recorded inlambda_selection. If this value is close tolambda.minorlambda.max, that could suggest that the range oflambdavalues should be expanded.- lambda_star_model_size
The number of selected features (excluding the always-present intercept) in the chosen model. If this value is close to
lambda.max_model_sizeorlambda.min_model_size, that could suggest that the range oflambdavalues should be expanded.- lambda_selection
Character scalar; either
"cv"or"bic". Mirrors thelambda_selectionargument the user passed.- cv_folds
Integer scalar; the
cv_foldsvalue used whenlambda_selection = "cv",NA_integer_whenlambda_selection = "bic".- cv_seed
Integer scalar; the seed actually fed to
set.seed()immediately beforecv.grpreg()was called. Defaults toas.integer(N * T)when the user did not pass a seed.NA_integer_whenlambda_selection = "bic".- ci_type
Character scalar; the
ci_typeargument the user passed ("simultaneous"or"pointwise"), controlling whether the reportedcatt_df/eventStudy()confidence-interval bounds are simultaneous (family-wise) or pointwise.- X_ints
The design matrix created containing all interactions, time and cohort dummies, etc.
- y
The vector of responses, containing
nrow(X_ints)entries.- X_final
The design matrix after applying the change in coordinates to fit the model and also multiplying on the left by the square root inverse of the estimated covariance matrix for each unit.
- y_final
The final response after multiplying on the left by the square root inverse of the estimated covariance matrix for each unit.
- N
The final number of units that were in the data set used for estimation (after any units may have been removed because they were treated in the first time period).
- T
The number of time periods in the final data set.
- G
The final number of treated cohorts that appear in the final data set.
- R
Deprecated alias for
G, retained for backward compatibility; populated with the same value. UseG. Will be removed in a future release.- d
The final number of covariates that appear in the final data set (after any covariates may have been removed because they contained missing values or all contained the same value for every unit).
- p
The final number of columns in the full set of covariates used to estimate the model.
- alpha
The alpha level used for confidence intervals.
- calc_ses
Logical indicating whether standard errors were calculated.
- cohort_probs_overall
A vector of the estimated cohort probabilities on the overall sample (treated and untreated), used in computing the variance of the overall ATT.
- indep_counts_used
Logical scalar;
TRUEif a validindep_countsargument was provided and used for asymptotically-exact ATT inference,FALSEotherwise.- se_type
Character scalar; the
se_typeargument the user passed ("default","conservative", or"cluster").- y_mean
Numeric scalar; mean of the original (pre-centering) response. Stored so downstream methods (
augment(),predict()) can return fitted values on the original-response scale.- response_col_name
Character scalar; the response column name in the original
pdata. Consumed byaugment.betwfe().- time_var, unit_var, treatment
Character scalars; the corresponding arguments the user passed.
- covs
Character vector; the original
covsargument (pre-factor- expansion).- internal
A list containing internal outputs that are typically not needed for interpretation, packaged here for parity with
fetwfe()so downstream consumers can use a single canonical access path across all four estimator classes (#144). The first five sub-slots (X_ints,y,X_final,y_final,calc_ses) are also duplicated at top level for backward compat;variance_componentsandfirst_yearlive only under$internal:- X_ints
The design matrix containing all interactions, time and cohort dummies, etc. Same value as top-level
X_ints.- y
The vector of responses. Same as top-level
y.- X_final
The design matrix after the change-of-coordinates step. Same as top-level
X_final.- y_final
The transformed response vector. Same as top-level
y_final.- calc_ses
Logical indicating whether standard errors were calculated. Same as top-level
calc_ses.- variance_components
A list exposing the two variance pieces (
att_var_1,att_var_2) plus paper-notation counterparts (V_1,V_2) and unit-scaled variance estimators (tilde_v_N,hat_v_N,tilde_v_N_C,tilde_v_N_C_pi_hat,tilde_v_N_C_pi_hat_cons,tilde_v_N_cons). The Wald CI is[hat_T_N +- qnorm(1-alpha/2) * sqrt(tilde_v_N / N)](paper Eq.conf.int.form). New in v1.12.0 (issue #141 + #146).- first_year
Integer or numeric scalar; the first (earliest)
time_varvalue in the panel afteridCohorts()processing. Consumed byeventStudy()to mapcohort_probs' cohort labels (treatment-start years) to 1-based panel-time-index offsets when the labels are integer-coercible. New in v1.13.3 (issue #174).
Examples
if (FALSE) { # \dontrun{
# Generate coefficients
coefs <- genCoefs(G = 5, T = 30, d = 12, density = 0.1, eff_size = 2, seed = 123)
# Simulate data using the coefficients
sim_data <- simulateData(coefs, N = 120, sig_eps_sq = 5, sig_eps_c_sq = 5, seed = 123)
result <- betwfeWithSimulatedData(sim_data)
} # }