SlideShare a Scribd company logo
1 of 21
1
Causal Inference in R
Ana Daglis, Farfetch
2
Farfetch
Customer
Customer
Customer
Customer
Customer
Boutique
Boutique
Boutique
Boutique
Boutique
3
One of the most common questions we face in
marketing is measuring the incremental effects
โ— How much incremental revenue did the new pricing
strategy drive?
โ— What impact did the new feature on the website have?
โ— How many incremental conversions were achieved by
increasing the commission rate for our affiliates?
โ— โ€ฆ
4
The main gold standard method for estimating causal
effects is a randomised experiment
10%
Conversion
15%
Conversion
50% of visitors see Version B
Version B
Version A
50% of visitors see Version A
5
However, often A/B tests are either too expensive to run
or cannot be run, e.g. due to legal reasons
15%
Conversion
100% of visitors see Version B
Version B
Version A
6
Example: financial performance of a company A
80
120
160
200
2011 2012 2013 2014 2015 2016 2017
Date
Adjusted
Closing
Price
Scandal broke
Actual share
price
7
Approach: estimate the share price had the scandal not
happened
100
150
200
2011 2012 2013 2014 2015 2016 2017
Date
Adjusted
Closing
Price
Scandal broke
Actual share
price
Predicted
share price
8
By comparing the actual and predicted share price, we
can estimate the drop in stock value due to the scandal
100
150
200
2011 2012 2013 2014 2015 2016 2017
Date
Adjusted
Closing
Price
Scandal broke
Drop in stock
value due to
scandal
Actual share
price
Predicted
share price
9
Thanks to a fully Bayesian approach, we can quantify
the confidence level of our predictions
100
150
200
250
2011 2012 2013 2014 2015 2016 2017
Day
Clicks
Scandal broke
Actual share
price
Predicted
share price
95% credible
interval
10
How do we construct the counterfactual estimate?
50
100
150
200
250
2011 2012 2013 2014 2015 2016 2017
Date
Adjusted
Closing
Price
Actual share
price
Predicted
share price
95% credible
interval
Company B
share price
Company C
share price
Training Prediction
Scandal broke
11
Causal Impact methodology is based on a Bayesian
structural time series model
๐‘ฆ๐‘ก = ๐‘๐‘ก
๐‘‡
๐›ผ๐‘ก + ๐œ€๐‘ก
๐›ผ๐‘ก+1 = ๐‘‡๐‘ก
๐‘‡
๐›ผ๐‘ก + ๐‘…๐‘ก๐œ‚๐‘ก
Causal Impact model
Most general form of the model
Observation equation
State equation
๐‘ฆ๐‘ก = ๐œ‡๐‘ก + ๐œ๐‘ก + ๐‘ฅ๐‘ก
๐‘‡
๐›ฝ + ๐œ€๐‘ก
๐œ‡๐‘ก+1 = ๐œ‡๐‘ก + ๐›ฟ๐‘ก + ๐œ‚๐œ‡,๐‘ก
๐›ฟ๐‘ก+1 = ๐›ฟ๐‘ก + ๐œ‚๐›ฟ,๐‘ก
๐œ๐‘ก+1 = โˆ’
๐‘–=0
๐‘†โˆ’2
๐œ๐‘กโˆ’๐‘– + ๐œ‚๐œ,๐‘ก
12
The model has 5 main parameters: 4 variance terms
๐ˆ๐œบ
๐Ÿ
, ๐ˆ๐
๐Ÿ
, ๐ˆ๐œน
๐Ÿ
, ๐ˆ๐‰
๐Ÿ
and regression coefficients ๐œท
๐‘ฆ๐‘ก = ๐œ‡๐‘ก + ๐œ๐‘ก + ๐‘ฅ๐‘ก
๐‘‡
๐›ฝ + ๐œ€๐‘ก
๐œ‡๐‘ก+1 = ๐œ‡๐‘ก + ๐›ฟ๐‘ก + ๐œ‚๐œ‡,๐‘ก
๐›ฟ๐‘ก+1 = ๐›ฟ๐‘ก + ๐œ‚๐›ฟ,๐‘ก
๐œ๐‘ก+1 = โˆ’
๐‘–=0
๐‘†โˆ’2
๐œ๐‘กโˆ’๐‘– + ๐œ‚๐œ,๐‘ก
~๐’ฉ 0, ๐œŽ๐œ€
2
~๐’ฉ 0, ๐œŽ๐œ‡
2
~๐’ฉ 0, ๐œŽ๐›ฟ
2
~๐’ฉ 0, ๐œŽ๐œ
2
13
We impose an inv-gamma prior on ๐ˆ๐œบ
๐Ÿ
, with parameters ๐’”๐œบ
and ๐’—๐œบ selected based on the expected goodness-of-fit
๐‘ฆ๐‘ก = ๐œ‡๐‘ก + ๐œ๐‘ก + ๐‘ฅ๐‘ก
๐‘‡
๐›ฝ + ๐œ€๐‘ก
๐œ‡๐‘ก+1 = ๐œ‡๐‘ก + ๐›ฟ๐‘ก + ๐œ‚๐œ‡,๐‘ก
๐›ฟ๐‘ก+1 = ๐›ฟ๐‘ก + ๐œ‚๐›ฟ,๐‘ก
๐œ๐‘ก+1 = โˆ’
๐‘–=0
๐‘†โˆ’2
๐œ๐‘กโˆ’๐‘– + ๐œ‚๐œ,๐‘ก
~๐’ฉ 0, ๐œŽ๐œ€
2
~๐’ฉ 0, ๐œŽ๐œ‡
2
~๐’ฉ 0, ๐œŽ๐›ฟ
2
~๐’ฉ 0, ๐œŽ๐œ
2
Priors
๐œŽ๐œ€
2
~ ๐ผ๐‘›๐‘ฃโˆ’๐บ๐‘Ž๐‘š๐‘š๐‘Ž ๐‘ ๐œ€, ๐‘ฃ๐œ€
0
1
2
3
4
0 1 2 3
x
Probability
Density
a = 1, b = 1
a = 2, b = 1
a = 3, b = 1
a = 3, b = 0.5
Invโˆ’Gamma(a,b) density for varying values of a and b
14
We impose weak priors on ๐ˆ๐
๐Ÿ, ๐ˆ๐œน
๐Ÿ
and ๐ˆ๐‰
๐Ÿ reflecting the
assumption that errors are small in the state process
๐‘ฆ๐‘ก = ๐œ‡๐‘ก + ๐œ๐‘ก + ๐‘ฅ๐‘ก
๐‘‡
๐›ฝ + ๐œ€๐‘ก
๐œ‡๐‘ก+1 = ๐œ‡๐‘ก + ๐›ฟ๐‘ก + ๐œ‚๐œ‡,๐‘ก
๐›ฟ๐‘ก+1 = ๐›ฟ๐‘ก + ๐œ‚๐›ฟ,๐‘ก
๐œ๐‘ก+1 = โˆ’
๐‘–=0
๐‘†โˆ’2
๐œ๐‘กโˆ’๐‘– + ๐œ‚๐œ,๐‘ก
~๐’ฉ 0, ๐œŽ๐œ€
2
~๐’ฉ 0, ๐œŽ๐œ‡
2
~๐’ฉ 0, ๐œŽ๐›ฟ
2
~๐’ฉ 0, ๐œŽ๐œ
2
Priors
๐œŽ๐œ‡
2
, ๐œŽ๐›ฟ
2
, ๐œŽ๐œ
2
~ ๐ผ๐‘›๐‘ฃโˆ’๐บ๐‘Ž๐‘š๐‘š๐‘Ž 1, 0.01 ร— ๐‘‰๐‘Ž๐‘Ÿ(๐‘ฆ)
0
1
2
3
4
0 1 2 3
x
Probability
Density
a = 1, b = 1
a = 2, b = 1
a = 3, b = 1
a = 3, b = 0.5
Invโˆ’Gamma(a,b) density for varying values of a and b
15
We let the model choose an appropriate set of controls
by placing a spike and slab prior over coefficients ๐œท
๐‘ฆ๐‘ก = ๐œ‡๐‘ก + ๐œ๐‘ก + ๐‘ฅ๐‘ก
๐‘‡
๐›ฝ + ๐œ€๐‘ก
๐œ‡๐‘ก+1 = ๐œ‡๐‘ก + ๐›ฟ๐‘ก + ๐œ‚๐œ‡,๐‘ก
๐›ฟ๐‘ก+1 = ๐›ฟ๐‘ก + ๐œ‚๐›ฟ,๐‘ก
๐œ๐‘ก+1 = โˆ’
๐‘–=0
๐‘†โˆ’2
๐œ๐‘กโˆ’๐‘– + ๐œ‚๐œ,๐‘ก
~๐’ฉ 0, ๐œŽ๐œ€
2
~๐’ฉ 0, ๐œŽ๐œ‡
2
~๐’ฉ 0, ๐œŽ๐›ฟ
2
~๐’ฉ 0, ๐œŽ๐œ
2
Priors
๐›ฝ๐›พ|๐œŽ๐œ€
2
~ ๐’ฉ(0, ๐‘›๐œŽ๐œ€
2
๐‘‹๐‘‡
๐‘‹ โˆ’1
)
๐‘ ๐œš ~
๐‘—=1
๐ฝ
๐œ‹๐‘—
๐œš๐‘—
(1 โˆ’ ๐œ‹๐‘—)
๐œš๐‘—
0
1
2
3
4
โˆ’2 โˆ’1 0 1 2
x
Probability
Density
Spike
Slab
Density functions of spike and slab priors
16
The inference can be performed in R with just 6 lines of
code
1 library(CausalImpact)
2 pre.period <- as.Date(c("2011-01-03", "2015-09-14"))
3 post.period <- as.Date(c("2015-09-21", "2017-03-19"))
4 impact <- CausalImpact(data, pre.period, post.period)
5 plot(impact)
6 summary(impact)
17
Results can be plotted and summarised in a table
original
pointwise
cumulative
2011 2012 2013 2014 2015 2016 2017
100
150
200
250
โˆ’80
โˆ’40
0
โˆ’4000
โˆ’3000
โˆ’2000
โˆ’1000
0
Date
Adjusted
Closing
Price
Cumulative panel only makes sense when the metric is
additive, such as clicks or the number of orders, but not
in the case when it is a share price
18
The package can even write a report for you!
19
Additional considerations
โ— It is important that covariates included in the model are not
themselves affected by the event. For each covariate included,
it is critical to reason why this is the case.
โ— The model can be validated by running the Causal Impact
analysis on an โ€˜imaginary eventโ€™ before the actual event. We
should not be seeing any significant effect, and actual and
predicted lines should match reasonably closely before the actual
event.
20
References
โ— K.H. Brodersen, F. Gallusser, J. Koehler, N. Remy, S. L. Scott,
(2015). Inferring Causal Impact Using Bayesian Structural Time-
Series Models.
https://research.google.com/pubs/pub41854.html.
โ— S. L. Scott, H. Varian, (2013). Predicting the Present with
Bayesian Structural Time Series.
https://people.ischool.berkeley.edu/~hal/Papers/2013/pred-
present-with-bsts.pdf.
21
Thank you!

