<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Yosuke Abe</title>
<link>https://yo5uke.com/en/</link>
<atom:link href="https://yo5uke.com/en/index.xml" rel="self" type="application/rss+xml"/>
<description></description>
<generator>quarto-1.10.19</generator>
<lastBuildDate>Sun, 01 Nov 2026 15:00:00 GMT</lastBuildDate>
<item>
  <title>Implementing the Synthetic Control Method in R with {coresynth}</title>
  <link>https://yo5uke.com/en/posts/coresynth_scm/</link>
  <description><![CDATA[ 






<p><a href="https://CRAN.R-project.org/package=coresynth"><img src="https://www.r-pkg.org/badges/version/coresynth" class="img-fluid" alt="CRAN status"></a> <a href="https://github.com/yo5uke/coresynth"><img src="https://img.shields.io/github/r-package/v/yo5uke/coresynth?label=dev%20version&amp;color=blue" class="img-fluid" alt="dev version"></a> <a href="https://github.com/yo5uke/coresynth/actions/workflows/R-CMD-check.yaml"><img src="https://github.com/yo5uke/coresynth/actions/workflows/R-CMD-check.yaml/badge.svg" class="img-fluid" alt="R-CMD-check"></a> <a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-yellow.svg" class="img-fluid" alt="License: MIT"></a> <a href="https://lifecycle.r-lib.org/articles/stages.html#experimental"><img src="https://img.shields.io/badge/lifecycle-experimental-orange.svg" class="img-fluid" alt="Lifecycle: experimental"></a></p>
<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>The {coresynth} package is an R package I developed to support modern panel data estimators, including the Synthetic Control Method (SCM), Synthetic Difference-in-Differences (SDID), and Matrix Completion. In this post, we focus on the foundational Synthetic Control Method, walking step-by-step through data preparation, estimation, visualization, and placebo-based inference using coresynth.</p>
</section>
<section id="what-is-coresynth" class="level2">
<h2 class="anchored" data-anchor-id="what-is-coresynth">What is {coresynth}?</h2>
<p>coresynth is an open-source R package developed to implement the Synthetic Control Method and related estimators with high computational speed. It is available on CRAN and under active development on GitHub.</p>
<p>The core computational bottlenecks are implemented in C++ via <code>RcppArmadillo</code>. Consequently, coresynth runs significantly faster than traditional pure-R implementations. This speed advantage becomes particularly dramatic when analyzing datasets with large donor pools or running leave-one-out placebo tests across all units.</p>
<p>For benchmark comparison, in a typical empirical setup where the classic {Synth} or {tidysynth} package takes around 15–20 seconds (including placebo runs), coresynth finishes the exact same routine in just a fraction of a second to a few seconds—often achieving substantial speedups depending on the number of predictor variables.</p>
<p>You might naturally wonder whether this performance boost comes at the cost of optimization accuracy. Across extensive multi-faceted validations against existing reference implementations, coresynth solves the underlying constrained convex optimization problem with high fidelity, reliably identifying global optimal weights.<sup>1</sup></p>
</section>
<section id="setup-installation" class="level2">
<h2 class="anchored" data-anchor-id="setup-installation">Setup &amp; Installation</h2>
<p>You can install the released version of coresynth from CRAN, or grab the latest development version directly from GitHub:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Install released version from CRAN</span></span>
<span id="cb1-2">pak<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pak</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"coresynth"</span>)</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Or install development version from GitHub</span></span>
<span id="cb1-5">pak<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pak</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"yo5uke/coresynth"</span>)</span></code></pre></div></div>
<p>Next, let’s load coresynth into your R session. For comparison purposes, we will also load tidysynth and utility packages from the {tidyverse}.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(coresynth)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidysynth)</span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span></code></pre></div></div>
</div>
</section>
<section id="data-california-proposition-99" class="level2">
<h2 class="anchored" data-anchor-id="data-california-proposition-99">Data: California Proposition 99</h2>
<p>To illustrate the workflow, we use the classic <code>smoking</code> dataset included in the tidysynth package. This dataset covers per-capita cigarette sales across 39 US states from 1970 to 2000, investigating the impact of California’s Proposition 99 tobacco control program passed in 1988 <span class="citation" data-cites="abadie_synthetic_2010">(Abadie et al. 2010)</span>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"smoking"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">package =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tidysynth"</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div id="tbl-smoking" class="cell quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl" id="tbl-smoking-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;1: First 6 rows of the smoking dataset
</figcaption>
<div aria-describedby="tbl-smoking-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<div id="qdnamchahz" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#qdnamchahz table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#qdnamchahz thead, #qdnamchahz tbody, #qdnamchahz tfoot, #qdnamchahz tr, #qdnamchahz td, #qdnamchahz th {
  border-style: none;
}

