## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  # This vignette uses PRECOMPUTED figures kept in man/figures/ -- the very same
  # images shown in the README and in the help pages. The estimator code below is
  # shown for reference but is NOT run at build time, which keeps R CMD build,
  # R CMD check and pkgdown fast (a full cross-validation + bootstrap per figure
  # would otherwise dominate every check). Copy any snippet into an R session to
  # run it live. Figures render because the output is self-contained: each
  # man/figures/*.png is embedded (base64) into the built HTML, so the vignette
  # still shows them after the package is installed.
  eval = FALSE,
  echo = TRUE
)
# Chunks that display a shared figure opt in with `opts.label = "fig"`.
knitr::opts_template$set(fig = list(
  eval = TRUE, echo = FALSE, out.width = "100%", fig.align = "center"
))

## ----setup--------------------------------------------------------------------
# library(cfcompare)

## ----sim----------------------------------------------------------------------
# df <- sim_panel(N = 16, T = 9, n_treated = 3, t0 = 7, rank = 2,
#                 att = 2, noise = 1, seed = 1)
# head(df)

## ----compare------------------------------------------------------------------
# cmp <- panel_compare(
#   df,
#   outcome = "y", treatment = "w", unit = "id", time = "t",
#   methods = c("DID", "MC", "TROP"),
#   se = "jackknife",
#   control = trop_control(n_cv_cells = 12L, cv_cycles = 1L)
# )
# cmp

## ----forest-code, eval = FALSE------------------------------------------------
# autoplot(cmp)

## ----forest-fig, opts.label = "fig"-------------------------------------------
knitr::include_graphics("../man/figures/compare_forest.png")

## ----cf, eval = FALSE---------------------------------------------------------
# plot_counterfactual(cmp)

## ----trop-fit-----------------------------------------------------------------
# fit <- trop(df, "y", "w", "id", "t", se = "jackknife",
#             control = trop_control(n_cv_cells = 12L, cv_cycles = 1L))
# fit

## ----trop-internals-----------------------------------------------------------
# head(fit$tau_cells)
# fit$lambda

## ----trop-autoplot, eval = FALSE----------------------------------------------
# autoplot(fit)

## ----trajectory-fig, opts.label = "fig"---------------------------------------
knitr::include_graphics("../man/figures/trop_trajectory.png")

## ----trop-event---------------------------------------------------------------
# es <- trop_event_study(fit, se = "bootstrap")   # bootstrap is the default
# es

## ----es-code, eval = FALSE----------------------------------------------------
# autoplot(es)

## ----es-fig, opts.label = "fig"-----------------------------------------------
knitr::include_graphics("../man/figures/trop_event_study_bootstrap.png")

## ----placebo-test-------------------------------------------------------------
# pt <- trop_placebo_test(fit, B = 500)
# pt

## ----pt-code, eval = FALSE----------------------------------------------------
# autoplot(pt)

## ----pt-fig, opts.label = "fig"-----------------------------------------------
knitr::include_graphics("../man/figures/placebo_test.png")

## ----se-modes-----------------------------------------------------------------
# se_cmp <- compare_se_modes(
#   df, "y", "w", "id", "t",
#   se = c("bootstrap", "jackknife"),
#   control = trop_control(n_cv_cells = 12L, cv_cycles = 1L)
# )
# se_cmp

## ----se-code, eval = FALSE----------------------------------------------------
# autoplot(se_cmp)

## ----se-fig, opts.label = "fig"-----------------------------------------------
knitr::include_graphics("../man/figures/se_comparison.png")

## ----special------------------------------------------------------------------
# did <- trop(df, "y", "w", "id", "t",
#             lambda = list(time = 0, unit = 0, nn = Inf))
# mc  <- trop(df, "y", "w", "id", "t",
#             lambda = list(time = 0, unit = 0, nn = 5))
# c(DID = did$att, MC = mc$att, TROP = fit$att)

## ----ablation-----------------------------------------------------------------
# ab <- trop_ablation(df, "y", "w", "id", "t",
#                     rmse = TRUE, horizon = 3, n_pseudo = 5, n_runs = 100,
#                     control = trop_control(n_cv_cells = 12L, cv_cycles = 1L))
# ab

## ----ablation-fmt, eval = FALSE-----------------------------------------------
# cat(format(ab, "latex"), sep = "\n")     # needs \usepackage{booktabs}
# writeLines(format(ab, "markdown"))
# plot(ab)                                 # paper-style Table-5 figure

## ----ablation-fig, opts.label = "fig"-----------------------------------------
knitr::include_graphics("../man/figures/trop_ablation.png")

## ----covariates---------------------------------------------------------------
# set.seed(1)
# dfx <- df
# dfx$x1 <- rnorm(nrow(dfx))
# dfx$y  <- dfx$y + 1.3 * dfx$x1          # covariate is part of Y(0)
# fitx <- trop(dfx, "y", "w", "id", "t", covariates = "x1", se = "none",
#              control = trop_control(n_cv_cells = 12L, cv_cycles = 1L))
# fitx$phi

## ----cf-matrix----------------------------------------------------------------
# M <- counterfactual_matrix(fit)
# dim(M)

## ----pred-rmse, eval = FALSE--------------------------------------------------
# panel_rmse(df, "y", "w", "id", "t",
#            methods = c("DID", "MC", "TROP", "DIFP"),
#            metric = "prediction", horizon = 3, n_pseudo = 5, n_runs = 5, seed = 1)

## ----rmse-fig, opts.label = "fig"---------------------------------------------
knitr::include_graphics("../man/figures/rmse_prediction.png")

## ----byo, eval = FALSE--------------------------------------------------------
# library(synthdid)
# sd <- synthdid_estimate(Y, N0, T0)
# 
# combined <- as_att(list(
#   fit,
#   as_att(sd, method = "SDID", outcome = "y")
# ))
# autoplot(combined)

## ----perf, eval = FALSE-------------------------------------------------------
# trop(df, "y", "w", "id", "t", se = "bootstrap",
#      control = trop_control(svd = "truncated", workers = 4))