More Related Content

Similar to Causal Inference in R

A PRACTICAL POWERFUL ROBUST AND INTERPRETABLE FAMILY OF CORRELATION COEFFICIE...
A PRACTICAL POWERFUL ROBUST AND INTERPRETABLE FAMILY OF CORRELATION COEFFICIE...A PRACTICAL POWERFUL ROBUST AND INTERPRETABLE FAMILY OF CORRELATION COEFFICIE...
A PRACTICAL POWERFUL ROBUST AND INTERPRETABLE FAMILY OF CORRELATION COEFFICIE...Savas Papadopoulos, Ph.D
ย 
Lecture Notes: EEEC4340318 Instrumentation and Control Systems - Fundamental...
Lecture Notes:  EEEC4340318 Instrumentation and Control Systems - Fundamental...Lecture Notes:  EEEC4340318 Instrumentation and Control Systems - Fundamental...
Lecture Notes: EEEC4340318 Instrumentation and Control Systems - Fundamental...AIMST University
ย 
Approximate Dynamic Programming: A New Paradigm for Process Control & Optimiz...
Approximate Dynamic Programming: A New Paradigm for Process Control & Optimiz...Approximate Dynamic Programming: A New Paradigm for Process Control & Optimiz...
Approximate Dynamic Programming: A New Paradigm for Process Control & Optimiz...height
ย 
Confirmatory Bayesian Online Change Point Detection in the Covariance Structu...
Confirmatory Bayesian Online Change Point Detection in the Covariance Structu...Confirmatory Bayesian Online Change Point Detection in the Covariance Structu...
Confirmatory Bayesian Online Change Point Detection in the Covariance Structu...JeeyeonHan
ย 
Pricing optimization poster version 2 (1)
Pricing optimization poster version 2 (1)Pricing optimization poster version 2 (1)
Pricing optimization poster version 2 (1)Alex Potocki
ย 
Doe introductionh
Doe introductionhDoe introductionh
Doe introductionhsumandaspersonal
ย 
Statistical Process Control - SPC
Statistical Process Control - SPCStatistical Process Control - SPC
Statistical Process Control - SPCPrasenjit Puri
ย 
Federated Tensor Factorization for Computational Phenotyping
Federated Tensor Factorization for Computational PhenotypingFederated Tensor Factorization for Computational Phenotyping
Federated Tensor Factorization for Computational PhenotypingYejin Kim
ย 
Regression Analysis
Regression AnalysisRegression Analysis
Regression AnalysisShiela Vinarao
ย 
Regression Analysis
Regression AnalysisRegression Analysis
Regression AnalysisShiela Vinarao
ย 
Intro to Quantitative Investment (Lecture 4 of 6)
Intro to Quantitative Investment (Lecture 4 of 6)Intro to Quantitative Investment (Lecture 4 of 6)
Intro to Quantitative Investment (Lecture 4 of 6)Adrian Aley
ย 
presentation_python_11_1569171345_375360.pptx
presentation_python_11_1569171345_375360.pptxpresentation_python_11_1569171345_375360.pptx
presentation_python_11_1569171345_375360.pptxGAURAVRATHORE86
ย 
newmicrosoftofficepowerpointpresentation-150826055944-lva1-app6891.pdf
newmicrosoftofficepowerpointpresentation-150826055944-lva1-app6891.pdfnewmicrosoftofficepowerpointpresentation-150826055944-lva1-app6891.pdf
newmicrosoftofficepowerpointpresentation-150826055944-lva1-app6891.pdfBiswajitPalei2
ย 
IE-002 Control Chart For Variables
IE-002 Control Chart For VariablesIE-002 Control Chart For Variables
IE-002 Control Chart For Variableshandbook
ย 
A Short Study of Galois Field
A Short Study of Galois FieldA Short Study of Galois Field
A Short Study of Galois FieldHazratali Naim
ย 
Interpreting test score ~ Language Testing
Interpreting test score ~ Language Testing Interpreting test score ~ Language Testing
Interpreting test score ~ Language Testing AndiSyafitriSyam
ย 

