bayesianFactor | R Documentation |
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
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
vizdraws object with the relative bias with offset.
timeTiles
ggplot2 object that shows when the intervention happened.
plotData
tibble with the observed outcome and the counterfactual data.
interventionTime
returns the intervention time period.
synthetic
ggplot2 object that shows the observed and counterfactual outcomes over time.
new()
Create a new bayesianFactor object.
bayesianFactor$new( data, time, id, treated, outcome, ci_width = 0.75, covariates )
data
Long data.frame object with fields outcome, time, id, and treatment indicator.
time
Name of the variable in the data frame that
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.
covariates
Dataframe with a column for id and the other columns Defaults to NULL if no covariates should be included in the model.
params described in the data structure section of the documentation of the R6 class at the top of the file.
A new bayesianFactor
object.
fit()
Fit Stan model.
bayesianFactor$fit(L = 8, ...)
L
Number of factors.
...
other arguments passed to rstan::sampling()
.
updateWidth()
Update the width of the credible interval.
bayesianFactor$updateWidth(ci_width = 0.75)
ci_width
New width for the credible interval. This number should be in the (0,1) interval.
summarizeLift()
summarizeLift returns descriptive statistics of the lift estimate.
bayesianFactor$summarizeLift()
effectPlot()
effectPlot returns ggplot2 object that shows the effect of the intervention over time.
bayesianFactor$effectPlot()
liftDraws()
Plots lift.
bayesianFactor$liftDraws(from, to, ...)
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().
vizdraws object with the posterior distribution of the lift.
liftBias()
Plot bias magnitude in terms of lift for period (firstT, lastT)
bayesianFactor$liftBias(firstT, lastT, offset, ...)
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. over. They must be after the intervention.
offset
Target lift %.
...
other arguments passed to vizdraws::vizdraws().
biasDraws()
Plots relative upper bias / tau for a time period (firstT, lastT).
bayesianFactor$biasDraws(small_bias = 0.3, firstT, lastT)
small_bias
Threshold value for considering the bias "small".
firstT, lastT
Time periods to compute relative bias over, they must after the intervention.
vizdraw object with the posterior distribution of relative bias. Bias is scaled by the time periods.
clone()
The objects of this class are cloneable with this method.
bayesianFactor$clone(deep = FALSE)
deep
Whether to make a deep clone.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.