Causal Impact R Package in Python

Causal Impact R Package in Python

How to run Google’s CausalImpact R package from your Python environment. CausalImpact is an R package developed by Google for causal inference using Bayesian Structural time-series models. You can find the R version here.

CausalImpact is an R package developed by Google for causal inference using Bayesian Structural time-series models. You can find the R version here.

In short, what this package does is making counterfactual predictions. In other words, what would have happened in a parallel (sort of) universe if an intervention never had happened? Here is a quick example straight from Google’s website: “Given a response time series (e.g., clicks) and a set of control time series (e.g., clicks in non-affected markets or clicks on other sites), the package constructs a Bayesian structural time-series model. This model is then used to try and predict the counterfactual, i.e., how the response metric would have evolved after the intervention if the intervention had never occurred.”

Image for post

CausalImpact 1.2.1, Brodersen et al., Annals of Applied Statistics (2015). http://google.github.io/CausalImpact/

Image Description: Part A of the image (original) shows, with the dark continuous line, the time series of something we are monitoring. The blue dotted one is the counterfactual prediction. The vertical grey line is the moment when an intervention was made. We can observe that from that moment onwards, blue and black lines drift apart. Part B (pointwise) illustrates the difference of those lines over time which in essence is the causal effect we are interested in, while Part C (cumulative) is the cumulative difference over time.

I know you can work with R, but for Python lovers, I am not aware of the equivalent package. Surely, there are some libraries implementing parts of the original paper. By checking out some of those Python implementations I noticed differences in terms of the results. Long story short, here you can check how to run this package from python. Similarly, the approach is generalisable to probably any R package for that matter.

What worked for me was to create a new Conda environment with both Python libraries and core R packages pre-installed. Here is an example: conda create -n r_env numpy pandas statsmodels r-essentials r-base

Creating the environment should take some time. Also, note that Jupyter notebook requires further configuration so I tend to edit the code in any programming editor instead and run from the command line.

What we would also need is rpy2 which does all the work for us. It is a python interface to the R language. pip install rpy2 would do.

Load all the libraries as below:

#rpy2 lib
from rpy2.robjects.packages import importr
import rpy2.robjects as robjects
from rpy2.robjects import pandas2ri
from rpy2.robjects import Formula
import rpy2.robjects.packages as rpackages
import rpy2.robjects.vectors as StrVector
from rpy2.ipython.ggplot import image_png
#typical python libs
import numpy as np
import pandas as pd
import datetime
#arma
from statsmodels.tsa.arima_process import ArmaProcess

python timeseries causality r counterfactual

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Python Tricks Every Developer Should Know

In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.

How to Remove all Duplicate Files on your Drive via Python

Today you're going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates. We gonna use Python OS remove( ) method to remove the duplicates on our drive. Well, that's simple you just call remove ( ) with a parameter of the name of the file you wanna remove done.

Python vs R vs SAS | R, Python and SAS Comparison | Learn R, Python and SAS? | Intellipaat

🔥🔥🔥This R, Python and SAS Comparison video you will learn the difference between Python vs R vs SAS and whether you should learn r, python and sas for data s...

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

From ‘R vs Python’ to ‘R and Python’

In this article, you'll learn to leverage the best of both ‘Python and R’ in a single project.