Similar to Causal Inference in R (20)

A PRACTICAL POWERFUL ROBUST AND INTERPRETABLE FAMILY OF CORRELATION COEFFICIE...
A PRACTICAL POWERFUL ROBUST AND INTERPRETABLE FAMILY OF CORRELATION COEFFICIE...A PRACTICAL POWERFUL ROBUST AND INTERPRETABLE FAMILY OF CORRELATION COEFFICIE...
A PRACTICAL POWERFUL ROBUST AND INTERPRETABLE FAMILY OF CORRELATION COEFFICIE...
ย 
Lecture Notes: EEEC4340318 Instrumentation and Control Systems - Fundamental...
Lecture Notes:  EEEC4340318 Instrumentation and Control Systems - Fundamental...Lecture Notes:  EEEC4340318 Instrumentation and Control Systems - Fundamental...
Lecture Notes: EEEC4340318 Instrumentation and Control Systems - Fundamental...
ย 
Approximate Dynamic Programming: A New Paradigm for Process Control & Optimiz...
Approximate Dynamic Programming: A New Paradigm for Process Control & Optimiz...Approximate Dynamic Programming: A New Paradigm for Process Control & Optimiz...
Approximate Dynamic Programming: A New Paradigm for Process Control & Optimiz...
ย 
Confirmatory Bayesian Online Change Point Detection in the Covariance Structu...
Confirmatory Bayesian Online Change Point Detection in the Covariance Structu...Confirmatory Bayesian Online Change Point Detection in the Covariance Structu...
Confirmatory Bayesian Online Change Point Detection in the Covariance Structu...
ย 
Statistics Assignment Help
Statistics Assignment HelpStatistics Assignment Help
Statistics Assignment Help
ย 
Pricing optimization poster version 2 (1)
Pricing optimization poster version 2 (1)Pricing optimization poster version 2 (1)
Pricing optimization poster version 2 (1)
ย 
Doe introductionh
Doe introductionhDoe introductionh
Doe introductionh
ย 
Statistical Process Control - SPC
Statistical Process Control - SPCStatistical Process Control - SPC
Statistical Process Control - SPC
ย 
Federated Tensor Factorization for Computational Phenotyping
Federated Tensor Factorization for Computational PhenotypingFederated Tensor Factorization for Computational Phenotyping
Federated Tensor Factorization for Computational Phenotyping
ย 
Py data19 final
Py data19   finalPy data19   final
Py data19 final
ย 
Regression Analysis
Regression AnalysisRegression Analysis
Regression Analysis
ย 
Regression Analysis
Regression AnalysisRegression Analysis
Regression Analysis
ย 
Intro to Quantitative Investment (Lecture 4 of 6)
Intro to Quantitative Investment (Lecture 4 of 6)Intro to Quantitative Investment (Lecture 4 of 6)
Intro to Quantitative Investment (Lecture 4 of 6)
ย 
presentation_python_11_1569171345_375360.pptx
presentation_python_11_1569171345_375360.pptxpresentation_python_11_1569171345_375360.pptx
presentation_python_11_1569171345_375360.pptx
ย 
Curvefitting
CurvefittingCurvefitting
Curvefitting
ย 
newmicrosoftofficepowerpointpresentation-150826055944-lva1-app6891.pdf
newmicrosoftofficepowerpointpresentation-150826055944-lva1-app6891.pdfnewmicrosoftofficepowerpointpresentation-150826055944-lva1-app6891.pdf
newmicrosoftofficepowerpointpresentation-150826055944-lva1-app6891.pdf
ย 
IE-002 Control Chart For Variables
IE-002 Control Chart For VariablesIE-002 Control Chart For Variables
IE-002 Control Chart For Variables
ย 
A Short Study of Galois Field
A Short Study of Galois FieldA Short Study of Galois Field
A Short Study of Galois Field
ย 
Interpreting test score ~ Language Testing
Interpreting test score ~ Language Testing Interpreting test score ~ Language Testing
Interpreting test score ~ Language Testing
ย 
Cat Quant Cheat Sheet
Cat Quant Cheat SheetCat Quant Cheat Sheet
Cat Quant Cheat Sheet
ย 

