| Title: | Synthetic Control Method with Spillover Effects |
|---|---|
| Description: | A general-purpose implementation of synthetic control methods that accounts for potential spillover effects between units. Based on the methodology of Cao and Dowd (2019) <doi:10.48550/arXiv.1902.07343> "Estimation and Inference for Synthetic Control Methods with Spillover Effects". |
| Authors: | Jianfei Cao [aut], Zhanchao Fu [cre] |
| Maintainer: | Zhanchao Fu <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.1 |
| Built: | 2026-05-09 07:11:24 UTC |
| Source: | https://github.com/cran/scmSpillover |
Generate simulated data for testing
generate_test_data( n_units = 20, n_periods = 30, treatment_start = 20, effect_size = -5 )generate_test_data( n_units = 20, n_periods = 30, treatment_start = 20, effect_size = -5 )
n_units |
Number of units |
n_periods |
Number of time periods |
treatment_start |
When treatment begins |
effect_size |
Size of treatment effect |
A numeric matrix with dimensions (n_periods x n_units). The first column represents the treated unit, remaining columns are control units. Rows represent time periods.
# Generate test data with default parameters data <- generate_test_data() dim(data) # 30 periods x 20 units # Generate smaller dataset data <- generate_test_data(n_units = 10, n_periods = 20, treatment_start = 15, effect_size = -3)# Generate test data with default parameters data <- generate_test_data() dim(data) # 30 periods x 20 units # Generate smaller dataset data <- generate_test_data(n_units = 10, n_periods = 20, treatment_start = 15, effect_size = -3)
Generate all plots for SCM analysis
plot_all( result, start_year = NULL, unit_name = "Treated Unit", outcome_label = "Outcome", treatment_label = "Treatment", show_ci = TRUE )plot_all( result, start_year = NULL, unit_name = "Treated Unit", outcome_label = "Outcome", treatment_label = "Treatment", show_ci = TRUE )
result |
Output from run_scm_spillover |
start_year |
First year of treatment period |
unit_name |
Name of treated unit (default "Treated Unit") |
outcome_label |
Label for outcome variable (default "Outcome") |
treatment_label |
Label for treatment (default "Treatment") |
show_ci |
Logical, whether to show confidence bands (default TRUE) |
A list containing three ggplot objects (p1, p2, p3), returned invisibly. Called primarily for side effects (displaying plots).
## Not run: data <- generate_test_data() result <- run_scm_spillover(data, treatment_start = 20) plots <- plot_all(result) ## End(Not run)## Not run: data <- generate_test_data() result <- run_scm_spillover(data, treatment_start = 20) plots <- plot_all(result) ## End(Not run)
Plot treatment effects only
plot_effects( result, start_year = NULL, unit_name = "Treated Unit", outcome_label = "Outcome", show_ci = TRUE, show_vanilla = FALSE )plot_effects( result, start_year = NULL, unit_name = "Treated Unit", outcome_label = "Outcome", show_ci = TRUE, show_vanilla = FALSE )
result |
Output from run_scm_spillover |
start_year |
First year of treatment period |
unit_name |
Name of treated unit (default "Treated Unit") |
outcome_label |
Label for outcome variable (default "Outcome") |
show_ci |
Logical, whether to show confidence bands (default TRUE) |
show_vanilla |
Logical, whether to show vanilla SCM comparison (default FALSE) |
A ggplot object that can be further customized or saved.
data <- generate_test_data() result <- run_scm_spillover(data, treatment_start = 20, verbose = FALSE) p <- plot_effects(result)data <- generate_test_data() result <- run_scm_spillover(data, treatment_start = 20, verbose = FALSE) p <- plot_effects(result)
Plot method for scm_spillover objects
## S3 method for class 'scm_spillover' plot(x, type = "effects", ...)## S3 method for class 'scm_spillover' plot(x, type = "effects", ...)
x |
An object of class scm_spillover |
type |
Type of plot: "effects" or "all" |
... |
Additional arguments passed to plotting functions |
A ggplot object (invisibly for type="all")
Print method for scm_spillover objects
## S3 method for class 'scm_spillover' print(x, ...)## S3 method for class 'scm_spillover' print(x, ...)
x |
An object of class scm_spillover |
... |
Additional arguments (not used) |
Invisibly returns the input object
Quick plot all three main plots in sequence
qplot_all( result, start_year = NULL, unit_name = "Treated Unit", outcome_label = "Outcome", treatment_label = "Treatment", show_ci = TRUE, pause = TRUE )qplot_all( result, start_year = NULL, unit_name = "Treated Unit", outcome_label = "Outcome", treatment_label = "Treatment", show_ci = TRUE, pause = TRUE )
result |
Output from run_scm_spillover |
start_year |
First year of treatment period |
unit_name |
Name of treated unit (default "Treated Unit") |
outcome_label |
Label for outcome variable (default "Outcome") |
treatment_label |
Label for treatment (default "Treatment") |
show_ci |
Logical, whether to show confidence bands (default TRUE) |
pause |
Logical, whether to pause between plots (default TRUE) |
List of ggplot objects (invisibly)
Quick plot with confidence intervals only
qplot_ci( result, start_year = NULL, unit_name = "Treated Unit", outcome_label = "Outcome" )qplot_ci( result, start_year = NULL, unit_name = "Treated Unit", outcome_label = "Outcome" )
result |
Output from run_scm_spillover |
start_year |
First year of treatment period |
unit_name |
Name of treated unit (default "Treated Unit") |
outcome_label |
Label for outcome variable (default "Outcome") |
ggplot object (invisibly)
Quick plot for method comparison (Spillover vs Vanilla)
qplot_compare( result, start_year = NULL, unit_name = "Treated Unit", outcome_label = "Outcome" )qplot_compare( result, start_year = NULL, unit_name = "Treated Unit", outcome_label = "Outcome" )
result |
Output from run_scm_spillover |
start_year |
First year of treatment period |
unit_name |
Name of treated unit (default "Treated Unit") |
outcome_label |
Label for outcome variable (default "Outcome") |
ggplot object (invisibly)
Quick plot for treatment effects with confidence intervals
qplot_effects( result, start_year = NULL, unit_name = "Treated Unit", outcome_label = "Outcome", show_ci = TRUE, show_vanilla = FALSE )qplot_effects( result, start_year = NULL, unit_name = "Treated Unit", outcome_label = "Outcome", show_ci = TRUE, show_vanilla = FALSE )
result |
Output from run_scm_spillover |
start_year |
First year of treatment period |
unit_name |
Name of treated unit (default "Treated Unit") |
outcome_label |
Label for outcome variable (default "Outcome") |
show_ci |
Logical, whether to show confidence bands (default TRUE) |
show_vanilla |
Logical, whether to show vanilla SCM comparison (default FALSE) |
ggplot object (invisibly)
Quick plot without confidence intervals
qplot_point( result, start_year = NULL, unit_name = "Treated Unit", outcome_label = "Outcome" )qplot_point( result, start_year = NULL, unit_name = "Treated Unit", outcome_label = "Outcome" )
result |
Output from run_scm_spillover |
start_year |
First year of treatment period |
unit_name |
Name of treated unit (default "Treated Unit") |
outcome_label |
Label for outcome variable (default "Outcome") |
ggplot object (invisibly)
Quick plot for actual vs synthetic control time series
qplot_series( result, start_year = NULL, unit_name = "Treated Unit", outcome_label = "Outcome", treatment_label = "Treatment" )qplot_series( result, start_year = NULL, unit_name = "Treated Unit", outcome_label = "Outcome", treatment_label = "Treatment" )
result |
Output from run_scm_spillover |
start_year |
First year of treatment period |
unit_name |
Name of treated unit (default "Treated Unit") |
outcome_label |
Label for outcome variable (default "Outcome") |
treatment_label |
Label for treatment (default "Treatment") |
ggplot object (invisibly)
Run Complete SCM Analysis with Spillover Effects
run_scm_spillover( data, treatment_start, treated_unit = 1, affected_units = NULL, verbose = TRUE )run_scm_spillover( data, treatment_start, treated_unit = 1, affected_units = NULL, verbose = TRUE )
data |
Matrix or data frame (time x units) |
treatment_start |
Integer, first treatment period (row number) |
treated_unit |
Integer, column index of treated unit (default = 1) |
affected_units |
Vector of column indices for all affected units (including treated) |
verbose |
Logical, print progress |
List with all results
Safely inverts a matrix using regularization if needed
safe_solve(M, tol = 1e-10)safe_solve(M, tol = 1e-10)
M |
Matrix to invert |
tol |
Tolerance for condition number check |
Inverted matrix
Save all plots to files
save_all_plots( result, prefix = "scm", path = NULL, width = 10, height = 6, dpi = 300, ... )save_all_plots( result, prefix = "scm", path = NULL, width = 10, height = 6, dpi = 300, ... )
result |
Output from run_scm_spillover |
prefix |
File name prefix (default "scm") |
path |
Directory to save plots (required, no default) |
width |
Plot width in inches (default 10) |
height |
Plot height in inches (default 6) |
dpi |
Resolution (default 300) |
... |
Additional arguments passed to plotting functions |
Character vector of saved file paths, returned invisibly.
## Not run: data <- generate_test_data() result <- run_scm_spillover(data, treatment_start = 20) save_all_plots(result, path = tempdir()) ## End(Not run)## Not run: data <- generate_test_data() result <- run_scm_spillover(data, treatment_start = 20) save_all_plots(result, path = tempdir()) ## End(Not run)
Computes synthetic control weights for a single treated unit
scm(Y, lambda = 1e-06)scm(Y, lambda = 1e-06)
Y |
N x T matrix where first row is treated unit |
lambda |
Regularization parameter for numerical stability |
List containing intercept (a) and weights (b)
Computes synthetic control weights for all units
scm_batch(Y, verbose = FALSE)scm_batch(Y, verbose = FALSE)
Y |
N x T matrix of outcomes |
verbose |
Print progress messages |
List with intercepts (a) and weight matrix (B)
Tests whether spillover effects are significant
sp_andrews_spillover(Y0, Y1, A, alpha_sig = 0.05)sp_andrews_spillover(Y0, Y1, A, alpha_sig = 0.05)
Y0 |
N x T pre-treatment matrix |
Y1 |
N x 1 post-treatment vector |
A |
N x k spillover structure matrix |
alpha_sig |
Significance level |
List with test results
Tests significance of treatment effects
sp_andrews_te(Y0, Y1, A, C = NULL, alpha_sig = 0.05)sp_andrews_te(Y0, Y1, A, C = NULL, alpha_sig = 0.05)
Y0 |
N x T pre-treatment matrix |
Y1 |
N x 1 post-treatment vector (single period) |
A |
N x k spillover structure matrix |
C |
Constraint matrix (default tests first unit) |
alpha_sig |
Significance level |
List with estimates, p-values, and confidence intervals
Estimates treatment effects accounting for spillovers
sp_estimation(Y_pre, Y_post, A, verbose = FALSE)sp_estimation(Y_pre, Y_post, A, verbose = FALSE)
Y_pre |
N x T pre-treatment matrix |
Y_post |
N x S post-treatment matrix |
A |
N x k spillover structure matrix |
verbose |
Print progress messages |
List containing treatment effects and other results
Summary method for scm_spillover objects
## S3 method for class 'scm_spillover' summary(object, ...)## S3 method for class 'scm_spillover' summary(object, ...)
object |
An object of class scm_spillover |
... |
Additional arguments (not used) |
Invisibly returns a data frame with detailed results
Computes standard synthetic control for comparison
vanilla_scm(Y_pre, Y_post)vanilla_scm(Y_pre, Y_post)
Y_pre |
N x T pre-treatment matrix |
Y_post |
N x S post-treatment matrix |
Synthetic control values for all periods