## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "figures/intro-",
  warning = FALSE,
  message = FALSE
)

## ----load-packages------------------------------------------------------------
library(SCMs)
library(data.table)
library(ggplot2)

# For reproducibility
set.seed(123)

## ----create-data--------------------------------------------------------------
# Create panel data for demonstration
states <- c("California", "Texas", "New York", "Florida", "Illinois")
years <- 1990:2010

# Generate full panel
panel_data <- expand.grid(
  state = states,
  year = years,
  stringsAsFactors = FALSE
)

# Convert to data.table for efficiency
setDT(panel_data)

# Add outcome variable with treatment effect for California after 2000
panel_data[, gdp := 100 + (year - 1990) * 1.5 + rnorm(.N, 0, 3)]
panel_data[state == "California" & year > 2000, gdp := gdp + 8]

# Add covariates
panel_data[, population := 1000 + (year - 1990) * 50 + rnorm(.N, 0, 100)]
panel_data[, investment := 20 + gdp * 0.15 + rnorm(.N, 0, 5)]

# Display sample of data
head(panel_data, 10)

## ----basic-scdata, eval=FALSE-------------------------------------------------
# # Prepare synthetic control data
# scdata_result <- scdata(
#   df = panel_data,
#   id.var = "state",
#   time.var = "year",
#   outcome.var = "gdp",
#   period.pre = 1990:2000,
#   period.post = 2001:2010,
#   unit.tr = "California",
#   unit.co = c("Texas", "New York", "Florida", "Illinois"),
#   covagg = list(
#     list(var = "population"),
#     list(var = "investment")
#   )
# )
# 
# # Estimate synthetic control weights
# scest_result <- scest(
#   data = scdata_result,
#   w.constr = list(name = "simplex"),
#   feature_weights = "uniform"
# )
# 
# # View results
# print(scest_result)
# summary(scest_result)

## ----spec-curve, eval=FALSE---------------------------------------------------
# # Define covariate specifications to test
# covariate_specs <- list(
#   # Basic specification
#   basic = list(
#     label = "basic",
#     operations = list(
#       list(var = "population", compute = "mean"),
#       list(var = "investment", compute = "mean")
#     )
#   ),
# 
#   # Include outcome lags
#   with_lags = list(
#     label = "with_lags",
#     operations = list(
#       list(var = "outcome_var", partition_periods = list(type = "by_period")),
#       list(var = "population", compute = "mean"),
#       list(var = "investment", compute = "mean")
#     )
#   ),
# 
#   # Population only
#   population_only = list(
#     label = "population_only",
#     operations = list(
#       list(var = "population", compute = "mean")
#     )
#   )
# )
# 
# # Run specification curve analysis
# analysis_params <- list(
#   outcomes = "gdp",
#   covagg = covariate_specs,
#   col_name_unit_name = "state",
#   name_treated_unit = "California",
#   treated_period = 2001,
#   min_period = 1990,
#   end_period = 2010,
#   col_name_period = "year",
#   feature_weights = c("uniform", "optimize"),
#   outcome_models = c("none", "ridge"),
#   constraints = list(
#     list(name = "simplex"),
#     list(name = "lasso", Q = 0.1)
#   ),
#   donor_sample = "all",
#   constants = FALSE,
#   inference_type = "placebo",
#   inference_config = list(verbose = TRUE)
# )
# 
# spec_results <- run_spec_curve_analysis(
#   dataset = panel_data,
#   params = analysis_params,
#   cores = 1  # Use 1 core for vignette
# )
# 
# # View specification curve
# plot(spec_results)

