-
Notifications
You must be signed in to change notification settings - Fork 4
/
vignette.Rmd
249 lines (159 loc) · 7.63 KB
/
vignette.Rmd
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
---
title: "vignette"
output:
pdf_document: default
html_document: default
date: '2022-12-20'
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Causal isotonic calibration
```{r}
library(causalCalibration)
# Generate dataset used for calibration
set.seed(123)
n <- 1000
W <- runif(n, -1 , 1)
pA1 <- plogis(2*W)
A <- rbinom(n, size = 1, pA1)
# True CATE(W) = 1 + W
CATE <- 1 + W
EY0 <- W
EY1 <- W + CATE
Y <- rnorm(n, W + A * CATE, 0.3)
# Initial uncalibrated predictor is a monotone transformation of the true CATE
# Thus it is highly correlated with true CATE but predictions are not uncalibrated for CATE values
tau <- exp(CATE) - 1
plot(W, tau)
n <- 10000
Wnew <- runif(n, -1 , 1)
Wnew <- Wnew[abs(Wnew) <= 0.99]
n <- length(Wnew)
pA1new <- plogis(2*Wnew)
Anew <- rbinom(n, size = 1, pA1new)
# True CATE(W) = 1 + W
CATE <- 1 + Wnew
tau_new <- exp(CATE) - 1
calibrator <- causalCalibrate(tau, A, Y, EY1, EY0, pA1, tau_pred = tau_new)
library(ggplot2)
library(latex2exp)
tau_cal <- calibrator$tau_calibrated
data <- data.frame(W = Wnew, CATE = c(CATE, tau_new, tau_cal), type = rep(c("CATE", "Original", "Calibrated"), each = length(Wnew) ))
ggplot(data, aes(x = W, y = CATE, color = type)) + geom_line() + labs(x = TeX("$\\tau(w)$"), y = TeX("$\\tau_0(w) = E[Y_1 - Y_0 | W = w]$") )+ ggtitle("Effect of calibration") + scale_x_continuous( ) + theme_bw()
tau <- 1 + W +sin(5*W)
tau_new <- 1 + Wnew+ sin(5*Wnew)
calibrator <- causalCalibrate(tau, A, Y, EY1, EY0, pA1, tau_pred = tau_new)
library(ggplot2)
library(latex2exp)
tau_cal <- calibrator$tau_calibrated
data <- data.frame(W = Wnew, CATE = c(CATE, tau_new, tau_cal), type = rep(c("CATE", "Original", "Calibrated"), each = length(Wnew) ))
ggplot(data, aes(x = W, y = CATE, color = type)) + geom_line() + ggtitle("Effect of calibration") + scale_x_continuous( ) + theme_bw()
tau <- 1 + W
tau_new <- 1 + Wnew
calibrator <- causalCalibrate(tau, A, Y, EY1, EY0, pA1, tau_pred = tau_new)
library(ggplot2)
library(latex2exp)
tau_cal <- calibrator$tau_calibrated
data <- data.frame(W = Wnew, CATE = c(CATE, tau_new, tau_cal), type = rep(c("CATE", "Original", "Calibrated"), each = length(Wnew) ))
ggplot(data, aes(x = W, y = CATE, color = type)) + geom_line() + ggtitle("Effect of calibration") + scale_x_continuous( ) + theme_bw()
tau <- 1 + W + (W >= 0) * ( -0.8* W)
tau_new <- 1 + Wnew + (Wnew >= 0) * ( -0.8* Wnew)
calibrator <- causalCalibrate(tau, A, Y, EY1, EY0, pA1, tau_pred = tau_new)
library(ggplot2)
library(latex2exp)
tau_cal <- calibrator$tau_calibrated
data <- data.frame(W = Wnew, CATE = c(CATE, tau_new, tau_cal), type = rep(c("CATE", "Original", "Calibrated"), each = length(Wnew) ))
ggplot(data, aes(x = W, y = CATE, color = type)) + geom_line() + ggtitle("Effect of calibration") + scale_x_continuous( ) + theme_bw()
tau <- 1 + W + (W >= 0) * ( -2* W)
tau_new <- 1 + Wnew + (Wnew >= 0) * ( -2* Wnew)
calibrator <- causalCalibrate(tau, A, Y, EY1, EY0, pA1, tau_pred = tau_new)
library(ggplot2)
library(latex2exp)
tau_cal <- calibrator$tau_calibrated
data <- data.frame(W = Wnew, CATE = c(CATE, tau_new, tau_cal), type = rep(c("CATE", "Original", "Calibrated"), each = length(Wnew) ))
ggplot(data, aes(x = W, y = CATE, color = type)) + geom_line() + ggtitle("Effect of calibration") + scale_x_continuous( ) + theme_bw()
tau <- 1 + W + (W >= 0) * ( -1* W)
tau_new <- 1 + Wnew + (Wnew >= 0) * ( -1* Wnew)
calibrator <- causalCalibrate(tau, A, Y, EY1, EY0, pA1, tau_pred = tau_new)
library(ggplot2)
library(latex2exp)
tau_cal <- calibrator$tau_calibrated
data <- data.frame(W = Wnew, CATE = c(CATE, tau_new, tau_cal), type = rep(c("CATE", "Original", "Calibrated"), each = length(Wnew) ))
ggplot(data, aes(x = W, y = CATE, color = type)) + geom_line() + ggtitle("Effect of calibration") + scale_x_continuous( ) + theme_bw()
```
# cross-calibration
## cross-calibration with nnet
```{r}
library(sl3) #tlverse/sl3 on github. Used for cross fitting
library(data.table)
n <- 1000
W <- runif(n, -1 , 1)
pA1 <- plogis(1.5*W)
A <- rbinom(n, size = 1, pA1)
# True CATE(W) = 1 + W
CATE <- abs(W)
EY0 <- W
EY1 <- W + CATE
EY <- ifelse(A==1, EY1, EY0)
Y <- rnorm(n, W + A * CATE, 0.3)
zeta <- EY1 - EY0 + (A - pA1) /( (1-pA1)*pA1) *(Y - EY)
data <- data.table(W, zeta = zeta)
task <- sl3_Task$new(data, covariates = c("W"), outcome = "zeta", folds = 10)
# choose between base_learners
base_learner <- Lrnr_nnet$new(size = 25) # single layer neural net
lrnr_tau <- Lrnr_cv$new(base_learner, full_fit = TRUE)
#train predictor
lrnr_tau <- lrnr_tau$train(task) # does both cross fitting and full sample fitting
tau_no_crossfit <- lrnr_tau$predict_fold(task, "full") # predictions using full predictor fit on all data
tau_out_of_fold <- lrnr_tau$predict_fold(task, "validation") # pooled out of fold predictions from cross fitted predictors
tau_all <- do.call(cbind, lapply(1:10, function(k) {
lrnr_tau$predict_fold(task, k) # fold-specific predictions
})) # Get full sample predictions from each fold-specific predictor.
# Compute calibrator from data
calibrator <- causalCalibrate(tau = tau_out_of_fold, A = A, Y = Y, EY1 = EY1, EY0 = EY0, pA1 = pA1)
# tau_all should be the stacked cross-fitted prediction matrix for observations at which we would like to get the calibrated predictions.
tau_cal <- cross_calibrate(calibrator, tau_all)
data <- data.frame(W = W, CATE = c(CATE, tau_no_crossfit, tau_cal), type = rep(c("True CATE", "Uncalibrated", "Calibrated"), each = length(W) ))
ggplot(data, aes(x = W, y = CATE, color = type)) + geom_line() + ggtitle("Effect of calibration") + scale_x_continuous( ) + theme_bw()
# We improve mean-squared error by calibrating.
mean((tau_no_crossfit - CATE)^2)
mean((tau_cal - CATE)^2)
```
## cross-calibration with xgboost
```{r}
library(sl3) #tlverse/sl3 on github. Used for cross fitting
library(data.table)
n <- 1000
W <- runif(n, -1 , 1)
pA1 <- plogis(1.5*W)
A <- rbinom(n, size = 1, pA1)
# True CATE(W) = 1 + W
CATE <- abs(W)
EY0 <- W
EY1 <- W + CATE
EY <- ifelse(A==1, EY1, EY0)
Y <- rnorm(n, W + A * CATE, 0.3)
zeta <- EY1 - EY0 + (A - pA1) /( (1-pA1)*pA1) *(Y - EY)
data <- data.table(W, zeta = zeta)
task <- sl3_Task$new(data, covariates = c("W"), outcome = "zeta", folds = 10)
# choose between base_learners
base_learner <- Lrnr_xgboost$new(max_depth = 3, nrounds = 10) # xgboost
lrnr_tau <- Lrnr_cv$new(base_learner, full_fit = TRUE)
#train predictor
lrnr_tau <- lrnr_tau$train(task) # does both cross fitting and full sample fitting
tau_no_crossfit <- lrnr_tau$predict_fold(task, "full") # predictions using full predictor fit on all data
tau_out_of_fold <- lrnr_tau$predict_fold(task, "validation") # pooled out of fold predictions from cross fitted predictors
tau_all <- do.call(cbind, lapply(1:10, function(k) {
lrnr_tau$predict_fold(task, k) # fold-specific predictions
})) # Get full sample predictions from each fold-specific predictor.
# Compute calibrator from data
calibrator <- causalCalibrate(tau = tau_out_of_fold, A = A, Y = Y, EY1 = EY1, EY0 = EY0, pA1 = pA1)
# tau_all should be the stacked cross-fitted prediction matrix for observations at which we would like to get the calibrated predictions.
tau_cal <- cross_calibrate(calibrator, tau_all)
data <- data.frame(W = W, CATE = c(CATE, tau_no_crossfit, tau_cal), type = rep(c("True CATE", "Uncalibrated", "Calibrated"), each = length(W) ))
ggplot(data, aes(x = W, y = CATE, color = type)) + geom_line() + ggtitle("Effect of calibration") + scale_x_continuous( ) + theme_bw()
# We improve mean-squared error by calibrating.
mean((tau_no_crossfit - CATE)^2)
mean((tau_cal - CATE)^2)
```