-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulation Analysis.R
More file actions
208 lines (186 loc) · 7.26 KB
/
Copy pathSimulation Analysis.R
File metadata and controls
208 lines (186 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
library(rethinking)
library(tidyverse)
library(foreach)
library(ggrepel)
library(lubridate)
standard_theme = function () {
theme (panel.background = element_rect (fill = 'white'),
panel.grid.major = element_line (colour = 'grey90', size = 0.20),
panel.grid.minor = element_line (colour = 'grey90', size = 0.10),
plot.title = element_text (size = 18, lineheight = 1.15),
plot.subtitle = element_text (size = 12, lineheight = 1.15),
axis.title.y = element_text (angle = 90),
axis.title = element_text (size = 12),
text = element_text (family = 'Avenir', size = 12),
legend.title = element_text (size = 12),
strip.text = element_text (size = 10, angle = 0),
strip.background = element_rect (fill = 'grey97'),
plot.caption = element_text (hjust = 0, size = 9))
}
floor_line = function (yintercept = 0, ...) {
geom_hline (yintercept = yintercept, colour = 'grey75', size = 0.3, ...)
}
save_plot = function(plot = last_plot(), path = '~/Documents/R.png'){
ggsave(paste0(path, ".png"), plot, device=NULL, dpi=500, units="in", height = 6, width=10)
}
beta_comparison = function(results, beta_prior = c(1, 1), sample_size=1E4){
distribution = rbeta(sample_size, results[1] + beta_prior[1], results[2] + beta_prior[2])
return (c(mean(distribution), quantile(distribution, c(0.05, 0.5, 0.95))))
}
simulations_data = read_csv('/Users/raphaeltamaki/Documents/personal_git/causal_methodologies_analysis/results/simulations_results.csv') %>%
mutate(
bias = (estimated_ate - true_ate) / true_ate,
included = (true_ate >= estimated_lower_bound) & (true_ate <= estimated_upper_bound),
ci_size = (estimated_upper_bound - estimated_lower_bound) / reference_value,
dataset = case_when(
dataset == 'lifetime_value' ~ 'Lifetime Value Dataset',
dataset == 'supermarket_sales' ~ 'Supermarket Sales Dataset',
dataset == 'nifty50_stock_market' ~ 'NIFTY50 Stock Market Value Dataset',
TRUE ~ 'Wrong Name'
),
model = factor(
model,
c("DifferenceInDifferencesEstimator", "SyntheticControlEstimator", "SLearner", "TLearner", "DoublyRobustEstimator", "DoWhyEstimator"),
c("Difference-In-Differences\n(CausalPy)", "Synthetic Control\n(CausalPy)", "S-Learner\n(-)", "T-Learner\n(CausalML)", "Doubly Robust Estimator\n(-)", "Graph Causal Model\n(DoWhy)")
)
)
avg_model_data = simulations_data %>%
mutate(model = 'Averaged Model',
`...1` = NA) %>%
group_by(`...1`, it, dataset, treated_groups, lift_size, reference_value, model, true_ate, ci, simulation_date) %>%
summarise(
estimated_ate = mean(estimated_ate),
estimated_std = mean(estimated_std),
estimated_lower_bound = mean(estimated_lower_bound),
estimated_upper_bound = mean(estimated_upper_bound)
) %>%
ungroup %>%
mutate(
bias = (estimated_ate - true_ate) / true_ate,
included = (true_ate >= estimated_lower_bound) & (true_ate <= estimated_upper_bound),
ci_size = (estimated_upper_bound - estimated_lower_bound) / reference_value
)
binded_data = rbind(simulations_data, avg_model_data) %>%
mutate(
model = factor(
model,
c("DifferenceInDifferencesEstimator", "SyntheticControlEstimator", "SLearner", "TLearner", "DoublyRobustEstimator", "DoWhyEstimator", 'Averaged Model'),
c("Difference-In-Differences\n(CausalPy)", "Synthetic Control\n(CausalPy)", "S-Learner\n(-)", "T-Learner\n(CausalML)", "Doubly Robust Estimator\n(-)", "Graph Causal Model\n(DoWhy)", 'Averaged Model')
)
)
#####
# Bias
ggplot(data = simulations_data,
aes(x = model, y = bias, color = dataset, fill = dataset)) +
standard_theme() +
theme(legend.position = 'bottom') +
floor_line() +
geom_boxplot(alpha = 0.7, width = 0.7, position = position_dodge(width=0.7), size=0.4) +
scale_y_continuous(
limits = c(-1, 1),
minor_breaks = NULL,
labels = scales::percent) +
labs(
x = 'Algorithm',
y = 'Bias',
color = 'Dataset',
fill = 'Dataset',
)
save_plot(path='/Users/raphaeltamaki/Documents/personal_git/causal_methodologies_analysis/results/bias')
#####
# Coverage
coverage_data = simulations_data %>%
group_by(model, dataset) %>%
summarise(
isin = sum(included),
isout = n() - sum(included)
) %>%
ungroup
coverage_data[c('avg', 'low', 'median', 'high')] = coverage_data %>%
select(isin, isout) %>%
apply(1, beta_comparison) %>%
t()
ggplot(data = coverage_data,
aes(x = model, y = avg, ymin = low, ymax = high, color = dataset, fill = dataset)) +
standard_theme() +
theme(legend.position = 'bottom') +
floor_line() +
geom_col(alpha = 0.7, width = 0.7, position = position_dodge(width=0.7), size=0.4) +
geom_errorbar(alpha = 0.7, width = 0.35, position = position_dodge(width=0.7), size=0.4) +
scale_y_continuous(
limits = c(-0, 1),
minor_breaks = NULL,
labels = scales::percent) +
labs(
x = 'Algorithm',
y = 'Rate CI 90% includes true ATT',
color = 'Dataset',
fill = 'Dataset',
)
save_plot(path='/Users/raphaeltamaki/Documents/personal_git/causal_methodologies_analysis/results/coverage')
#####
# CI 90% Size
ggplot(data = simulations_data,
aes(x = model, y = ci_size, color = dataset, fill = dataset)) +
standard_theme() +
theme(legend.position = 'bottom') +
floor_line() +
geom_boxplot(alpha = 0.7, width = 0.7, position = position_dodge(width=0.7), size=0.4) +
scale_y_continuous(
minor_breaks = NULL) +
labs(
x = 'Algorithm',
y = 'Indexed Size of CI 90%',
color = 'Dataset',
fill = 'Dataset',
)
save_plot(path='/Users/raphaeltamaki/Documents/personal_git/causal_methodologies_analysis/results/ci_size')
scatter_data = simulations_data %>%
group_by(model, dataset) %>%
summarise(
isin_rate = sum(included) / n(),
ci_size = mean(ci_size)
) %>%
ungroup
ggplot(data = scatter_data,
aes(x = ci_size, y = isin_rate, color = model, fill = model)) +
standard_theme() +
theme(legend.position = "none") +
floor_line() +
geom_point(size=3, alpha=0.6) +
geom_text_repel(aes(label = model), size=3) +
facet_wrap(~dataset) +
scale_x_continuous(minor_breaks = NULL, limits=c(-0.5, 2)) +
scale_y_continuous(
minor_breaks = NULL,
labels = scales::percent) +
scale_color_brewer(palette = "Dark2") +
scale_fill_brewer(palette = "Dark2") +
labs(
x = 'Indexed Size of CI 90%',
y = 'Rate CI 90% includes true ATT',
color = 'Model',
fill = 'Model',
)
save_plot(path='/Users/raphaeltamaki/Documents/personal_git/causal_methodologies_analysis/results/compromisse_ci_coverage')
synthetic_control_data = tibble(
x = c(seq(200), seq(200), seq(200)),
y = c(rnorm(200, 5, 1), rnorm(200, 7, .5), rnorm(100, 10, 1.5), rnorm(100, 15, 1.5)),
group = c(rep('A', 200), rep('B', 200), rep('C', 200))
)
ggplot(data = synthetic_control_data,
aes(x = x, y = y, color = group)) +
standard_theme() +
floor_line() +
geom_line() +
scale_x_continuous(minor_breaks = NULL) +
scale_y_continuous(
minor_breaks = NULL) +
scale_color_brewer(palette = "Set1") +
scale_fill_brewer(palette = "Set1") +
labs(
x = 'Time',
y = 'Target Variable',
color = 'Location',
)
save_plot(path='/Users/raphaeltamaki/Documents/personal_git/causal_methodologies_analysis/results/synthetic_control_example')