## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  warning = FALSE,
  message = FALSE,
  fig.width = 7,
  fig.height = 5,
  dpi = 150
)

# Load the development version of the package
library(fixes)
library(dplyr)
library(ggplot2)

## ----minimal-example----------------------------------------------------------
# Load example data
df <- fixest::base_did

# Run the event study (supports multiple confidence levels)
event_study <- run_es(
  data       = df,
  outcome    = y,
  treatment  = treat,
  time       = period,
  timing     = 5,  # Treatment occurs at period 5
  fe         = ~ id + period,
  cluster    = ~ id,
  baseline   = -1,
  interval   = 1,
  lead_range = 3,
  lag_range  = 3,
  conf.level = c(0.90, 0.95, 0.99)  # Multiple CIs supported!
)

# View results
head(event_study)

## ----plot-basic---------------------------------------------------------------
# Basic plot (default: ribbon, 95% CI)
plot_es(event_study)

## ----plot-errorbar------------------------------------------------------------
# Plot with error bars and 99% CI
plot_es(event_study, type = "errorbar", ci_level = 0.99)

## ----plot-custom--------------------------------------------------------------
# Customize further with ggplot2
plot_es(event_study, type = "errorbar", ci_level = 0.9, theme_style = "classic") +
  scale_x_continuous(breaks = seq(-3, 3, by = 1)) +
  ggtitle("Event Study with 90% CI and Classic Theme")

## ----sunab-example------------------------------------------------------------
# Example with fixest::base_stagg data
df_stagg <- fixest::base_stagg

event_study_sunab <- run_es(
  data       = df_stagg,
  outcome    = y,
  treatment  = treated,
  time       = year,
  timing     = year_treated,
  fe         = ~ id + year,
  staggered  = TRUE,
  method     = "sunab",  # Use Sun & Abraham decomposition
  lead_range = 3,
  lag_range  = 3,
  cluster    = ~ id
)

head(event_study_sunab)

## ----plot-sunab---------------------------------------------------------------
# Visualize sunab results
plot_es(event_study_sunab) +
  ggtitle("Staggered Adoption Event Study (Sun & Abraham 2021)")

## ----stagg-data---------------------------------------------------------------
df_stagg <- fixest::base_stagg
# Mark never-treated units with NA (convention for all three estimators)
df_stagg$timing <- df_stagg$year_treated
df_stagg$timing[df_stagg$year_treated == 10000] <- NA

## ----cs-est-------------------------------------------------------------------
res_cs <- run_es(
  data          = df_stagg,
  outcome       = y,
  time          = year,
  timing        = timing,
  unit          = id,
  staggered     = TRUE,
  estimator     = "cs",
  control_group = "nevertreated"
)
plot_es(res_cs) + ggplot2::ggtitle("Callaway & Sant'Anna (2021)")

## ----sa-est-------------------------------------------------------------------
res_sa <- run_es(
  data      = df_stagg,
  outcome   = y,
  treatment = treated,
  time      = year,
  timing    = timing,
  unit      = id,
  fe        = ~ id + year,
  staggered = TRUE,
  estimator = "sa",
  cluster   = ~ id
)
plot_es(res_sa) + ggplot2::ggtitle("Sun & Abraham (2021)")

## ----bjs-est------------------------------------------------------------------
res_bjs <- run_es(
  data      = df_stagg,
  outcome   = y,
  time      = year,
  timing    = timing,
  unit      = id,
  staggered = TRUE,
  estimator = "bjs"
)
plot_es(res_bjs) + ggplot2::ggtitle("Borusyak, Jaravel & Spiess (2024)")

## ----boot-demo, eval=FALSE----------------------------------------------------
# # NOTE: B = 199 shown here for brevity; use B = 999 in practice
# res_cs_boot <- run_es(
#   data          = df_stagg,
#   outcome       = y,
#   time          = year,
#   timing        = timing,
#   unit          = id,
#   staggered     = TRUE,
#   estimator     = "cs",
#   control_group = "nevertreated",
#   bootstrap     = TRUE,
#   B             = 199,
#   boot_seed     = 42
# )
# # The lighter outer band is the simultaneous CI; the darker inner band is
# # the standard pointwise CI.
# plot_es(res_cs_boot, show_simultaneous = TRUE)

## ----heatmap-demo-------------------------------------------------------------
plot_att_gt(res_cs, type = "heatmap")

## ----facet-demo---------------------------------------------------------------
plot_att_gt(res_cs, type = "facet")