Recently uploaded

Decoding Loan Approval: Predictive Modeling in Action
Decoding Loan Approval: Predictive Modeling in ActionDecoding Loan Approval: Predictive Modeling in Action
Decoding Loan Approval: Predictive Modeling in ActionBoston Institute of Analytics
ย 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130Suhani Kapoor
ย 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Sapana Sha
ย 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts ServiceSapana Sha
ย 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...Pooja Nehwal
ย 
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...Suhani Kapoor
ย 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...Suhani Kapoor
ย 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
ย 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubaihf8803863
ย 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998YohFuh
ย 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
ย 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptxAnupama Kate
ย 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
ย 
ไปฃๅŠžๅ›ฝๅค–ๅคงๅญฆๆ–‡ๅ‡ญใ€ŠๅŽŸ็‰ˆ็พŽๅ›ฝUCLAๆ–‡ๅ‡ญ่ฏไนฆใ€‹ๅŠ ๅทžๅคงๅญฆๆด›ๆ‰็Ÿถๅˆ†ๆ กๆฏ•ไธš่ฏๅˆถไฝœๆˆ็ปฉๅ•ไฟฎๆ”น
ไปฃๅŠžๅ›ฝๅค–ๅคงๅญฆๆ–‡ๅ‡ญใ€ŠๅŽŸ็‰ˆ็พŽๅ›ฝUCLAๆ–‡ๅ‡ญ่ฏไนฆใ€‹ๅŠ ๅทžๅคงๅญฆๆด›ๆ‰็Ÿถๅˆ†ๆ กๆฏ•ไธš่ฏๅˆถไฝœๆˆ็ปฉๅ•ไฟฎๆ”นไปฃๅŠžๅ›ฝๅค–ๅคงๅญฆๆ–‡ๅ‡ญใ€ŠๅŽŸ็‰ˆ็พŽๅ›ฝUCLAๆ–‡ๅ‡ญ่ฏไนฆใ€‹ๅŠ ๅทžๅคงๅญฆๆด›ๆ‰็Ÿถๅˆ†ๆ กๆฏ•ไธš่ฏๅˆถไฝœๆˆ็ปฉๅ•ไฟฎๆ”น
ไปฃๅŠžๅ›ฝๅค–ๅคงๅญฆๆ–‡ๅ‡ญใ€ŠๅŽŸ็‰ˆ็พŽๅ›ฝUCLAๆ–‡ๅ‡ญ่ฏไนฆใ€‹ๅŠ ๅทžๅคงๅญฆๆด›ๆ‰็Ÿถๅˆ†ๆ กๆฏ•ไธš่ฏๅˆถไฝœๆˆ็ปฉๅ•ไฟฎๆ”นatducpo
ย 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystSamantha Rae Coolbeth
ย 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
ย 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz1
ย 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfSocial Samosa
ย 

