Code Monkey home page Code Monkey logo

causalmbsts's Introduction

CausalMBSTS

What is CausalMBSTS?

CausalMBSTS is an R package that can be used to infer a causal effect of an intervention on a multivariate time series. For example, if two items are frequently bought together (e.g., printer and ink) but only one of them gets a discount, what is the effect of the discount on the sales of both products? To answer this question, the dependence structure between the items must be considered. The package does so by using Multivariate Bayesian Structural Time Series models (MBSTS).

Why MBSTS models?

MBSTS models are flexible tools that also allow for a transparent way to deal with uncertainty. Flexibility comes from an option for a user to add sub-components (e.g., trend, seasonality, and cycle) that encapsulate the characteristics of a time series. Transparency comes from the Bayesian framework, in which our uncertainty surrounding model parameters is explicitly accounted for by setting prior distributions that represent our prior knowledge of the phenomenon. Then, by combining the priors with the data, we obtain posterior distributions that can be used for several purposes (e.g., compute a point estimate, credible intervals, perform prediction). The package implements the Gaussian linear model that, possibly after transformation of the dependent variable, provides an adequate representation for many time series that are analyzed in practice.

How does the package work?

The main function included in the package is CausalMBSTS(). The user is asked to provide the data, a vector of dates (including the intervention date) and to select the desired model components. The user can set custom values of the hyperparameters of the priors or use the default values. Then, the function uses a Gibbs sampler to draw from the joint posterior distribution of the model parameters before the intervention. The next step is the prediction of the counterfactual outcomes in the absence of intervention, which is done by sampling from the posterior predictive distribution (PPD). The causal effect at each point in time after the intervention is computed as the difference between the observed outcome and the PPD. Plotting and printing methods for the resulting object are provided.

Aside from causal inference, the package can also be used for forecasting. This task can be completed with the function as.mbsts() coupled with the method predict.mbsts(): the former estimates the MBSTS model and outputs an object of class mbsts; the latter performs a prediction step for a given number of future periods.

Installing the package from GitHub

# Get CausalMBSTS from GitHub
devtools::install_github("FMenchetti/CausalMBSTS", build_vignettes = TRUE)

# Get CausalMBSTS from CRAN
install.packages("CausalMBSTS")
library(CausalMBSTS)

Further readings

Menchetti & Bojinov (2020), Estimating causal effects in the presence of partial interference using multivariate Bayesian structural time series models https://arxiv.org/abs/2006.12269

causalmbsts's People

Contributors

fmenchetti avatar vliublinska avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

causalmbsts's Issues

Add function(s) that do not require SSModel input

In order to remove the entry barrier of using CausalMBSTS package, which currently requires knowing the SSModel structure and being able to create a corresponding object, the most important functions in a package usually allow a user to just feed a data set and some background covariates and either add a user-specified formula (like, say, any modeling routine such as lm() or glm()) or just fit a basic default model (like in CausalImpact()).

We can choose either of these two routes and add a function, say CausalMBSTS(), that would take in either a data set with some covariates or an SSModel. It would then either create an SSModel object for the former case and call causal.mbsts (or causal.SSModel, if we rename it), or just call the causal.mbsts function directly. In fact, in such case causal.mbsts (or causal.SSModel) doesn't even have to be exported and can be internal (I'll defer to @ISTA to weigh in on that).

Hope this description makes sense! Please, give your feedback, if any. @FMenchetti

add function 'plot.mbsts'

The function should plot: i) the pointwise causal effect with its confidence bounds; ii) the observed time series vs the predicted counterfactual series ; iii) the posterior predictive checks. The function should give the possibility to choose the plot type.

Import or depend on KFAS?

Will users of this package always want to have KFAS attached for SSmodel fitting? If so we can depend on KFAS instead of importing it. If it makes more sense to depend on it we can remove it from the imports list as in #19

Relative effect

Is there a way to get the relative effect size of an intervention?

'mbsts.mcmc' model building

Given a 'model' of class 'SSModel' and prior parameters, samples two values for the variances from their prior distributions and assign them to 'model$H' and 'model$Q'

NA action

What about NA in the response variable? mbsts.mcmc doesn't work if NA are present. Shall we require the user to do his own NA imputation/treatment or shall we provide an internal NA treatment as in bsts?

checks breaks on r-devel-windows-x86_64-gcc10-UCRT

Checking PDF version of manual ... WARNING : LaTeX errors when creating PDF version. This typically indicates Rd problems.
checking PDF version of manual without hyperrefs or index ... ERROR Re-running with no redirection of stdout/stderr

Release CausalMBSTS 0.1.0

Prepare for release:

  • Check that description is informative
  • Check licensing of included files
  • usethis::use_cran_comments()
  • devtools::check(remote = TRUE, manual = TRUE)
  • devtools::check_win_devel()
  • rhub::check_for_cran()
  • Update cran-comments.md

Submit to CRAN:

  • devtools::release()
  • Approve email

Wait for CRAN...

  • Accepted 🎉
  • Update install instructions in README

Bug in causal.mbsts

I was checking whether defining H as singular matrix produces an error (see issue #25, last point). Instead, it doesn't and that's not correct...the function is not supposed to work if H is singular

Error in rInvWishart؛ 'scal' matrix is not positive-definite

Hello
When we put the data in the (as.mbsts) function, the following error is observed, what is the reason for this error? How to fix it?

Error in rInvWishart(1, nu, s[[paste("degree.", i, sep = "")]]) :
'scal' matrix is not positive-definite

Kind regards

insert holiday option in 'causal.mbsts'

e.g. holiday.rm = T; given a provided time period ('dates' arg), the function should be able to check for the presence of a national holiday and remove it (if asked by the user) before computing the causal effect

Renaming functions

Following the latest naming conventions in software development (which are also utilized by CausalImpact package), lets make some changes to the function names:

  • mbsts.mcmc to mcmc.SSModel to reflect the fact that SSModel is a class of the input object and mcmc can be viewed as its method
  • causal.mbsts to causal.SSModel, same thinking

Edit: The corresponding .R files should also be renamed.

@FMenchetti @izahn, what do you think?

See also #32

Uniformative error message

After creating a CausalMBSTS class object with CausalMBSTS() I wanted to create a plot with the counterfactuals usingplot(causal_asu, int.date = int.date, horizon = horizon, type = "forecast"). However, I get an uninformative (at least to me) error message:

plot(causal_asu, int.date = int.date, horizon = horizon, type = "forecast")
Error in start:(end - 1) : argument of length 0

The same message for type = "impact"
I followed the "working example' from the CRAN website.
Data: A panel data set, converted from long to wide, with 5 y variables and 2 x variables

causal.mbsts cumulative effect computation

The function only computes the temporal average effect, but in the paper we also defined a cumulative effect. So, we may want to add its computation to causal.mbsts.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.