Introduction to Effectiveness Verification Chapters 4 and 5 are written in Python

Introduction

Introduction to Effectiveness Verification-Causal Reasoning for Correct Comparison / Basics of Econometrics Reproduce the source code in Python To do.

I already have a Great ancestor implementation example, but I will leave it as a memo for my study.

This article describes Chapters 4 and 5. The code is also posted on github. In addition, variable names and processing contents are basically implemented in the book.

CausalImpact

Here, it is implemented using pycausal impact. The result does not match the R version, but this seems to be because the method handled is different as described in the README. It seems that you can specify the state space model with the argument model, but I gave up because it seems to be difficult to match the implementation with the R version.

CausalImpact


from causalimpact import CausalImpact

impact = CausalImpact(CI_data, pre_period, post_period, prior_level_sd=None)

impact.plot()
print(impact.summary())

RDD Non-linear regression analysis can be performed by learning the following model and referring to the effect size of the intervention variable.

Y = \beta_0 + \beta_1 f_1(X-c) + \beta_2 f_2(X-c) + \rho Z + \epsilon

RDD


from sklearn.preprocessing import PolynomialFeatures

degree = 4
X = nonlinear_rdd_data[['history_log']]
X = X - cutpoint
X_poly = PolynomialFeatures(degree=degree, include_bias=False).fit_transform(X)
X_poly = pd.DataFrame(X_poly, columns=[f'X{i+1}' for i in range(X_poly.shape[1])])
nonlinear_rdd_data = pd.concat([nonlinear_rdd_data, X_poly], axis=1)

nonlinear_rdd_ord4 = ols('visit ~ treatment + X1 + X2 + X3 + X4 + treatment:X1 + treatment:X2 + treatment:X3 + treatment:X4', data=nonlinear_rdd_data).fit()

Relation

-I wrote "Introduction to Effect Verification" in Python

Recommended Posts

Introduction to Effectiveness Verification Chapters 4 and 5 are written in Python
Introduction to effectiveness verification Chapter 3 written in Python
Introduction to Effectiveness Verification Chapter 2 Written in Python
Introduction to Effectiveness Verification Chapter 1 in Python
I wrote "Introduction to Effect Verification" in Python
[Introduction to Python3 Day 1] Programming and Python
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part1-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part4-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part3-
Fourier series verification code written in Python
[Introduction to Python] How to use class in Python?
Modules and packages in Python are "namespaces"
How to use is and == in Python
Introduction to Vectors: Linear Algebra in Python <1>
How to generate permutations in Python and C ++
[Introduction to Python3 Day 12] Chapter 6 Objects and Classes (6.3-6.15)
tse --Introduction to Text Stream Editor in Python
[Introduction to Python3 Day 22] Chapter 11 Concurrency and Networking (11.1 to 11.3)
Send messages to Skype and Chatwork in Python
[Introduction to Udemy Python3 + Application] 64. Namespace and Scope
[Introduction to Python3 Day 11] Chapter 6 Objects and Classes (6.1-6.2)
To represent date, time, time, and seconds in Python
How to plot autocorrelation and partial autocorrelation in python
Tips for those who are wondering how to use is and == in Python
Introduction to Time Series Analysis ~ Seasonal Adjustment Model ~ Implemented in R and Python
Python learning notes for machine learning with Chainer Chapters 11 and 12 Introduction to Pandas Matplotlib
Introduction to Python language
Introduction to OpenCV (python)-(2)
[Introduction to Udemy Python3 + Application] 35. Comparison operators and logical operators
Convert timezoned date and time to Unixtime in Python2.7
Parse a JSON string written to a file in Python
Write tests in Python to profile and check coverage
[Python] How to sort dict in list and instance in list
Introduction to Linear Algebra in Python: A = LU Decomposition
[Introduction to Udemy Python3 + Application] 68. Import statement and AS
[Introduction to element decomposition] Let's arrange time series analysis methods in R and python ♬
"Introduction to effect verification Chapter 3 Analysis using propensity score" + α is tried in Python
[Introduction to Python] Summary of functions and methods that frequently appear in Python [Problem format]
[Introduction to cx_Oracle] (Part 6) DB and Python data type mapping
Script to count and stop up to 5 seconds in Python in Blender
Try to make it using GUI and PyQt in Python
To flush stdout in Python
Application to display and search local memos (diary) in Python
[Introduction to Udemy Python3 + Application] 42. for statement, break statement, and continue statement
[Introduction to Udemy Python3 + Application] 39. while statement, continue statement and break statement
Introduction to serial communication [Python]
Display numbers and letters assigned to variables in python print
Tips for coding short and easy to read in Python
A standard way to develop and distribute packages in Python
Useful tricks related to list and for statements in Python
Design Patterns in Python: Introduction
Comparison of how to use higher-order functions in Python 2 and 3
An introduction to statistical modeling for data analysis (Midorimoto) reading notes (in Python and Stan)
Stack and Queue in Python
Speech to speech in python [text to speech]
[Introduction to Data Scientists] Basics of Python ♬ Functions and classes
[Introduction to Udemy Python3 + Application] 50. Positional arguments, keyword arguments, and default arguments
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
Gacha written in Python -BOX gacha-