Recently uploaded (20)

Decoding Loan Approval: Predictive Modeling in Action
Decoding Loan Approval: Predictive Modeling in ActionDecoding Loan Approval: Predictive Modeling in Action
Decoding Loan Approval: Predictive Modeling in Action
ย 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
ย 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
ย 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts Service
ย 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
ย 
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
ย 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
ย 
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
ย 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
ย 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
ย 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998
ย 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
ย 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
ย 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
ย 
ไปฃๅŠžๅ›ฝๅค–ๅคงๅญฆๆ–‡ๅ‡ญใ€ŠๅŽŸ็‰ˆ็พŽๅ›ฝUCLAๆ–‡ๅ‡ญ่ฏไนฆใ€‹ๅŠ ๅทžๅคงๅญฆๆด›ๆ‰็Ÿถๅˆ†ๆ กๆฏ•ไธš่ฏๅˆถไฝœๆˆ็ปฉๅ•ไฟฎๆ”น
ไปฃๅŠžๅ›ฝๅค–ๅคงๅญฆๆ–‡ๅ‡ญใ€ŠๅŽŸ็‰ˆ็พŽๅ›ฝUCLAๆ–‡ๅ‡ญ่ฏไนฆใ€‹ๅŠ ๅทžๅคงๅญฆๆด›ๆ‰็Ÿถๅˆ†ๆ กๆฏ•ไธš่ฏๅˆถไฝœๆˆ็ปฉๅ•ไฟฎๆ”นไปฃๅŠžๅ›ฝๅค–ๅคงๅญฆๆ–‡ๅ‡ญใ€ŠๅŽŸ็‰ˆ็พŽๅ›ฝUCLAๆ–‡ๅ‡ญ่ฏไนฆใ€‹ๅŠ ๅทžๅคงๅญฆๆด›ๆ‰็Ÿถๅˆ†ๆ กๆฏ•ไธš่ฏๅˆถไฝœๆˆ็ปฉๅ•ไฟฎๆ”น
ไปฃๅŠžๅ›ฝๅค–ๅคงๅญฆๆ–‡ๅ‡ญใ€ŠๅŽŸ็‰ˆ็พŽๅ›ฝUCLAๆ–‡ๅ‡ญ่ฏไนฆใ€‹ๅŠ ๅทžๅคงๅญฆๆด›ๆ‰็Ÿถๅˆ†ๆ กๆฏ•ไธš่ฏๅˆถไฝœๆˆ็ปฉๅ•ไฟฎๆ”น
ย 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data Analyst
ย 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
ย 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signals
ย 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
ย 
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
ย 

