bayesianSynth {bsynth} | R Documentation |
Create a Bayesian Synthetic Control Object Using Panel Data
Description
A Bayesian Synthetic Control has raw data and draws from the posterior distribution. This is represented by an R6 Class.
public methods:
-
initialize()
initializes the variables and model parameters -
fit()
fits the stan model and returns a fit object -
updateWidth
updates the width of the credible interval -
placeboPlot
generates a counterfactual placebo plot -
effectPlot
returns a plot of the treatment effect over time -
summarizeLift
returns descriptive statistics of the lift estimate -
biasDraws
returns a plot of the relative bias in a LFM -
liftDraws
returns a plot of the posterior lift distribution -
liftBias
returns a plot of the relative bias given a lift offset Data structure:
Value
vizdraws object with the relative bias with offset.
Active bindings
timeTiles
ggplot2 object that shows when the intervention happened.
plotData
returns tibble with the observed outcome and the counterfactual data.
interventionTime
returns intervention time period (e.g., year) in which the treatment occurred.
synthetic
returns ggplot2 object that shows the observed and counterfactual outcomes over time.
checks
returns MCMC checks.
lift
draws from the posterior distribution of the lift.
Methods
Public methods
Method new()
Create a new bayesianSynth object.
Usage
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 )
Arguments
data
Long data.frame object with fields outcome, time, id, and treatment indicator.
time
Name of the variable in the data frame that identifies the time period (e.g. year, month, week etc).
id
Name of the variable in the data frame that identifies the units (e.g. country, region etc).
treated
Name of the variable in the data frame that contains the treatment assignment of the intervention.
outcome
Name of the outcome variable.
ci_width
Credible interval's width. This number is in the (0,1) interval.
gp
Logical that indicates whether or not to include a Gaussian Process as part of the model.
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.
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.
predictor_match_covariates0
data.frame with time independent covariates on each row and column indicating the control unit names (dim k x J+1).
predictor_match_covariates1
Vector with time independent covariates for the treated unit (dim k x 1).
vs
Vector of weights for the importance of the predictors used in creating the synthetic control. Defaults to equal weight for all predictors.
Returns
A new bayesianSynth
object.
Method fit()
Fit Stan model.
Usage
bayesianSynth$fit(...)
Arguments
...
other arguments passed to
rstan::sampling()
.
Method updateWidth()
Update the width of the credible interval.
Usage
bayesianSynth$updateWidth(ci_width = 0.75)
Arguments
ci_width
New width for the credible interval. This number should be in the (0,1) interval.
Method summarizeLift()
returns descriptive statistics of the lift estimate.
Usage
bayesianSynth$summarizeLift()
Method effectPlot()
effect ggplot2 object that shows the effect of the intervention over time.
Usage
bayesianSynth$effectPlot(facet = TRUE, subset = NULL)
Arguments
facet
Boolean that is TRUE if we want to divide the plot for each unit.
subset
Set of units to use in the effect plot.
Method placeboPlot()
Plot placebo intervention.
Usage
bayesianSynth$placeboPlot(periods, ...)
Arguments
periods
Positive number of periods for the placebo intervention.
...
other arguments passed to
rstan::sampling()
.
Returns
ggplot2 object for placebo treatment effect.
Method biasDraws()
Plots relative upper bias / tau for a time period (firstT, lastT).
Usage
bayesianSynth$biasDraws(small_bias = 0.3, firstT, lastT)
Arguments
small_bias
Threshold value for considering the bias "small".
firstT
Start of the time period to compute relative bias over. Must be after the intervention.
lastT
End of the time period to compute relative bias over. Must be after the intervention.
Returns
vizdraw object with the posterior distribution of relative bias. Bias is scaled by the time periods.
Method liftDraws()
Plots lift.
Usage
bayesianSynth$liftDraws(from, to, ...)
Arguments
from
First period to consider when calculating lift. If infinite, set to the time of the intervention.
to
Last period to consider when calculating lift. If infinite, set to the last period.
...
other arguments passed to vizdraws::vizdraws().
Returns
vizdraws object with the posterior distribution of the lift.
Method liftBias()
Plot Bias magnitude in terms of lift for period (firstT, lastT) pre_MADs / y0 relative to lift thresholds.
Usage
bayesianSynth$liftBias(firstT, lastT, offset, ...)
Arguments
firstT
start of the time period to compute relative bias over. They must be after the intervention.
lastT
end of the Time period to compute relative bias over. They must be after the intervention.
offset
Target lift %.
...
other arguments passed to vizdraws::vizdraws().
Method weightDraws()
Plot implicit weight distribution across draws.
Usage
bayesianSynth$weightDraws()
Returns
ggplot object with weight distribution per unit.
Method weightCorr()
Plots correlations between weights across draws.
Usage
bayesianSynth$weightCorr()
Returns
ggplot heatmap object with correlations.
Method clone()
The objects of this class are cloneable with this method.
Usage
bayesianSynth$clone(deep = FALSE)
Arguments
deep
Whether to make a deep clone.