-
Notifications
You must be signed in to change notification settings - Fork 3
/
test-2016.R
144 lines (114 loc) · 4.12 KB
/
test-2016.R
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
# Dependencies: import-2016.R
source("lib/function_library.R")
# Assumes 2016 data is extracted to inbound/data-2016/
# Download from https://drive.google.com/file/d/0B8TUkApaUlsGekFSblJWa25NM1E/edit
conf = list(
inbound_dir = "inbound/data-2016",
# Subdirectory to save temporary csv files.
data_dir = "data",
# Subdirectory to save output logs.
output_dir = "output",
# Can be "simple" or "complex".
#sl_lib_type = "simple",
sl_lib_type = "complex",
# Set to T for extra output during execution.
verbose = T,
parallel = F,
# Set auto-install to T for code to install any missing packages.
auto_install = T,
# Use up to this many cores if available.
max_cores = 4
)
# Set auto-install to T for code to install any missing packages.
load_all_packages(auto_install = conf$auto_install, verbose = conf$verbose)
# Load all .R files in the lib directory.
ck37r::load_all_code("lib", verbose = conf$verbose)
input_file = paste0(conf$data_dir, "/import-2016.RData")
if (!file.exists(input_file)) {
stop(paste("Can't find", input_file, ". Make sure to run import-2016.R first."))
}
load(input_file)
# First test on a single file.
names(files[[1]])
data = files[[1]]$data
# Setup parallelization? Use up to 4 cores.
num_cores = RhpcBLASctl::get_num_cores()
if (conf$verbose) {
cat("Cores detected:", num_cores, "\n")
}
use_cores = min(num_cores, conf$max_cores)
if (conf$parallel) {
options("mc.cores" = use_cores)
}
if (conf$verbose) {
# Check how many parallel workers we are using:
cat("Cores used:", getOption("mc.cores"), "\n")
}
if (conf$sl_lib_type == "simple") {
q_lib = c("SL.mean", "SL.glmnet")
g_lib = c("SL.mean", "SL.glmnet")
} else {
q_lib = c(list(# speedglm doesn't work :/ just use plain ol' glm.
c("SL.glm", "All", "screen.corRank4", "screen.corRank8", "prescreen.nosq")#,
#c("SL.mgcv", "All", "prescreen.nosq"),
#c("sg.gbm.2500", "prescreen.nocat"),
#"SL.xgboost",
#"SL.xgboost_threads_4"
# Effect modification learners can't be used with g, only Q.
),
# create.Learner() grids.
sl_glmnet_em15$names,
sl_xgb$names,
sl_ksvm$names,
list(
#"SL.randomForest_fast",
"SL.ranger_fast",
c("SL.glmnet_fast", "All", "screen.corRank4", "screen.corRank8"),
c("SL.nnet", "All", "screen.corRank4", "screen.corRank8"),
c("SL.earth", "prescreen.nosq"),
# Works only if parallel = F. Do not use with mcSuperlearner!
"SL.bartMachine",
"SL.mean"))
# Need a separate g lib that does not include effect modification learners.
g_lib = c(list(c("SL.glm", "All", "screen.corRank4", "screen.corRank8", "prescreen.nosq"),
#c("SL.mgcv", "All", "prescreen.nosq"),
#c("sg.gbm.2500", "prescreen.nocat"),
#"SL.xgboost",
#"SL.xgboost_threads_4",
"SL.ranger_fast"#,
), # create.Learner() grids.
sl_xgb$names,
sl_ksvm$names,
list(
c("SL.glmnet_fast", "All", "screen.corRank4", "screen.corRank8"),
c("SL.nnet", "All", "screen.corRank4", "screen.corRank8"),
c("SL.earth", "prescreen.nosq"),
# Works only if parallel = F. Do not use with mcSuperlearner!
"SL.bartMachine",
"SL.mean"))
}
set.seed(1, "L'Ecuyer-CMRG")
# Restrict to 250 observations to be similar to contest.
obs = sample(nrow(data), 250, replace = F)
A = data$z[obs]
Y = data$y[obs]
W = data_x[obs, ]
# Takes a minute or so to run using a simple library.
results = estimate_att(A = A,
Y = Y,
W = W,
SL.library = q_lib,
g.SL.library = g_lib,
pooled_outcome = T,
parallel = F,
verbose = conf$verbose)
# Parallel (4 cores): X seconds with stratified or pooled outcome regression.
# Non-parallel: X seconds with stratified, X seconds with pooled outcome regression.
results$time
# Extract out unit estimates to make displaying more convenient.
unit_estimates = results$unit_estimates
results$unit_estimates = NULL
# Display everything except the unit estimates.
results
summary(unit_estimates)
# TODO: test command-line version like in analyze-2016.R