Causal Inference in R

  • 1. 1 Causal Inference in R Ana Daglis, Farfetch
  • 3. 3 One of the most common questions we face in marketing is measuring the incremental effects โ— How much incremental revenue did the new pricing strategy drive? โ— What impact did the new feature on the website have? โ— How many incremental conversions were achieved by increasing the commission rate for our affiliates? โ— โ€ฆ
  • 4. 4 The main gold standard method for estimating causal effects is a randomised experiment 10% Conversion 15% Conversion 50% of visitors see Version B Version B Version A 50% of visitors see Version A
  • 5. 5 However, often A/B tests are either too expensive to run or cannot be run, e.g. due to legal reasons 15% Conversion 100% of visitors see Version B Version B Version A
  • 6. 6 Example: financial performance of a company A 80 120 160 200 2011 2012 2013 2014 2015 2016 2017 Date Adjusted Closing Price Scandal broke Actual share price
  • 7. 7 Approach: estimate the share price had the scandal not happened 100 150 200 2011 2012 2013 2014 2015 2016 2017 Date Adjusted Closing Price Scandal broke Actual share price Predicted share price
  • 8. 8 By comparing the actual and predicted share price, we can estimate the drop in stock value due to the scandal 100 150 200 2011 2012 2013 2014 2015 2016 2017 Date Adjusted Closing Price Scandal broke Drop in stock value due to scandal Actual share price Predicted share price
  • 9. 9 Thanks to a fully Bayesian approach, we can quantify the confidence level of our predictions 100 150 200 250 2011 2012 2013 2014 2015 2016 2017 Day Clicks Scandal broke Actual share price Predicted share price 95% credible interval
  • 10. 10 How do we construct the counterfactual estimate? 50 100 150 200 250 2011 2012 2013 2014 2015 2016 2017 Date Adjusted Closing Price Actual share price Predicted share price 95% credible interval Company B share price Company C share price Training Prediction Scandal broke
  • 11. 11 Causal Impact methodology is based on a Bayesian structural time series model ๐‘ฆ๐‘ก = ๐‘๐‘ก ๐‘‡ ๐›ผ๐‘ก + ๐œ€๐‘ก ๐›ผ๐‘ก+1 = ๐‘‡๐‘ก ๐‘‡ ๐›ผ๐‘ก + ๐‘…๐‘ก๐œ‚๐‘ก Causal Impact model Most general form of the model Observation equation State equation ๐‘ฆ๐‘ก = ๐œ‡๐‘ก + ๐œ๐‘ก + ๐‘ฅ๐‘ก ๐‘‡ ๐›ฝ + ๐œ€๐‘ก ๐œ‡๐‘ก+1 = ๐œ‡๐‘ก + ๐›ฟ๐‘ก + ๐œ‚๐œ‡,๐‘ก ๐›ฟ๐‘ก+1 = ๐›ฟ๐‘ก + ๐œ‚๐›ฟ,๐‘ก ๐œ๐‘ก+1 = โˆ’ ๐‘–=0 ๐‘†โˆ’2 ๐œ๐‘กโˆ’๐‘– + ๐œ‚๐œ,๐‘ก
  • 12. 12 The model has 5 main parameters: 4 variance terms ๐ˆ๐œบ ๐Ÿ , ๐ˆ๐ ๐Ÿ , ๐ˆ๐œน ๐Ÿ , ๐ˆ๐‰ ๐Ÿ and regression coefficients ๐œท ๐‘ฆ๐‘ก = ๐œ‡๐‘ก + ๐œ๐‘ก + ๐‘ฅ๐‘ก ๐‘‡ ๐›ฝ + ๐œ€๐‘ก ๐œ‡๐‘ก+1 = ๐œ‡๐‘ก + ๐›ฟ๐‘ก + ๐œ‚๐œ‡,๐‘ก ๐›ฟ๐‘ก+1 = ๐›ฟ๐‘ก + ๐œ‚๐›ฟ,๐‘ก ๐œ๐‘ก+1 = โˆ’ ๐‘–=0 ๐‘†โˆ’2 ๐œ๐‘กโˆ’๐‘– + ๐œ‚๐œ,๐‘ก ~๐’ฉ 0, ๐œŽ๐œ€ 2 ~๐’ฉ 0, ๐œŽ๐œ‡ 2 ~๐’ฉ 0, ๐œŽ๐›ฟ 2 ~๐’ฉ 0, ๐œŽ๐œ 2
  • 13. 13 We impose an inv-gamma prior on ๐ˆ๐œบ ๐Ÿ , with parameters ๐’”๐œบ and ๐’—๐œบ selected based on the expected goodness-of-fit ๐‘ฆ๐‘ก = ๐œ‡๐‘ก + ๐œ๐‘ก + ๐‘ฅ๐‘ก ๐‘‡ ๐›ฝ + ๐œ€๐‘ก ๐œ‡๐‘ก+1 = ๐œ‡๐‘ก + ๐›ฟ๐‘ก + ๐œ‚๐œ‡,๐‘ก ๐›ฟ๐‘ก+1 = ๐›ฟ๐‘ก + ๐œ‚๐›ฟ,๐‘ก ๐œ๐‘ก+1 = โˆ’ ๐‘–=0 ๐‘†โˆ’2 ๐œ๐‘กโˆ’๐‘– + ๐œ‚๐œ,๐‘ก ~๐’ฉ 0, ๐œŽ๐œ€ 2 ~๐’ฉ 0, ๐œŽ๐œ‡ 2 ~๐’ฉ 0, ๐œŽ๐›ฟ 2 ~๐’ฉ 0, ๐œŽ๐œ 2 Priors ๐œŽ๐œ€ 2 ~ ๐ผ๐‘›๐‘ฃโˆ’๐บ๐‘Ž๐‘š๐‘š๐‘Ž ๐‘ ๐œ€, ๐‘ฃ๐œ€ 0 1 2 3 4 0 1 2 3 x Probability Density a = 1, b = 1 a = 2, b = 1 a = 3, b = 1 a = 3, b = 0.5 Invโˆ’Gamma(a,b) density for varying values of a and b
  • 14. 14 We impose weak priors on ๐ˆ๐ ๐Ÿ, ๐ˆ๐œน ๐Ÿ and ๐ˆ๐‰ ๐Ÿ reflecting the assumption that errors are small in the state process ๐‘ฆ๐‘ก = ๐œ‡๐‘ก + ๐œ๐‘ก + ๐‘ฅ๐‘ก ๐‘‡ ๐›ฝ + ๐œ€๐‘ก ๐œ‡๐‘ก+1 = ๐œ‡๐‘ก + ๐›ฟ๐‘ก + ๐œ‚๐œ‡,๐‘ก ๐›ฟ๐‘ก+1 = ๐›ฟ๐‘ก + ๐œ‚๐›ฟ,๐‘ก ๐œ๐‘ก+1 = โˆ’ ๐‘–=0 ๐‘†โˆ’2 ๐œ๐‘กโˆ’๐‘– + ๐œ‚๐œ,๐‘ก ~๐’ฉ 0, ๐œŽ๐œ€ 2 ~๐’ฉ 0, ๐œŽ๐œ‡ 2 ~๐’ฉ 0, ๐œŽ๐›ฟ 2 ~๐’ฉ 0, ๐œŽ๐œ 2 Priors ๐œŽ๐œ‡ 2 , ๐œŽ๐›ฟ 2 , ๐œŽ๐œ 2 ~ ๐ผ๐‘›๐‘ฃโˆ’๐บ๐‘Ž๐‘š๐‘š๐‘Ž 1, 0.01 ร— ๐‘‰๐‘Ž๐‘Ÿ(๐‘ฆ) 0 1 2 3 4 0 1 2 3 x Probability Density a = 1, b = 1 a = 2, b = 1 a = 3, b = 1 a = 3, b = 0.5 Invโˆ’Gamma(a,b) density for varying values of a and b
  • 15. 15 We let the model choose an appropriate set of controls by placing a spike and slab prior over coefficients ๐œท ๐‘ฆ๐‘ก = ๐œ‡๐‘ก + ๐œ๐‘ก + ๐‘ฅ๐‘ก ๐‘‡ ๐›ฝ + ๐œ€๐‘ก ๐œ‡๐‘ก+1 = ๐œ‡๐‘ก + ๐›ฟ๐‘ก + ๐œ‚๐œ‡,๐‘ก ๐›ฟ๐‘ก+1 = ๐›ฟ๐‘ก + ๐œ‚๐›ฟ,๐‘ก ๐œ๐‘ก+1 = โˆ’ ๐‘–=0 ๐‘†โˆ’2 ๐œ๐‘กโˆ’๐‘– + ๐œ‚๐œ,๐‘ก ~๐’ฉ 0, ๐œŽ๐œ€ 2 ~๐’ฉ 0, ๐œŽ๐œ‡ 2 ~๐’ฉ 0, ๐œŽ๐›ฟ 2 ~๐’ฉ 0, ๐œŽ๐œ 2 Priors ๐›ฝ๐›พ|๐œŽ๐œ€ 2 ~ ๐’ฉ(0, ๐‘›๐œŽ๐œ€ 2 ๐‘‹๐‘‡ ๐‘‹ โˆ’1 ) ๐‘ ๐œš ~ ๐‘—=1 ๐ฝ ๐œ‹๐‘— ๐œš๐‘— (1 โˆ’ ๐œ‹๐‘—) ๐œš๐‘— 0 1 2 3 4 โˆ’2 โˆ’1 0 1 2 x Probability Density Spike Slab Density functions of spike and slab priors
  • 16. 16 The inference can be performed in R with just 6 lines of code 1 library(CausalImpact) 2 pre.period <- as.Date(c("2011-01-03", "2015-09-14")) 3 post.period <- as.Date(c("2015-09-21", "2017-03-19")) 4 impact <- CausalImpact(data, pre.period, post.period) 5 plot(impact) 6 summary(impact)
  • 17. 17 Results can be plotted and summarised in a table original pointwise cumulative 2011 2012 2013 2014 2015 2016 2017 100 150 200 250 โˆ’80 โˆ’40 0 โˆ’4000 โˆ’3000 โˆ’2000 โˆ’1000 0 Date Adjusted Closing Price Cumulative panel only makes sense when the metric is additive, such as clicks or the number of orders, but not in the case when it is a share price
  • 18. 18 The package can even write a report for you!
  • 19. 19 Additional considerations โ— It is important that covariates included in the model are not themselves affected by the event. For each covariate included, it is critical to reason why this is the case. โ— The model can be validated by running the Causal Impact analysis on an โ€˜imaginary eventโ€™ before the actual event. We should not be seeing any significant effect, and actual and predicted lines should match reasonably closely before the actual event.
  • 20. 20 References โ— K.H. Brodersen, F. Gallusser, J. Koehler, N. Remy, S. L. Scott, (2015). Inferring Causal Impact Using Bayesian Structural Time- Series Models. https://research.google.com/pubs/pub41854.html. โ— S. L. Scott, H. Varian, (2013). Predicting the Present with Bayesian Structural Time Series. https://people.ischool.berkeley.edu/~hal/Papers/2013/pred- present-with-bsts.pdf.