alpha[I];
+ vector[sumN] eta[I];
+}
+
+transformed parameters {
+ vector[sumN] f[I];
+ for(i in 1:I){
+ matrix[sumN,sumN] K_matrix = cov_exp_quad(time,alpha[i],rho[i]) +
+ diag_matrix(rep_vector(1e-9, sumN));
+ matrix[sumN,sumN] L_K = cholesky_decompose(K_matrix);
+ f[i] = L_K * eta[i];
+ }
+}
+
+model {
+ for(i in 1:I){
+ sigma[i] ~ normal(0,1);
+ rho[i] ~ normal(0,3);
+ alpha[i] ~ normal(0,1);
+ eta[i] ~ normal(0,1);
+ target += normal_lpdf(y_std[i] | X_std*beta[i] +
+ f[i][1:N], sigma[i]);
+ }
+}
+
+generated quantities {
+ vector[N] y_sim[I];
+ vector[N_pred] y_pred[I];
+ for(i in 1:I){
+ for (n in 1:N) {
+ y_sim[i][n] = normal_rng(X_std[n,]*beta[i] +
+ f[i][n], sigma[i]) * sd_y[i] + mean_y[i]; //
+ }
+ for (j in 1:N_pred) {
+ y_pred[i][j] = normal_rng(X_pred_std[j,]*beta[i] +
+ f[i][N + j], sigma[i]) * sd_y[i] +
+ mean_y[i]; //
+ }
+ }
+}
diff --git a/man/bayesianFactor.Rd b/man/bayesianFactor.Rd
new file mode 100644
index 0000000..0d7a761
--- /dev/null
+++ b/man/bayesianFactor.Rd
@@ -0,0 +1,260 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/bfactor.R
+\name{bayesianFactor}
+\alias{bayesianFactor}
+\title{Create a Bayesian Synthetic Control Object Using Panel Data}
+\value{
+vizdraws object with the relative bias with offset.
+}
+\description{
+A Bayesian Factor Model has raw data and draws from the posterior
+distribution. This is represented by an R6 Class.
+
+Code and theory based on
+\href{https://arxiv.org/abs/2103.16244}{Pinkney 2021}.
+
+public methods:
+\itemize{
+\item \code{initialize()} initializes the variables and model parameters
+\item \code{fit()} fits the stan model and returns a fit object
+\item \code{updateWidth} updates the width of the credible interval
+\item \code{placeboPlot} generates a counterfactual placebo plot
+\item \code{effectPlot} returns a plot of the treatment effect over time
+\item \code{summarizeLift}returns descriptive statistics of the lift estimate
+\item \code{biasDraws} returns a plot of the relative bias in a LFM
+\item \code{liftDraws} returns a plot of the posterior lift distribution
+\item \code{liftBias} returns a plot of the relative bias given a lift offset
+}
+}
+\section{Active bindings}{
+\if{html}{\out{}}
+\describe{
+\item{\code{timeTiles}}{ggplot2 object that shows when
+the intervention happened.}
+
+\item{\code{plotData}}{tibble with the observed outcome and the
+counterfactual data.}
+
+\item{\code{interventionTime}}{returns the intervention time period.}
+
+\item{\code{synthetic}}{ggplot2 object that shows the
+observed and counterfactual outcomes over time.}
+}
+\if{html}{\out{
}}
+}
+\section{Methods}{
+\subsection{Public methods}{
+\itemize{
+\item \href{#method-new}{\code{bayesianFactor$new()}}
+\item \href{#method-fit}{\code{bayesianFactor$fit()}}
+\item \href{#method-updateWidth}{\code{bayesianFactor$updateWidth()}}
+\item \href{#method-summarizeLift}{\code{bayesianFactor$summarizeLift()}}
+\item \href{#method-effectPlot}{\code{bayesianFactor$effectPlot()}}
+\item \href{#method-liftDraws}{\code{bayesianFactor$liftDraws()}}
+\item \href{#method-liftBias}{\code{bayesianFactor$liftBias()}}
+\item \href{#method-biasDraws}{\code{bayesianFactor$biasDraws()}}
+\item \href{#method-clone}{\code{bayesianFactor$clone()}}
+}
+}
+\if{html}{\out{
}}
+\if{html}{\out{}}
+\if{latex}{\out{\hypertarget{method-new}{}}}
+\subsection{Method \code{new()}}{
+Create a new bayesianFactor object.
+\subsection{Usage}{
+\if{html}{\out{}}\preformatted{bayesianFactor$new(
+ data,
+ time,
+ id,
+ treated,
+ outcome,
+ ci_width = 0.75,
+ covariates
+)}\if{html}{\out{
}}
+}
+
+\subsection{Arguments}{
+\if{html}{\out{}}
+\describe{
+\item{\code{data}}{Long data.frame object with fields outcome, time, id,
+and treatment indicator.}
+
+\item{\code{time}}{Name of the variable in the data frame that}
+
+\item{\code{id}}{Name of the variable in the data frame that
+identifies the units (e.g. country, region etc).}
+
+\item{\code{treated}}{Name of the variable in the data frame that contains the
+treatment assignment of the intervention.}
+
+\item{\code{outcome}}{Name of the outcome variable.}
+
+\item{\code{ci_width}}{Credible interval's width. This number is in the
+(0,1) interval.}
+
+\item{\code{covariates}}{Dataframe with a column for {id} and the other columns
+Defaults to NULL if no covariates should be included in the model.}
+}
+\if{html}{\out{
}}
+}
+\subsection{Details}{
+params described in the data structure section of the
+documentation of the R6 class at the top of the file.
+}
+
+\subsection{Returns}{
+A new \code{bayesianFactor} object.
+}
+}
+\if{html}{\out{
}}
+\if{html}{\out{}}
+\if{latex}{\out{\hypertarget{method-fit}{}}}
+\subsection{Method \code{fit()}}{
+Fit Stan model.
+\subsection{Usage}{
+\if{html}{\out{}}\preformatted{bayesianFactor$fit(L = 8, ...)}\if{html}{\out{
}}
+}
+
+\subsection{Arguments}{
+\if{html}{\out{}}
+\describe{
+\item{\code{L}}{Number of factors.}
+
+\item{\code{...}}{other arguments passed to \code{\link[rstan:stanmodel-method-sampling]{rstan::sampling()}}.}
+}
+\if{html}{\out{
}}
+}
+}
+\if{html}{\out{
}}
+\if{html}{\out{}}
+\if{latex}{\out{\hypertarget{method-updateWidth}{}}}
+\subsection{Method \code{updateWidth()}}{
+Update the width of the credible interval.
+\subsection{Usage}{
+\if{html}{\out{}}\preformatted{bayesianFactor$updateWidth(ci_width = 0.75)}\if{html}{\out{
}}
+}
+
+\subsection{Arguments}{
+\if{html}{\out{}}
+\describe{
+\item{\code{ci_width}}{New width for the credible interval. This number
+should be in the (0,1) interval.}
+}
+\if{html}{\out{
}}
+}
+}
+\if{html}{\out{
}}
+\if{html}{\out{}}
+\if{latex}{\out{\hypertarget{method-summarizeLift}{}}}
+\subsection{Method \code{summarizeLift()}}{
+summarizeLift returns descriptive statistics of
+the lift estimate.
+\subsection{Usage}{
+\if{html}{\out{}}\preformatted{bayesianFactor$summarizeLift()}\if{html}{\out{
}}
+}
+
+}
+\if{html}{\out{
}}
+\if{html}{\out{}}
+\if{latex}{\out{\hypertarget{method-effectPlot}{}}}
+\subsection{Method \code{effectPlot()}}{
+effectPlot returns ggplot2 object that shows the
+effect of the intervention over time.
+\subsection{Usage}{
+\if{html}{\out{}}\preformatted{bayesianFactor$effectPlot()}\if{html}{\out{
}}
+}
+
+}
+\if{html}{\out{
}}
+\if{html}{\out{}}
+\if{latex}{\out{\hypertarget{method-liftDraws}{}}}
+\subsection{Method \code{liftDraws()}}{
+Plots lift.
+\subsection{Usage}{
+\if{html}{\out{}}\preformatted{bayesianFactor$liftDraws(from, to, ...)}\if{html}{\out{
}}
+}
+
+\subsection{Arguments}{
+\if{html}{\out{}}
+\describe{
+\item{\code{from}}{First period to consider when calculating lift. If infinite,
+set to the time of the intervention.}
+
+\item{\code{to}}{Last period to consider when calculating lift. If infinite, set
+to the last period.}
+
+\item{\code{...}}{other arguments passed to vizdraws::vizdraws().}
+}
+\if{html}{\out{
}}
+}
+\subsection{Returns}{
+vizdraws object with the posterior distribution of the lift.
+}
+}
+\if{html}{\out{
}}
+\if{html}{\out{}}
+\if{latex}{\out{\hypertarget{method-liftBias}{}}}
+\subsection{Method \code{liftBias()}}{
+Plot bias magnitude in terms of lift for period \link{firstT, lastT}
+\subsection{Usage}{
+\if{html}{\out{}}\preformatted{bayesianFactor$liftBias(firstT, lastT, offset, ...)}\if{html}{\out{
}}
+}
+
+\subsection{Arguments}{
+\if{html}{\out{}}
+\describe{
+\item{\code{firstT}}{start of the time period to compute relative bias
+over. They must be after the intervention.}
+
+\item{\code{lastT}}{end of the Time period to compute relative bias
+over. They must be after the intervention.}
+
+\item{\code{offset}}{Target lift \%.}
+
+\item{\code{...}}{other arguments passed to vizdraws::vizdraws().}
+}
+\if{html}{\out{
}}
+}
+}
+\if{html}{\out{
}}
+\if{html}{\out{}}
+\if{latex}{\out{\hypertarget{method-biasDraws}{}}}
+\subsection{Method \code{biasDraws()}}{
+Plots relative upper bias / tau for a time period \link{firstT, lastT}.
+\subsection{Usage}{
+\if{html}{\out{}}\preformatted{bayesianFactor$biasDraws(small_bias = 0.3, firstT, lastT)}\if{html}{\out{
}}
+}
+
+\subsection{Arguments}{
+\if{html}{\out{}}
+\describe{
+\item{\code{small_bias}}{Threshold value for considering the bias "small".}
+
+\item{\code{firstT, lastT}}{Time periods to compute relative bias over, they must
+after the intervention.}
+}
+\if{html}{\out{
}}
+}
+\subsection{Returns}{
+vizdraw object with the posterior distribution of relative bias.
+Bias is scaled by the time periods.
+}
+}
+\if{html}{\out{
}}
+\if{html}{\out{}}
+\if{latex}{\out{\hypertarget{method-clone}{}}}
+\subsection{Method \code{clone()}}{
+The objects of this class are cloneable with this method.
+\subsection{Usage}{
+\if{html}{\out{}}\preformatted{bayesianFactor$clone(deep = FALSE)}\if{html}{\out{
}}
+}
+
+\subsection{Arguments}{
+\if{html}{\out{}}
+\describe{
+\item{\code{deep}}{Whether to make a deep clone.}
+}
+\if{html}{\out{
}}
+}
+}
+}
diff --git a/man/bayesianSynth.Rd b/man/bayesianSynth.Rd
new file mode 100644
index 0000000..32bc8ba
--- /dev/null
+++ b/man/bayesianSynth.Rd
@@ -0,0 +1,314 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/factory.R
+\name{bayesianSynth}
+\alias{bayesianSynth}
+\title{Create a Bayesian Synthetic Control Object Using Panel Data}
+\value{
+vizdraws object with the relative bias with offset.
+}
+\description{
+A Bayesian Synthetic Control has raw data and draws from the posterior
+distribution. This is represented by an R6 Class.
+
+public methods:
+\itemize{
+\item \code{initialize()} initializes the variables and model parameters
+\item \code{fit()} fits the stan model and returns a fit object
+\item \code{updateWidth} updates the width of the credible interval
+\item \code{placeboPlot} generates a counterfactual placebo plot
+\item \code{effectPlot} returns a plot of the treatment effect over time
+\item \code{summarizeLift}returns descriptive statistics of the lift estimate
+\item \code{biasDraws} returns a plot of the relative bias in a LFM
+\item \code{liftDraws} returns a plot of the posterior lift distribution
+\item \code{liftBias} returns a plot of the relative bias given a lift offset
+Data structure:
+}
+}
+\section{Active bindings}{
+\if{html}{\out{}}
+\describe{
+\item{\code{timeTiles}}{ggplot2 object that shows when
+the intervention happened.}
+
+\item{\code{plotData}}{returns tibble with the observed outcome and the
+counterfactual data.}
+
+\item{\code{interventionTime}}{returns intervention time period (e.g., year)
+in which the treatment occurred.}
+
+\item{\code{synthetic}}{returns ggplot2 object that shows the
+observed and counterfactual outcomes over time.}
+
+\item{\code{checks}}{returns MCMC checks.}
+
+\item{\code{lift}}{draws from the posterior distribution of the lift.}
+}
+\if{html}{\out{
}}
+}
+\section{Methods}{
+\subsection{Public methods}{
+\itemize{
+\item \href{#method-new}{\code{bayesianSynth$new()}}
+\item \href{#method-fit}{\code{bayesianSynth$fit()}}
+\item \href{#method-updateWidth}{\code{bayesianSynth$updateWidth()}}
+\item \href{#method-summarizeLift}{\code{bayesianSynth$summarizeLift()}}
+\item \href{#method-effectPlot}{\code{bayesianSynth$effectPlot()}}
+\item \href{#method-placeboPlot}{\code{bayesianSynth$placeboPlot()}}
+\item \href{#method-biasDraws}{\code{bayesianSynth$biasDraws()}}
+\item \href{#method-liftDraws}{\code{bayesianSynth$liftDraws()}}
+\item \href{#method-liftBias}{\code{bayesianSynth$liftBias()}}
+\item \href{#method-clone}{\code{bayesianSynth$clone()}}
+}
+}
+\if{html}{\out{
}}
+\if{html}{\out{}}
+\if{latex}{\out{\hypertarget{method-new}{}}}
+\subsection{Method \code{new()}}{
+Create a new bayesianSynth object.
+\subsection{Usage}{
+\if{html}{\out{}}\preformatted{bayesianSynth$new(
+ data,
+ time,
+ id,
+ treated,
+ outcome,
+ ci_width = 0.75,
+ gp = FALSE,
+ covariates = NULL,
+ predictor_match = FALSE,
+ predictor_match_covariates0 = NULL,
+ predictor_match_covariates1 = NULL,
+ vs = NULL
+)}\if{html}{\out{
}}
+}
+
+\subsection{Arguments}{
+\if{html}{\out{}}
+\describe{
+\item{\code{data}}{Long data.frame object with fields outcome, time, id,
+and treatment indicator.}
+
+\item{\code{time}}{Name of the variable in the data frame that
+identifies the time period (e.g. year, month, week etc).}
+
+\item{\code{id}}{Name of the variable in the data frame that
+identifies the units (e.g. country, region etc).}
+
+\item{\code{treated}}{Name of the variable in the data frame that contains
+the treatment assignment of the intervention.}
+
+\item{\code{outcome}}{Name of the outcome variable.}
+
+\item{\code{ci_width}}{Credible interval's width. This number is in the
+\link{0,1} interval.}
+
+\item{\code{gp}}{Logical that indicates whether or not to include a
+Gaussian Process as part of the model.}
+
+\item{\code{covariates}}{Data.frame with time dependent covariates for
+for each unit and time field.
+Defaults to NULL if no covariates should be included in the model.}
+
+\item{\code{predictor_match}}{Logical that indicates whether or not to run
+the matching version of the Bayesian Synthetic Control. This option can
+not be used with gp, covariates or multiple treated units.}
+
+\item{\code{predictor_match_covariates0}}{data.frame with time independent
+covariates on each row and column indicating the control unit names
+(dim k x J+1).}
+
+\item{\code{predictor_match_covariates1}}{Vector with time independent
+covariates for the treated unit (dim k x 1).}
+
+\item{\code{vs}}{Vector of weights for the importance of the predictors used
+in creating the synthetic control.
+Defaults to equal weight for all predictors.}
+}
+\if{html}{\out{
}}
+}
+\subsection{Returns}{
+A new \code{bayesianSynth} object.
+}
+}
+\if{html}{\out{
}}
+\if{html}{\out{}}
+\if{latex}{\out{\hypertarget{method-fit}{}}}
+\subsection{Method \code{fit()}}{
+Fit Stan model.
+\subsection{Usage}{
+\if{html}{\out{}}\preformatted{bayesianSynth$fit(...)}\if{html}{\out{
}}
+}
+
+\subsection{Arguments}{
+\if{html}{\out{}}
+\describe{
+\item{\code{...}}{other arguments passed to \code{\link[rstan:stanmodel-method-sampling]{rstan::sampling()}}.}
+}
+\if{html}{\out{
}}
+}
+}
+\if{html}{\out{
}}
+\if{html}{\out{}}
+\if{latex}{\out{\hypertarget{method-updateWidth}{}}}
+\subsection{Method \code{updateWidth()}}{
+Update the width of the credible interval.
+\subsection{Usage}{
+\if{html}{\out{}}\preformatted{bayesianSynth$updateWidth(ci_width = 0.75)}\if{html}{\out{
}}
+}
+
+\subsection{Arguments}{
+\if{html}{\out{}}
+\describe{
+\item{\code{ci_width}}{New width for the credible interval. This number
+should be in the (0,1) interval.}
+}
+\if{html}{\out{
}}
+}
+}
+\if{html}{\out{
}}
+\if{html}{\out{}}
+\if{latex}{\out{\hypertarget{method-summarizeLift}{}}}
+\subsection{Method \code{summarizeLift()}}{
+returns descriptive statistics of the lift estimate.
+\subsection{Usage}{
+\if{html}{\out{}}\preformatted{bayesianSynth$summarizeLift()}\if{html}{\out{
}}
+}
+
+}
+\if{html}{\out{
}}
+\if{html}{\out{}}
+\if{latex}{\out{\hypertarget{method-effectPlot}{}}}
+\subsection{Method \code{effectPlot()}}{
+effect ggplot2 object that shows the
+effect of the intervention over time.
+\subsection{Usage}{
+\if{html}{\out{}}\preformatted{bayesianSynth$effectPlot(facet = TRUE, subset = NULL)}\if{html}{\out{
}}
+}
+
+\subsection{Arguments}{
+\if{html}{\out{}}
+\describe{
+\item{\code{facet}}{Boolean that is TRUE if we want to divide the plot for each
+unit.}
+
+\item{\code{subset}}{Set of units to use in the effect plot.}
+}
+\if{html}{\out{
}}
+}
+}
+\if{html}{\out{
}}
+\if{html}{\out{}}
+\if{latex}{\out{\hypertarget{method-placeboPlot}{}}}
+\subsection{Method \code{placeboPlot()}}{
+Plot placebo intervention.
+\subsection{Usage}{
+\if{html}{\out{}}\preformatted{bayesianSynth$placeboPlot(periods, ...)}\if{html}{\out{
}}
+}
+
+\subsection{Arguments}{
+\if{html}{\out{}}
+\describe{
+\item{\code{periods}}{Positive number of periods for the placebo intervention.}
+
+\item{\code{...}}{other arguments passed to \code{\link[rstan:stanmodel-method-sampling]{rstan::sampling()}}.}
+}
+\if{html}{\out{
}}
+}
+\subsection{Returns}{
+ggplot2 object for placebo treatment effect.
+}
+}
+\if{html}{\out{
}}
+\if{html}{\out{}}
+\if{latex}{\out{\hypertarget{method-biasDraws}{}}}
+\subsection{Method \code{biasDraws()}}{
+Plots relative upper bias / tau for a time period \link{firstT, lastT}.
+\subsection{Usage}{
+\if{html}{\out{}}\preformatted{bayesianSynth$biasDraws(small_bias = 0.3, firstT, lastT)}\if{html}{\out{
}}
+}
+
+\subsection{Arguments}{
+\if{html}{\out{}}
+\describe{
+\item{\code{small_bias}}{Threshold value for considering the bias "small".}
+
+\item{\code{firstT, lastT}}{Time periods to compute relative bias over, they
+must be after the intervention.}
+}
+\if{html}{\out{
}}
+}
+\subsection{Returns}{
+vizdraw object with the posterior distribution of relative bias.
+Bias is scaled by the time periods.
+}
+}
+\if{html}{\out{
}}
+\if{html}{\out{}}
+\if{latex}{\out{\hypertarget{method-liftDraws}{}}}
+\subsection{Method \code{liftDraws()}}{
+Plots lift.
+\subsection{Usage}{
+\if{html}{\out{}}\preformatted{bayesianSynth$liftDraws(from, to, ...)}\if{html}{\out{
}}
+}
+
+\subsection{Arguments}{
+\if{html}{\out{}}
+\describe{
+\item{\code{from}}{First period to consider when calculating lift. If infinite,
+set to the time of the intervention.}
+
+\item{\code{to}}{Last period to consider when calculating lift. If infinite, set
+to the last period.}
+
+\item{\code{...}}{other arguments passed to vizdraws::vizdraws().}
+}
+\if{html}{\out{
}}
+}
+\subsection{Returns}{
+vizdraws object with the posterior distribution of the lift.
+}
+}
+\if{html}{\out{
}}
+\if{html}{\out{}}
+\if{latex}{\out{\hypertarget{method-liftBias}{}}}
+\subsection{Method \code{liftBias()}}{
+Plot Bias magnitude in terms of lift for period \link{firstT, lastT}
+pre_MADs / y0 relative to lift thresholds.
+\subsection{Usage}{
+\if{html}{\out{}}\preformatted{bayesianSynth$liftBias(firstT, lastT, offset, ...)}\if{html}{\out{
}}
+}
+
+\subsection{Arguments}{
+\if{html}{\out{}}
+\describe{
+\item{\code{firstT}}{start of the time period to compute relative bias
+over. They must be after the intervention.}
+
+\item{\code{lastT}}{end of the Time period to compute relative bias
+over. They must be after the intervention.}
+
+\item{\code{offset}}{Target lift \%.}
+
+\item{\code{...}}{other arguments passed to vizdraws::vizdraws().}
+}
+\if{html}{\out{
}}
+}
+}
+\if{html}{\out{
}}
+\if{html}{\out{}}
+\if{latex}{\out{\hypertarget{method-clone}{}}}
+\subsection{Method \code{clone()}}{
+The objects of this class are cloneable with this method.
+\subsection{Usage}{
+\if{html}{\out{}}\preformatted{bayesianSynth$clone(deep = FALSE)}\if{html}{\out{
}}
+}
+
+\subsection{Arguments}{
+\if{html}{\out{}}
+\describe{
+\item{\code{deep}}{Whether to make a deep clone.}
+}
+\if{html}{\out{
}}
+}
+}
+}
diff --git a/man/bsynth-package.Rd b/man/bsynth-package.Rd
new file mode 100644
index 0000000..75d84e5
--- /dev/null
+++ b/man/bsynth-package.Rd
@@ -0,0 +1,14 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/bsynth-package.R
+\docType{package}
+\name{bsynth-package}
+\alias{bsynth-package}
+\alias{bsynth}
+\title{The 'bsynth' package.}
+\description{
+Provides causal inference with a Bayesian synthetic
+control method.
+}
+\references{
+Stan Development Team (2020). RStan: the R interface to Stan. R package version 2.21.2. https://mc-stan.org
+}
diff --git a/man/pipe.Rd b/man/pipe.Rd
new file mode 100644
index 0000000..0eec752
--- /dev/null
+++ b/man/pipe.Rd
@@ -0,0 +1,12 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/utils-pipe.R
+\name{\%>\%}
+\alias{\%>\%}
+\title{Pipe operator}
+\usage{
+lhs \%>\% rhs
+}
+\description{
+See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details.
+}
+\keyword{internal}
diff --git a/src/Makevars.in b/src/Makevars.in
new file mode 100644
index 0000000..e48b4c9
--- /dev/null
+++ b/src/Makevars.in
@@ -0,0 +1,23 @@
+## Copyright 2021 Google LLC
+##
+## Licensed under the Apache License, Version 2.0 (the "License");
+## you may not use this file except in compliance with the License.
+## You may obtain a copy of the License at
+##
+## https://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing, software
+## distributed under the License is distributed on an "AS IS" BASIS,
+## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+## See the License for the specific language governing permissions and
+## limitations under the License.
+##
+# Generated by rstantools. Do not edit by hand.
+
+STANHEADERS_SRC = $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "message()" -e "cat(system.file('include', 'src', package = 'StanHeaders', mustWork = TRUE))" -e "message()" | grep "StanHeaders")
+
+PKG_CPPFLAGS = -I"../inst/include" -I"$(STANHEADERS_SRC)" -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -DBOOST_MATH_OVERFLOW_ERROR_POLICY=errno_on_error
+PKG_CXXFLAGS = $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "RcppParallel::CxxFlags()") $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "StanHeaders:::CxxFlags()")
+PKG_LIBS = $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "RcppParallel::RcppParallelLibs()") $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "StanHeaders:::LdFlags()")
+
+CXX_STD = CXX14
diff --git a/tests/testthat.R b/tests/testthat.R
new file mode 100644
index 0000000..20f9d63
--- /dev/null
+++ b/tests/testthat.R
@@ -0,0 +1,18 @@
+## Copyright 2021 Google LLC
+##
+## Licensed under the Apache License, Version 2.0 (the "License");
+## you may not use this file except in compliance with the License.
+## You may obtain a copy of the License at
+##
+## https://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing, software
+## distributed under the License is distributed on an "AS IS" BASIS,
+## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+## See the License for the specific language governing permissions and
+## limitations under the License.
+##
+library(testthat)
+library(bsynth)
+
+test_check("bsynth")
diff --git a/tests/testthat/test-factory.R b/tests/testthat/test-factory.R
new file mode 100644
index 0000000..f667160
--- /dev/null
+++ b/tests/testthat/test-factory.R
@@ -0,0 +1,26 @@
+## Copyright 2021 Google LLC
+##
+## Licensed under the Apache License, Version 2.0 (the "License");
+## you may not use this file except in compliance with the License.
+## You may obtain a copy of the License at
+##
+## https://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing, software
+## distributed under the License is distributed on an "AS IS" BASIS,
+## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+## See the License for the specific language governing permissions and
+## limitations under the License.
+##
+test_that("the factory works", {
+ # Create a new factory
+ germany_synth <- bayesianSynth$new(
+ data = germany,
+ time = year,
+ id = country,
+ treated = D,
+ outcome = gdp,
+ ci_width = 0.95
+ )
+ expect_is(germany_synth, "bayesianSynth")
+})
diff --git a/vignettes/.gitignore b/vignettes/.gitignore
new file mode 100644
index 0000000..097b241
--- /dev/null
+++ b/vignettes/.gitignore
@@ -0,0 +1,2 @@
+*.html
+*.R
diff --git a/vignettes/germany.Rmd b/vignettes/germany.Rmd
new file mode 100644
index 0000000..951e1de
--- /dev/null
+++ b/vignettes/germany.Rmd
@@ -0,0 +1,284 @@
+---
+## Copyright 2021 Google LLC
+##
+## Licensed under the Apache License, Version 2.0 (the "License");
+## you may not use this file except in compliance with the License.
+## You may obtain a copy of the License at
+##
+## https://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing, software
+## distributed under the License is distributed on an "AS IS" BASIS,
+## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+## See the License for the specific language governing permissions and
+## limitations under the License.
+##
+---
+title: "Germany Reunification"
+output: rmarkdown::html_vignette
+vignette: >
+ %\VignetteIndexEntry{Germany Reunification}
+ %\VignetteEngine{knitr::rmarkdown}
+ %\VignetteEncoding{UTF-8}
+---
+
+```{r, include = FALSE}
+knitr::opts_chunk$set(
+ collapse = TRUE,
+ comment = "#>",
+ echo = TRUE,
+ warning = FALSE,
+ message = FALSE,
+ out.width = '90%',
+ out.height = '600px',
+ fig.align='center'
+)
+set.seed(123)
+```
+
+## Background
+
+In this vignette I show how a simple Bayesian model can be used to generate a synthetic control to estimate the effect of the 1990 German reunification on per-capita GDP in West Germany. Figure 1 (Figure 1a, [Abadie, Diamond, and Hainmueller 2015](https://onlinelibrary.wiley.com/doi/abs/10.1111/ajps.12116)) shows the trajectory of GDP per-capita for West Germany and for a synthetic control generated using a the canonical approach described in ([Abadie and Gardeazabal, 2003](https://www.aeaweb.org/articles?id=10.1257/000282803321455188); [Abadie et al., 2010](https://www.aeaweb.org/articles?id=10.1257/000282803321455188)).
+
+
+```{r original_figure, echo = FALSE, fig.cap = "Figure 1: Trends in per Capita GDP: West Germany versus Synthetic West Germany"}
+knitr::include_graphics(path = "https://ignacio.martinez.fyi/synthetic_control/german_reunification.png")
+
+```
+
+## Bayesian Synthetic Control
+
+Let $y^\star_t$ be the standardized per Capita GDP of West Germany in period $t$,
+and $x^\star_t$ be a vector of the standardized per Capita GDP of
+the donor countries in period $t$.
+
+$$
+\begin{aligned}
+y^\star_t &\sim N(x^\star_t\beta , \sigma^2) \\
+\beta &\sim \text{Dir}(1)\\
+\sigma &\sim N^+(0,1)
+\end{aligned}
+$$
+
+Notice that $\beta$ is a simplex. Therefore, the weights ares bound to be
+positive and sum to 1. Further more, notice that I use a non-informative prior
+for the weights.
+
+
+```{r time_tiles}
+library(dplyr)
+library(ggplot2)
+library(bsynth)
+# Create a new factory
+germany_synth <- bayesianSynth$new(data = germany,
+ time = year,
+ id = country,
+ treated = D,
+ outcome = gdp,
+ ci_width = 0.95)
+# Visualize the timeline
+germany_synth$timeTiles +
+ ggplot2::xlab("Year") +
+ ggplot2::ylab("Country")
+
+```
+
+```{r results="hide"}
+# Fit the model
+germany_synth$fit()
+
+# Visualize the synthetic control
+germany_synth$synthetic +
+ ggplot2::xlab("Year") +
+ ggplot2::ylab("Per Capita GDP (PPP, 2002 USD)") +
+ ggplot2::scale_y_continuous(labels=scales::dollar_format())
+
+# Visualize the treatment effect
+germany_synth$effect() +
+ ggplot2::xlab("Year") +
+ ggplot2::ylab("Gap in Per Capita GDP (PPP, 2002 USD)") +
+ ggplot2::scale_y_continuous(labels = scales::dollar_format())
+
+```
+
+## GSynth
+
+In this section of the vignette I use the generalized synthetic control method based on interactive fixed effect models and implemented in [{gsynth}](https://github.com/xuyiqing/gsynth) to provide an additional comparison.
+
+```{r gsynth}
+
+library(gsynth)
+
+out <-
+ gsynth(
+ gdp ~ D,
+ data = germany,
+ index = c("country", "year"),
+ force = "two-way",
+ CV = TRUE,
+ r = c(0, 5),
+ se = TRUE,
+ inference = "parametric",
+ nboots = 10000,
+ parallel = FALSE,
+ cores = 4
+ )
+
+gsynth_plot <- plot(out,
+ xlab = "Period",
+ main = "GSynth: German Reunification",
+ ylab = "Effect")
+
+```
+
+## Side by Side
+
+```{r side_by_side}
+
+intervention <- germany %>%
+ filter(D == 1) %>%
+ summarise(year = min(year)) %>%
+ pull(year)
+
+
+bsynth_plot_data <-
+ germany_synth$plotData %>%
+ select(x = year, y = tau, ymin = tau_LB, ymax = tau_UB) %>%
+ mutate(method = "bsynth")
+
+timeXwalk <- germany %>%
+ select(year) %>%
+ distinct() %>%
+ arrange(year) %>%
+ mutate(t = 1:n())
+
+gsynth_plot_data <- gsynth_plot$data %>%
+ mutate(t = time + 30) %>%
+ inner_join(timeXwalk, by = "t") %>%
+ select(x = year, y = ATT, ymin = CI.lower, ymax = CI.upper) %>%
+ mutate(method = "GSynth")
+
+plot_data <- bind_rows(gsynth_plot_data, bsynth_plot_data)
+
+ggplot(data = plot_data, aes(x = x)) +
+ geom_line(aes(y = y)) +
+ geom_ribbon(aes(ymin = ymin, ymax = ymax),
+ color = "gray",
+ alpha = 0.2) +
+ theme_bw(base_size = 14) +
+ theme(
+ legend.position = "none",
+ panel.border = element_blank(),
+ axis.line = element_line()
+ ) +
+ geom_vline(xintercept = intervention, linetype = "dashed") +
+ facet_grid(cols = dplyr::vars(method)) +
+ xlab("Year") +
+ ylab("Gap in Per Capita GDP (PPP, 2002 USD)") +
+ scale_y_continuous(labels = scales::dollar_format())
+
+
+```
+
+## Placebo
+
+### GSynth
+
+```{r}
+
+placebo_germany <- germany %>%
+ filter(year < intervention) %>%
+ mutate(D = case_when(
+ (country == "West Germany" &
+ year >= (intervention - lubridate::years(5)) )~ 1,
+ TRUE ~ 0
+ ))
+
+out <-
+ gsynth(
+ gdp ~ D,
+ data = placebo_germany,
+ index = c("country", "year"),
+ force = "two-way",
+ CV = TRUE,
+ r = c(0, 5),
+ se = TRUE,
+ inference = "parametric",
+ nboots = 10000,
+ parallel = FALSE,
+ cores = 4
+ )
+
+gsynth_plot <- plot(out,
+ xlab = "Period",
+ main = "GSynth: Placebo German Reunification",
+ ylab = "Effect")
+```
+
+
+### bsynth
+
+```{r results="hide"}
+germany_synth_placebo <- bayesianSynth$new(
+ data = placebo_germany,
+ time = year,
+ id = country,
+ treated = D,
+ outcome = gdp,
+ ci_width = 0.95
+)
+
+germany_synth_placebo$fit()
+
+germany_synth_placebo$timeTiles
+
+
+germany_synth_placebo$effect() +
+ ggplot2::xlab("Year") +
+ ggplot2::ylab("Gap in Per Capita GDP (PPP, 2002 USD)") +
+ ggplot2::scale_y_continuous(labels = scales::dollar_format())
+
+```
+
+
+### Side by Side
+
+```{r}
+bsynth_plot_data <-
+ germany_synth_placebo$plotData %>%
+ select(x = year, y = tau, ymin = tau_LB, ymax = tau_UB) %>%
+ mutate(method = "bsynth")
+
+timeXwalk <- germany %>%
+ select(year) %>%
+ distinct() %>%
+ arrange(year) %>%
+ mutate(t = 1:n())
+
+gsynth_plot_data <- gsynth_plot$data %>%
+ mutate(t = time + 25) %>%
+ inner_join(timeXwalk, by = "t") %>%
+ select(x = year, y = ATT, ymin = CI.lower, ymax = CI.upper) %>%
+ mutate(method = "GSynth")
+
+plot_data <- bind_rows(gsynth_plot_data, bsynth_plot_data)
+
+ggplot(data = plot_data, aes(x = x)) +
+ geom_line(aes(y = y)) +
+ geom_ribbon(aes(ymin = ymin, ymax = ymax),
+ color = "gray",
+ alpha = 0.2) +
+ theme_bw(base_size = 14) +
+ theme(
+ legend.position = "none",
+ panel.border = element_blank(),
+ axis.line = element_line()
+ ) +
+ geom_vline(xintercept = intervention - lubridate::years(5),
+ linetype = "dashed") +
+ facet_grid(cols = dplyr::vars(method)) +
+ xlab("Year") +
+ ylab("Gap in Per Capita GDP (PPP, 2002 USD)") +
+ scale_y_continuous(labels = scales::dollar_format())
+```
+
diff --git a/vignettes/multiple_treated.Rmd b/vignettes/multiple_treated.Rmd
new file mode 100644
index 0000000..4240d62
--- /dev/null
+++ b/vignettes/multiple_treated.Rmd
@@ -0,0 +1,99 @@
+---
+## Copyright 2021 Google LLC
+##
+## Licensed under the Apache License, Version 2.0 (the "License");
+## you may not use this file except in compliance with the License.
+## You may obtain a copy of the License at
+##
+## https://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing, software
+## distributed under the License is distributed on an "AS IS" BASIS,
+## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+## See the License for the specific language governing permissions and
+## limitations under the License.
+##
+---
+title: "Multiple Treated"
+output: rmarkdown::html_vignette
+vignette: >
+ %\VignetteIndexEntry{Multiple Treated}
+ %\VignetteEngine{knitr::rmarkdown}
+ %\VignetteEncoding{UTF-8}
+---
+
+```{r, include = FALSE}
+knitr::opts_chunk$set(
+ collapse = TRUE,
+ comment = "#>",
+ echo = TRUE,
+ warning = FALSE,
+ message = FALSE,
+ out.width = '90%',
+ out.height = '600px',
+ fig.align='center'
+)
+set.seed(123)
+```
+
+## Background
+
+In this vignette I show how {bsynth} can be used for causal estimation when you
+ have multiple treated accounts. In particular, I use the synthetic data exaple
+ from [Xu, Yiqing, 2017](http://dx.doi.org/10.1017/pan.2016.2). In this example,
+ the treatment starts in period 21 and increases by one each period
+ (e.g. the effect is 5 period 25 and 10 in period 30).
+
+```{r original_figure, echo = FALSE, fig.cap = "Figure 1: Xu, Yiqing, 2017", out.height = '400px'}
+knitr::include_graphics(path = "https://ignacio.martinez.fyi/synthetic_control/gsynth.png")
+
+```
+
+## Bayesian Synthetic Control with Covariates
+
+```{r setup, out.height = '800px'}
+library(bsynth)
+ci_width <- 0.95
+data(gsynth, package = "gsynth")
+dplyr::glimpse(simdata)
+
+outcome_data <- simdata %>%
+ dplyr::select(time, id, D, Y)
+
+covariates <- simdata %>%
+ dplyr::select(time, id, X1, X2)
+
+synth <-
+ bsynth::bayesianSynth$new(
+ data = outcome_data,
+ time = time,
+ id = id,
+ treated = D,
+ outcome = Y,
+ ci_width = ci_width,
+ covariates = covariates
+ )
+
+synth$timeTiles +
+ ggplot2::theme(text = ggplot2::element_text(size=6))
+
+```
+
+### Fit
+
+```{r fit, results="hide"}
+synth$fit(cores = 4)
+```
+
+### Visualize the synthetic controls for each treated unit
+
+```{r}
+synth$synthetic
+```
+### Visualize the treatment effect
+
+```{r}
+synth$effect(subset = c("Average"), facet = FALSE) +
+ ggplot2::scale_y_continuous(breaks=seq(-2,12,2))
+```
+
diff --git a/vignettes/prop99.Rmd b/vignettes/prop99.Rmd
new file mode 100644
index 0000000..7b56f43
--- /dev/null
+++ b/vignettes/prop99.Rmd
@@ -0,0 +1,110 @@
+---
+## Copyright 2021 Google LLC
+##
+## Licensed under the Apache License, Version 2.0 (the "License");
+## you may not use this file except in compliance with the License.
+## You may obtain a copy of the License at
+##
+## https://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing, software
+## distributed under the License is distributed on an "AS IS" BASIS,
+## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+## See the License for the specific language governing permissions and
+## limitations under the License.
+##
+---
+title: "Proposition 99"
+output: rmarkdown::html_vignette
+vignette: >
+ %\VignetteIndexEntry{Proposition 99}
+ %\VignetteEngine{knitr::rmarkdown}
+ %\VignetteEncoding{UTF-8}
+---
+
+```{r, include = FALSE}
+knitr::opts_chunk$set(
+ collapse = TRUE,
+ comment = "#>",
+ echo = TRUE,
+ warning = FALSE,
+ message = FALSE,
+ out.width = '90%',
+ out.height = '600px',
+ fig.align='center'
+)
+set.seed(123)
+```
+
+
+```{r time_tiles, out.height = '900px'}
+library(bsynth)
+
+prop99_synth <-
+ bayesianSynth$new(
+ data = prop99_df,
+ time = year,
+ id = state,
+ treated = D,
+ outcome = packs,
+ GP = FALSE,
+ ci_width = 0.95
+ )
+
+prop99_synth$timeTiles +
+ ggplot2::xlab("Year") +
+ ggplot2::ylab("State") +
+ ggplot2::theme(text = ggplot2::element_text(size=12))
+
+
+```
+
+## Fit without Gaussian Process
+
+```{r results="hide"}
+prop99_synth$fit(control = list(adapt_delta = 0.999, max_treedepth = 15),
+ cores = 4,
+ refresh = 0,
+ seed = 1982,
+ warmup = 4000,
+ iter = 6000)
+
+prop99_synth$synthetic +
+ ggplot2::xlab("Year") +
+ ggplot2::ylab("Per-Capita Cigarrette Sales (in Packs)")
+
+prop99_synth$effect() +
+ ggplot2::xlab("Year") +
+ ggplot2::ylab("Change in Per-Capita Cigarrette Sales (in Packs)")
+```
+
+## Fit with Gaussian Process
+
+```{r results="hide"}
+prop99_synth_gp <-
+ bayesianSynth$new(
+ data = prop99_df,
+ time = year,
+ id = state,
+ treated = D,
+ outcome = packs,
+ GP = TRUE,
+ ci_width = 0.95
+ )
+
+prop99_synth_gp$fit(control = list(adapt_delta = 0.999, max_treedepth = 15),
+ cores = 4,
+ refresh = 0,
+ seed = 1982,
+ warmup = 4000,
+ iter = 6000)
+
+prop99_synth_gp$synthetic +
+ ggplot2::xlab("Year") +
+ ggplot2::ylab("Per-Capita Cigarrette Sales (in Packs)")
+
+prop99_synth_gp$effect() +
+ ggplot2::xlab("Year") +
+ ggplot2::ylab("Change in Per-Capita Cigarrette Sales (in Packs)")
+```
+
diff --git a/vignettes/with_covariates.Rmd b/vignettes/with_covariates.Rmd
new file mode 100644
index 0000000..24c77a1
--- /dev/null
+++ b/vignettes/with_covariates.Rmd
@@ -0,0 +1,216 @@
+---
+## Copyright 2021 Google LLC
+##
+## Licensed under the Apache License, Version 2.0 (the "License");
+## you may not use this file except in compliance with the License.
+## You may obtain a copy of the License at
+##
+## https://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing, software
+## distributed under the License is distributed on an "AS IS" BASIS,
+## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+## See the License for the specific language governing permissions and
+## limitations under the License.
+##
+---
+title: "With Covariates"
+output: rmarkdown::html_vignette
+vignette: >
+ %\VignetteIndexEntry{With Covariates}
+ %\VignetteEngine{knitr::rmarkdown}
+ %\VignetteEncoding{UTF-8}
+---
+
+```{r, include = FALSE}
+knitr::opts_chunk$set(
+ collapse = TRUE,
+ comment = "#>",
+ echo = TRUE,
+ warning = FALSE,
+ message = FALSE,
+ out.width = '90%',
+ out.height = '600px',
+ fig.align='center'
+)
+```
+
+## Generate Synthetic Data
+
+```{r}
+set.seed(1)
+library(dplyr)
+library(ggplot2)
+
+true_lift <- tibble(t = 1:100) %>%
+ mutate(
+ x = t - 71,
+ lift = case_when(x < 0 ~ 0,
+ TRUE ~ dgamma(x = x, shape = 2, scale = 10)),
+ lift = 3*lift) %>%
+ select(-x)
+
+
+
+
+fake_data <-
+ tibble(x1 = 100 + arima.sim(model = list(ar = 0.99), n = 100),
+ x2 = 300 + arima.sim(model = list(ar = 0.57), n = 100),
+ x3 = 50 + arima.sim(model = list(ar = 0.79, ma = 0.8), n = 100),
+ m1 = rnorm(n = 100, mean = -2, sd = 1),
+ m2 = rnorm(n = 100, mean = 0.5, sd = 1),
+ m3 = rnorm(n = 100, mean = 0.25, sd = 1),
+ m4 = rnorm(n = 100, mean = 0.15, sd = 1)) %>%
+ mutate(
+ y = as.numeric(0.2 * x1 +
+ 0.3 * x2 +
+ 0.5 * x3 -
+ 6.5*m1 -
+ 3.1*m2 -
+ 2.3*m3 -
+ 1.1*m4 +
+ rnorm(100)),
+ t = 1:n()
+ ) %>%
+ inner_join(true_lift, by = "t") %>%
+ mutate(
+ y0 = y,
+ d = y * lift,
+ y = y * (1+lift)
+ )
+
+true_lift <- fake_data %>%
+ select(t, lift, d)
+
+true_lift_table <- fake_data %>%
+ filter(t >= 71) %>%
+ summarise(sum_y0 = sum(y0),
+ sum_y1 = sum(y),
+ lift = (sum_y1 - sum_y0)/sum_y0)
+
+truth <- true_lift_table %>%
+ pull(lift)
+
+fake_data <- fake_data%>%
+ select(-lift, -d, -y0)
+
+ggplot(data = fake_data, aes(x=t, y=y)) +
+ geom_line() +
+ xlab("Time") +
+ ylab("y") +
+ theme_bw() +
+ geom_vline(xintercept = 71, linetype = "dashed") +
+ ggtitle(label = "Time Series of Interest",
+ subtitle = "Intervention happens in period 71")
+
+ggplot(data = true_lift, aes(x=t,y=lift)) +
+ geom_line() +
+ xlab("Time") +
+ ylab("Lift") +
+ theme_bw() +
+ scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
+ geom_vline(xintercept = 71, linetype = "dashed") +
+ ggtitle(label = "True Lift",
+ subtitle = "Intervention happens in period 71")
+```
+
+* In these synthetic data the true lift between periods 71 and 100 is `r scales::percent(truth, accuracy = 0.1)`.
+
+
+## Simplest Model
+
+
+```{r}
+library(bsynth)
+
+ci_width <- 0.95
+
+long_data <- fake_data %>%
+ select(-m1, -m2, -m3, -m4) %>%
+ tidyr::pivot_longer(-t, names_to = "id", values_to = "y") %>%
+ mutate(D = case_when(id == "y" & t >= 71 ~ 1,
+ TRUE ~ 0))
+
+synth <- bsynth::bayesianSynth$new(
+ data = long_data,
+ time = t,
+ id = id,
+ treated = D,
+ outcome = y,
+ ci_width = ci_width
+)
+
+
+synth$fit(cores = 4)
+
+synth$effect()
+
+synth$synthetic
+
+synth$liftDraws(
+ from = 71,
+ to = 100,
+ breaks = c(0.01, 0.06, 0.09),
+ break_names = c("Not worth it",
+ "Worth it",
+ "Very worth it",
+ "Amazing")
+)
+
+point <- synth$summarizeLift()
+
+
+```
+* We estimate a counterfactual lift between periods 71 and 100 of
+`r scales::percent(point[[1]], accuracy = 0.1)` with a
+`r scales::percent(ci_width, accuracy = 1)` probability that is as low as
+`r scales::percent(point[[2]], accuracy = 0.1)` and as high as
+`r scales::percent(point[[3]], accuracy = 0.1)`.
+
+
+## With Covariates
+
+```{r}
+covariates <- fake_data %>%
+ select(t, m1, m2, m3, m4)
+
+synth <- bsynth::bayesianSynth$new(
+ data = long_data,
+ time = t,
+ id = id,
+ treated = D,
+ outcome = y,
+ ci_width = ci_width,
+ covariates = covariates
+)
+
+
+synth$fit(cores = 4)
+
+synth$effect()
+
+synth$synthetic
+
+synth$liftDraws(
+ from = 71,
+ to = 100,
+ breaks = c(0.01, 0.06, 0.09),
+ break_names = c("Not worth it",
+ "Worth it",
+ "Very worth it",
+ "Amazing")
+)
+
+point <- synth$summarizeLift()
+
+```
+
+* We estimate a counterfactual lift between periods 71 and 100 of
+`r scales::percent(point[[1]], accuracy = 0.1)` with a
+`r scales::percent(ci_width, accuracy = 1)` probability that is as low as
+`r scales::percent(point[[2]], accuracy = 0.1)` and as high as
+`r scales::percent(point[[3]], accuracy = 0.1)`.
+
+
+
+