#qdnamchahz p {
  margin: 0;
  padding: 0;
}

#qdnamchahz .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#qdnamchahz .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#qdnamchahz .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#qdnamchahz .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#qdnamchahz .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#qdnamchahz .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#qdnamchahz .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#qdnamchahz .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#qdnamchahz .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#qdnamchahz .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#qdnamchahz .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#qdnamchahz .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#qdnamchahz .gt_spanner_row {
  border-bottom-style: hidden;
}

#qdnamchahz .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#qdnamchahz .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#qdnamchahz .gt_from_md > :first-child {
  margin-top: 0;
}

#qdnamchahz .gt_from_md > :last-child {
  margin-bottom: 0;
}

#qdnamchahz .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#qdnamchahz .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#qdnamchahz .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#qdnamchahz .gt_row_group_first td {
  border-top-width: 2px;
}

#qdnamchahz .gt_row_group_first th {
  border-top-width: 2px;
}

#qdnamchahz .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#qdnamchahz .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#qdnamchahz .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#qdnamchahz .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#qdnamchahz .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#qdnamchahz .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#qdnamchahz .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#qdnamchahz .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#qdnamchahz .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#qdnamchahz .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#qdnamchahz .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#qdnamchahz .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#qdnamchahz .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#qdnamchahz .gt_left {
  text-align: left;
}

#qdnamchahz .gt_center {
  text-align: center;
}

#qdnamchahz .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#qdnamchahz .gt_font_normal {
  font-weight: normal;
}

#qdnamchahz .gt_font_bold {
  font-weight: bold;
}

#qdnamchahz .gt_font_italic {
  font-style: italic;
}

#qdnamchahz .gt_super {
  font-size: 65%;
}

#qdnamchahz .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#qdnamchahz .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#qdnamchahz .gt_indent_1 {
  text-indent: 5px;
}

#qdnamchahz .gt_indent_2 {
  text-indent: 10px;
}

#qdnamchahz .gt_indent_3 {
  text-indent: 15px;
}

#qdnamchahz .gt_indent_4 {
  text-indent: 20px;
}

#qdnamchahz .gt_indent_5 {
  text-indent: 25px;
}

#qdnamchahz .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#qdnamchahz div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>

