knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) knitr::opts_chunk$set(echo = TRUE)
# Load necessary libraries library(dplyr) library(fetwfe)
This vignette demonstrates how to conduct simulation studies with the {fetwfe}
package. In particular, we will:
genCoefs()
. These coefficients produce unit‐and‐time specific responses that respect difference‐in‐differences assumptions (e.g., conditional parallel trends) and the sparsity assumptions behind FETWFE.simulateData()
.fetwfeWithSimulatedData()
.getTes()
.The workflow here follows the simulation‐study design outlined in the paper, so you may wish to skim its setup section for additional context.
Below is a complete simulation pipeline, step by step.
The genCoefs()
function returns an object of class "FETWFE_coefs"
, containing both the coefficient vector and its simulation parameters. In this example we set:
# Generate the coefficient object for simulation sim_coefs <- genCoefs( R = 3, T = 4, d = 2, density = 0.1, eff_size = 2, seed = 101 )
(Again, for more details on the meaning of these parameters, see the simulation study section of the paper.)
Next, we simulate a panel data set using the generated coefficient object with the simulateData()
function. With simulateData()
, we generate:
N
units, each assigned to one of the cohortsHere we choose:
N
(number of units) as 60,sig_eps_sq
(observation-level noise variance) as 1,sig_eps_c_sq
(unit-level noise variance) as 1, and"gaussian"
distribution for the covariates.# Simulate panel data based on the coefficients sim_data <- simulateData( sim_coefs, N = 60, sig_eps_sq = 1, sig_eps_c_sq = 1, distribution = "gaussian" )
The dataframe is stored in sim_data$pdata
, so we can take a quick look at the results:
head(sim_data$pdata)
We then run the estimator on the simulated data using fetwfeWithSimulatedData()
. (We could get the same results by manually unpacking sim_data
and passing the arguments appropriately to fewtfe()
. fetwfeWithSimulatedData()
is just a wrapper function that takes care of this for us.)
result <- fetwfeWithSimulatedData(sim_data)
We can now extract the results from result
in the same way that we can with the standard fetwfe()
function.
summary(result)
To evaluate the estimated ATT, we can compute the true treatment effects using the original coefficient object. The getTes()
function extracts both the overall average treatment effect and the cohort-specific effects.
# Extract the true treatment effects true_tes <- getTes(sim_coefs) # Print the true overall treatment effect cat("True Overall ATT:", true_tes$att_true, "\n") # Print the cohort-specific treatment effects print(true_tes$actual_cohort_tes)
We can use this to calculate metrics to evaluate our estimated treatment effect, like squared error:
squared_error <- (result$att_hat - true_tes$att_true)^2 cat("Squared error of ATT estimate:", squared_error, "\n")
You can also chain the simulation functions together with the pipe operator. The following code generates the coefficients, simulates the data, and runs the estimator all in one pipeline:
coefs <- genCoefs(R = 3, T = 4, d = 2, density = 0.1, eff_size = 2, seed = 2025) result_piped <- coefs |> simulateData(N = 60, sig_eps_sq = 1, sig_eps_c_sq = 1) |> fetwfeWithSimulatedData() cat("Estimated Overall ATT from piped workflow:", result_piped$att_hat, "\n") true_tes_piped <- coefs |> getTes() # Print the true overall treatment effect cat("True Overall ATT:", true_tes_piped$att_true, "\n") # Print the squared estimation error squared_error_piped = (result_piped$att_hat - true_tes_piped$att_true)^2 cat("Squared estimation error:", squared_error_piped, "\n")
In this vignette, we walked through how to use the simulation functions in the {fetwfe}
package to simulate data and run simulations similar to the ones in the simulation studies section of the FETWFE paper.
This pipeline streamlines simulation experiments so you can rapidly evaluate FETWFE’s performance under varying scenarios. For more details, consult the package documentation or reach out to the author.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.