| bayesianFactor {bsynth} | R Documentation |
Create a Bayesian Synthetic Control Object Using Panel Data
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 Pinkney 2021.
public methods:
-
initialize()initializes the variables and model parameters -
fit()fits the stan model and returns a fit object -
updateWidthupdates the width of the credible interval -
placeboPlotgenerates a counterfactual placebo plot -
effectPlotreturns a plot of the treatment effect over time -
summarizeLiftreturns descriptive statistics of the lift estimate -
biasDrawsreturns a plot of the relative bias in a LFM -
liftDrawsreturns a plot of the posterior lift distribution -
liftBiasreturns a plot of the relative bias given a lift offset
Value
vizdraws object with the relative bias with offset.
Active bindings
timeTilesggplot2 object that shows when the intervention happened.
plotDatatibble with the observed outcome and the counterfactual data.
interventionTimereturns the intervention time period.
syntheticggplot2 object that shows the observed and counterfactual outcomes over time.
Methods
Public methods
Method new()
Create a new bayesianFactor object.
Usage
bayesianFactor$new( data, time, id, treated, outcome, ci_width = 0.75, covariates )
Arguments
dataLong data.frame object with fields outcome, time, id, and treatment indicator.
timeName of the variable in the data frame that
idName of the variable in the data frame that identifies the units (e.g. country, region etc).
treatedName of the variable in the data frame that contains the treatment assignment of the intervention.
outcomeName of the outcome variable.
ci_widthCredible interval's width. This number is in the (0,1) interval.
covariatesDataframe with a column for id and the other columns Defaults to NULL if no covariates should be included in the model.
Details
params described in the data structure section of the documentation of the R6 class at the top of the file.
Returns
A new bayesianFactor object.
Method fit()
Fit Stan model.
Usage
bayesianFactor$fit(L = 8, ...)
Arguments
LNumber of factors.
...other arguments passed to
rstan::sampling().
Method updateWidth()
Update the width of the credible interval.
Usage
bayesianFactor$updateWidth(ci_width = 0.75)
Arguments
ci_widthNew width for the credible interval. This number should be in the (0,1) interval.
Method summarizeLift()
summarizeLift returns descriptive statistics of the lift estimate.
Usage
bayesianFactor$summarizeLift()
Method effectPlot()
effectPlot returns ggplot2 object that shows the effect of the intervention over time.
Usage
bayesianFactor$effectPlot()
Method liftDraws()
Plots lift.
Usage
bayesianFactor$liftDraws(from, to, ...)
Arguments
fromFirst period to consider when calculating lift. If infinite, set to the time of the intervention.
toLast 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)
Usage
bayesianFactor$liftBias(firstT, lastT, offset, ...)
Arguments
firstTStart of the time period to compute relative bias over. Must be after the intervention.
lastTEnd of the time period to compute relative bias over. Must be after the intervention. over. They must be after the intervention.
offsetTarget lift %.
...other arguments passed to vizdraws::vizdraws().
Method biasDraws()
Plots relative upper bias / tau for a time period (firstT, lastT).
Usage
bayesianFactor$biasDraws(small_bias = 0.3, firstT, lastT)
Arguments
small_biasThreshold value for considering the bias "small".
firstT, lastTTime periods to compute relative bias over, they must after the intervention.
Returns
vizdraw object with the posterior distribution of relative bias. Bias is scaled by the time periods.
Method clone()
The objects of this class are cloneable with this method.
Usage
bayesianFactor$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.