<table class="gt_table cell caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<thead>
<tr class="gt_col_headings header">
<th id="state" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">state</th>
<th id="year" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">year</th>
<th id="cigsale" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">cigsale</th>
<th id="lnincome" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">lnincome</th>
<th id="beer" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">beer</th>
<th id="age15to24" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">age15to24</th>
<th id="retprice" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">retprice</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_left" headers="state">Rhode Island</td>
<td class="gt_row gt_right" headers="year">1970</td>
<td class="gt_row gt_right" headers="cigsale">123.9</td>
<td class="gt_row gt_right" headers="lnincome">NA</td>
<td class="gt_row gt_right" headers="beer">NA</td>
<td class="gt_row gt_right" headers="age15to24">0.1831579</td>
<td class="gt_row gt_right" headers="retprice">39.3</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="state">Tennessee</td>
<td class="gt_row gt_right" headers="year">1970</td>
<td class="gt_row gt_right" headers="cigsale">99.8</td>
<td class="gt_row gt_right" headers="lnincome">NA</td>
<td class="gt_row gt_right" headers="beer">NA</td>
<td class="gt_row gt_right" headers="age15to24">0.1780438</td>
<td class="gt_row gt_right" headers="retprice">39.9</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="state">Indiana</td>
<td class="gt_row gt_right" headers="year">1970</td>
<td class="gt_row gt_right" headers="cigsale">134.6</td>
<td class="gt_row gt_right" headers="lnincome">NA</td>
<td class="gt_row gt_right" headers="beer">NA</td>
<td class="gt_row gt_right" headers="age15to24">0.1765159</td>
<td class="gt_row gt_right" headers="retprice">30.6</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="state">Nevada</td>
<td class="gt_row gt_right" headers="year">1970</td>
<td class="gt_row gt_right" headers="cigsale">189.5</td>
<td class="gt_row gt_right" headers="lnincome">NA</td>
<td class="gt_row gt_right" headers="beer">NA</td>
<td class="gt_row gt_right" headers="age15to24">0.1615542</td>
<td class="gt_row gt_right" headers="retprice">38.9</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="state">Louisiana</td>
<td class="gt_row gt_right" headers="year">1970</td>
<td class="gt_row gt_right" headers="cigsale">115.9</td>
<td class="gt_row gt_right" headers="lnincome">NA</td>
<td class="gt_row gt_right" headers="beer">NA</td>
<td class="gt_row gt_right" headers="age15to24">0.1851852</td>
<td class="gt_row gt_right" headers="retprice">34.3</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="state">Oklahoma</td>
<td class="gt_row gt_right" headers="year">1970</td>
<td class="gt_row gt_right" headers="cigsale">108.4</td>
<td class="gt_row gt_right" headers="lnincome">NA</td>
<td class="gt_row gt_right" headers="beer">NA</td>
<td class="gt_row gt_right" headers="age15to24">0.1754592</td>
<td class="gt_row gt_right" headers="retprice">38.4</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
</figure>
</div>
</div>
</section>
<section id="estimation-with-scm_fit" class="level2">
<h2 class="anchored" data-anchor-id="estimation-with-scm_fit">Estimation with <code>scm_fit()</code></h2>
<p>In coresynth, all supported panel estimators are unified under a single primary function: <code>scm_fit()</code>. To run the Synthetic Control Method, specify <code>method = "scm"</code>.</p>
<section id="constructing-the-treatment-indicator" class="level3">
<h3 class="anchored" data-anchor-id="constructing-the-treatment-indicator">Constructing the Treatment Indicator</h3>
<p><code>scm_fit()</code> requires a binary treatment indicator that takes the value <code>1</code> for the treated unit in the post-treatment period, and <code>0</code> otherwise.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">smoking <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> smoking <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb4-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">treated =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(state <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"California"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1989</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>L, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>L)</span>
<span id="cb4-4">  )</span></code></pre></div></div>
</div>
<p>In this case, California passed Proposition 99 in late 1988, so <code>treated</code> equals <code>1</code> for California starting in 1989.</p>
</section>
<section id="model-formula-predictor-syntax" class="level3">
<h3 class="anchored" data-anchor-id="model-formula-predictor-syntax">Model Formula &amp; Predictor Syntax</h3>
<p>Following the two-way fixed effects syntax common in packages like {fixest}, the formula is specified as <code>outcome ~ treated | unit + time</code>.</p>
<p>Pre-treatment predictor variables are specified using the <code>predictors</code> argument as a list of <code>pred()</code> specifications. The <code>pred()</code> function takes the predictor column name(s) and the corresponding pre-treatment time window over which to aggregate them.</p>
<p>To benchmark the runtime, let’s time the complete estimation including all-donor placebo calculations using the {tictoc} package:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">{coresynth}</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">{tidysynth}</a></li></ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<p>Here we run <code>scm_fit()</code> and immediately calculate the full placebo distribution via <code>mspe_ratio_pval()</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">core_fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scm_fit</span>(</span>
<span id="cb5-2">  cigsale <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> treated <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> state <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> year,</span>
<span id="cb5-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> smoking,</span>
<span id="cb5-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"scm"</span>,</span>
<span id="cb5-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">predictors =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb5-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pred</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lnincome"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"retprice"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age15to24"</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1980</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1988</span>),</span>
<span id="cb5-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pred</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cigsale"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1980</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1988</span>),</span>
<span id="cb5-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pred</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"beer"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1984</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1988</span>),</span>
<span id="cb5-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pred</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cigsale"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1975</span>),</span>
<span id="cb5-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pred</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cigsale"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1980</span>),</span>
<span id="cb5-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pred</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cigsale"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1988</span>)</span>
<span id="cb5-12">  )</span>
<span id="cb5-13">)</span>
<span id="cb5-14">core_placebo <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mspe_ratio_pval</span>(core_fit) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate placebos as well</span></span></code></pre></div></div>
<div class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>2.37 sec elapsed</code></pre>
</div>
</div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<p>For comparison, here is the equivalent specification implemented in tidysynth:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">smoking_out <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> smoking <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">synthetic_control</span>(</span>
<span id="cb7-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outcome =</span> cigsale,</span>
<span id="cb7-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">unit =</span> state,</span>
<span id="cb7-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> year,</span>
<span id="cb7-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">i_unit =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"California"</span>,</span>
<span id="cb7-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">i_time =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1988</span>,</span>
<span id="cb7-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">generate_placebos =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span></span>
<span id="cb7-9">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">generate_predictor</span>(</span>
<span id="cb7-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time_window =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1980</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1988</span>,</span>
<span id="cb7-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ln_income =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(lnincome, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb7-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ret_price =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(retprice, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb7-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">youth =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(age15to24, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb7-15">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">generate_predictor</span>(</span>
<span id="cb7-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time_window =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1980</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1988</span>,</span>
<span id="cb7-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ln_cigsale =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(cigsale, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb7-19">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">generate_predictor</span>(</span>
<span id="cb7-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time_window =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1984</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1988</span>,</span>
<span id="cb7-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">beer_sales =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(beer, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb7-23">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">generate_predictor</span>(</span>
<span id="cb7-25">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time_window =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1975</span>,</span>
<span id="cb7-26">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cigsale_1975 =</span> cigsale</span>
<span id="cb7-27">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">generate_predictor</span>(</span>
<span id="cb7-29">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time_window =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1980</span>,</span>
<span id="cb7-30">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cigsale_1980 =</span> cigsale</span>
<span id="cb7-31">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">generate_predictor</span>(</span>
<span id="cb7-33">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time_window =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1988</span>,</span>
<span id="cb7-34">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cigsale_1988 =</span> cigsale</span>
<span id="cb7-35">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-36">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">generate_weights</span>(</span>
<span id="cb7-37">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">optimization_window =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1970</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1988</span>,</span>
<span id="cb7-38">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">margin_ipop =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">02</span>,</span>
<span id="cb7-39">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigf_ipop =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>,</span>
<span id="cb7-40">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bound_ipop =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span></span>
<span id="cb7-41">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-42">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">generate_control</span>()</span></code></pre></div></div>
<div class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>16.906 sec elapsed</code></pre>
</div>
</div>
</div>
</div>
</div>
<p>In this comparison, coresynth finishes in <strong>2.37 seconds</strong>, whereas tidysynth takes <strong>16.91 seconds</strong>—a <strong>7.1× speedup</strong>.</p>
<p>When running extensive sensitivity analyses, tuning hyperparameters, or testing multiple candidate donor pools, waiting 20 seconds per run quickly adds up. This efficiency gain was a core motivation behind developing coresynth.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Specifying Non-Annual Time Horizons in <code>pred()</code>
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>The second argument of <code>pred()</code> takes a vector of time points. For annual data, integer sequences like <code>1980:1988</code> work naturally. For monthly or daily data, you can pass a sequence of Date objects using <code>seq()</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Example for monthly panel data</span></span>
<span id="cb9-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2020-01-01"</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2020-12-01"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"month"</span>)</span></code></pre></div></div>
</div>
</div>
</div>
</section>
</section>
<section id="visualizing-trends-and-gaps" class="level2">
<h2 class="anchored" data-anchor-id="visualizing-trends-and-gaps">Visualizing Trends and Gaps</h2>
<p>coresynth objects come with a unified <code>plot()</code> method returning clean, publication-ready ggplot2 figures.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-2-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-1" aria-controls="tabset-2-1" aria-selected="true" href="">Trajectory Plot</a></li><li class="nav-item"><a class="nav-link" id="tabset-2-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-2" aria-controls="tabset-2-2" aria-selected="false" href="">Gap Plot</a></li></ul>
<div class="tab-content">
<div id="tabset-2-1" class="tab-pane active" aria-labelledby="tabset-2-1-tab">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(core_fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trend"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://yo5uke.com/en/posts/coresynth_scm/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The trajectory plot displays the actual outcome series for the treated unit alongside the estimated synthetic control counterpart. Because <code>type = "trend"</code> is the default, calling <code>plot(core_fit)</code> yields the same figure.</p>
</div>
<div id="tabset-2-2" class="tab-pane" aria-labelledby="tabset-2-2-tab">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(core_fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gap"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://yo5uke.com/en/posts/coresynth_scm/index_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The gap plot shows the difference between the treated unit and its synthetic control over time (<img src="https://latex.codecogs.com/png.latex?Y_%7B1t%7D%20-%20%5Cwidehat%7BY%7D_%7B1t%7D%5E%7B%5Ctext%7Bsynth%7D%7D">), making post-treatment treatment effects visually striking.</p>
</div>
</div>
</div>
<section id="customizing-plots-with-ggplot2" class="level3">
<h3 class="anchored" data-anchor-id="customizing-plots-with-ggplot2">Customizing Plots with ggplot2</h3>
<p>Because coresynth plots are standard <code>ggplot</code> objects, you can customize labels, colors, and themes effortlessly with standard ggplot2 syntax:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(</span>
<span id="cb12-2">  core_fit,</span>
<span id="cb12-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colors =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">treated =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkgreen"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">synthetic =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkred"</span>),</span>
<span id="cb12-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">treated =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"California"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">synthetic =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Synthetic California"</span>),</span>
<span id="cb12-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vline =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>),</span>
<span id="cb12-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vline_offset =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb12-7">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb12-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Effect of Proposition 99 on Cigarette Consumption"</span>,</span>
<span id="cb12-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb12-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Packs per Capita"</span></span>
<span id="cb12-12">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://yo5uke.com/en/posts/coresynth_scm/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Arguments like <code>colors</code> and <code>labels</code> modify aesthetic mappings directly, while <code>vline</code> and <code>vline_offset</code> provide flexible control over the intervention vertical marker.</p>
</section>
</section>
<section id="inspecting-weights-and-predictor-balance" class="level2">
<h2 class="anchored" data-anchor-id="inspecting-weights-and-predictor-balance">Inspecting Weights and Predictor Balance</h2>
<p>Understanding how the synthetic counterfactual was constructed is central to any SCM analysis. Calling <code>summary()</code> on a fitted object displays the donor unit weights (<img src="https://latex.codecogs.com/png.latex?W">) and the predictor balance table:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(core_fit)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>=== coresynth summary ===
Method : SCM 
Periods : T_pre = 19 | T_post = 12 
ATT estimate: -18.97965 
Unit weights (non-zero donors):
   Colorado Connecticut     Montana      Nevada        Utah 
     0.1590      0.0677      0.2022      0.2357      0.3353 

Predictor balance:
            predictor  treated synthetic
  lnincome[1980:1988]  10.0766    9.8583
  retprice[1980:1988]  89.4222   89.4222
 age15to24[1980:1988]   0.1735    0.1735
   cigsale[1980:1988] 106.6556  107.4008
      beer[1984:1988]  24.2800   24.2237
        cigsale[1975] 127.1000  127.1000
        cigsale[1980] 120.2000  120.4667
        cigsale[1988]  90.1000   91.6480</code></pre>
</div>
</div>
<p>You can also visualize the donor unit weights and predictor importance weights (<img src="https://latex.codecogs.com/png.latex?V">) graphically:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(core_fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"weights"</span>)</span>
<span id="cb15-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(core_fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pred_weights"</span>)</span></code></pre></div></div>
<div class="cell quarto-layout-panel" data-layout-ncol="2">
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://yo5uke.com/en/posts/coresynth_scm/index_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid" width="672"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://yo5uke.com/en/posts/coresynth_scm/index_files/figure-html/unnamed-chunk-10-2.png" class="img-fluid" width="672"></p>
</div>
</div>
</div>
<section id="extracting-tidy-data-via-plot_data" class="level3">
<h3 class="anchored" data-anchor-id="extracting-tidy-data-via-plot_data">Extracting Tidy Data via <code>plot_data()</code></h3>
<p>If you prefer building fully customized visualizations from raw numbers, coresynth provides the helper function <code>plot_data()</code> to extract data frames directly:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-3-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-3-1" aria-controls="tabset-3-1" aria-selected="true" href="">Trajectory Data</a></li><li class="nav-item"><a class="nav-link" id="tabset-3-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-3-2" aria-controls="tabset-3-2" aria-selected="false" href="">Weights Data</a></li></ul>
<div class="tab-content">
<div id="tabset-3-1" class="tab-pane active" aria-labelledby="tabset-3-1-tab">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_data</span>(core_fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trend"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  time value  series
1 1970 123.0 Treated
2 1971 121.0 Treated
3 1972 123.5 Treated
4 1973 124.4 Treated
5 1974 126.7 Treated
6 1975 127.1 Treated</code></pre>
</div>
</div>
<p>The resulting data frame includes columns for unit, time, series (<code>treated</code> vs <code>synthetic</code>), and outcome values.</p>
</div>
<div id="tabset-3-2" class="tab-pane" aria-labelledby="tabset-3-2-tab">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_data</span>(core_fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"weights"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb18-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(weight)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb18-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>         unit     weight
1        Utah 0.33525118
2      Nevada 0.23572876
3     Montana 0.20224658
4    Colorado 0.15904083
5 Connecticut 0.06773264
6     Alabama 0.00000000</code></pre>
</div>
</div>
<p>This makes it easy to rank-order and inspect positive donor contributions.</p>
</div>
</div>
</div>
</section>
</section>
<section id="inference-via-placebo-tests" class="level2">
<h2 class="anchored" data-anchor-id="inference-via-placebo-tests">Inference via Placebo Tests</h2>
<p>Statistical inference in SCM typically relies on in-space placebo tests <span class="citation" data-cites="abadie_synthetic_2010">(Abadie et al. 2010)</span>. In this framework, treatment is iteratively reassigned to each donor in the pool to compute an exact <img src="https://latex.codecogs.com/png.latex?p">-value based on the distribution of post/pre-treatment Mean Squared Prediction Error (MSPE) ratios. coresynth provides the <code>mspe_ratio_pval()</code> function to conduct this procedure automatically.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">core_placebo <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mspe_ratio_pval</span>(core_fit)</span>
<span id="cb20-2">core_placebo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p_value</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.07692308</code></pre>
</div>
</div>
<p>The resulting exact <img src="https://latex.codecogs.com/png.latex?p">-value can be accessed directly from <code>core_placebo$p_value</code>.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Why Results May Slightly Differ from Other Packages
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>When comparing results across SCM software, you may notice subtle numerical differences in the optimal weights or the resulting <img src="https://latex.codecogs.com/png.latex?p">-values.</p>
<p>In the standard Synthetic Control Method, the outer optimization selects predictor weights (<img src="https://latex.codecogs.com/png.latex?V">) to minimize the pre-treatment prediction error. Because this problem possesses an intricate, non-convex outer landscape with many local minima, different solvers often converge to slightly different weight vectors. coresynth uses an advanced multi-start optimization routine with C++ acceleration, often discovering solutions with strictly lower pre-treatment MSE than traditional single-start approaches. The numerical discrepancy reflects finding a better local or global optimum rather than a numerical error.</p>
</div>
</div>
</div>
<section id="plotting-placebo-gaps" class="level3">
<h3 class="anchored" data-anchor-id="plotting-placebo-gaps">Plotting Placebo Gaps</h3>
<p>You can plot the gap trajectories of California alongside all placebo donors:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-4-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-4-1" aria-controls="tabset-4-1" aria-selected="true" href="">All Placebos</a></li><li class="nav-item"><a class="nav-link" id="tabset-4-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-4-2" aria-controls="tabset-4-2" aria-selected="false" href="">Pruning Poor Pre-Treatment Fits</a></li></ul>
<div class="tab-content">
<div id="tabset-4-1" class="tab-pane active" aria-labelledby="tabset-4-1-tab">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(core_placebo, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gaps"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://yo5uke.com/en/posts/coresynth_scm/index_files/figure-html/unnamed-chunk-14-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The gap trajectories for all donor units are shown in light gray, contrasting with the dark line representing the treated unit.</p>
</div>
<div id="tabset-4-2" class="tab-pane" aria-labelledby="tabset-4-2-tab">
<p><span class="citation" data-cites="abadie_synthetic_2010">Abadie et al. (2010)</span> recommend pruning placebo donors whose pre-treatment MSPE is substantially higher than that of the treated unit. Use the <code>mspe_prune</code> argument to set a threshold cutoff:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(core_placebo, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gaps"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vline_offset =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mspe_prune =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://yo5uke.com/en/posts/coresynth_scm/index_files/figure-html/unnamed-chunk-15-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Setting <code>mspe_prune = 2</code> removes all control states with pre-treatment MSPE more than twice California’s, highlighting meaningful counterfactual paths.</p>
</div>
</div>
</div>
</section>
</section>
<section id="useful-extras" class="level2">
<h2 class="anchored" data-anchor-id="useful-extras">Useful Extras</h2>
<p>coresynth provides several convenient helpers for extracting vectors or tidying outputs:</p>
<section id="extracting-outcome-vectors" class="level3">
<h3 class="anchored" data-anchor-id="extracting-outcome-vectors">Extracting Outcome Vectors</h3>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">treated_outcomes</span>(core_fit) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 123.0 121.0 123.5 124.4 126.7 127.1</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">synthetic_outcomes</span>(core_fit) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 117.0956 118.9075 124.2976 125.4564 126.9863 127.1000</code></pre>
</div>
</div>
<p>These functions allow you to directly inspect the observed and synthetic time series as standard numeric vectors.</p>
</section>
<section id="tidying-with-broomaugment" class="level3">
<h3 class="anchored" data-anchor-id="tidying-with-broomaugment">Tidying with <code>broom::augment()</code></h3>
<p>By integrating with the {broom} ecosystem, you can convert the entire fitted trajectory into a tidy data frame:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1">fit_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> broom<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">augment</span>(core_fit)</span>
<span id="cb28-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(fit_df)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  .time .observed  .fitted        .resid .treated .period
1  1970     123.0 117.0956  5.904415e+00    FALSE     pre
2  1971     121.0 118.9075  2.092479e+00    FALSE     pre
3  1972     123.5 124.2976 -7.976368e-01    FALSE     pre
4  1973     124.4 125.4564 -1.056377e+00    FALSE     pre
5  1974     126.7 126.9863 -2.862995e-01    FALSE     pre
6  1975     127.1 127.1000  2.904875e-05    FALSE     pre</code></pre>
</div>
</div>
<p>The returned tibble includes indicator columns for the pre- and post-treatment regimes, making downstream analysis or custom plotting straightforward.</p>
</section>
</section>
<section id="summary-feedback" class="level2">
<h2 class="anchored" data-anchor-id="summary-feedback">Summary &amp; Feedback</h2>
<p>coresynth is built from the ground up to bring unprecedented speed and a clean, unified syntax to Synthetic Control methods in R without sacrificing optimization precision.</p>
<p>Whether you are conducting classroom demonstrations, empirical policy evaluations, or large-scale simulation benchmarks, we hope coresynth accelerates your research workflow.</p>
<p>If you encounter any issues, have questions, or wish to request new features, please feel free to open an issue on the <a href="https://github.com/yo5uke/coresynth/issues">GitHub Repository</a> or leave a comment below!</p>


<!-- -->


</section>


<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-abadie_synthetic_2010" class="csl-entry">
Abadie, Alberto, Alexis Diamond, and Jens Hainmueller. 2010. "Synthetic Control Methods for Comparative Case Studies: Estimating the Effect of <span>California</span>’s Tobacco Control Program". <em>Journal of the American Statistical Association</em> 105 (490): 493–505. <a href="https://doi.org/10.1198/jasa.2009.ap08746">https://doi.org/10.1198/jasa.2009.ap08746</a>.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">注</h2>

<ol>
<li id="fn1"><p>Strictly speaking, while both packages solve the same mathematical optimization problem, slight algorithmic differences in the search procedure can produce minor numerical variations. However, both target the identical objective function, and the resulting estimates are equally valid.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>R</category>
  <guid>https://yo5uke.com/en/posts/coresynth_scm/</guid>
  <pubDate>Sun, 01 Nov 2026 15:00:00 GMT</pubDate>
  <media:content url="https://yo5uke.com/en/posts/coresynth_scm/image/thumbnail.png" medium="image" type="image/png" height="72" width="144"/>
</item>
</channel>
</rss>
