-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04a_riskModel.R
284 lines (238 loc) · 9.42 KB
/
04a_riskModel.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
library(readr)
library(tidymodels)
library(bonsai)
library(probably)
library(lubridate)
source("00_functions.R")
imputeDF <- read_csv("data/processed/imputeDF.csv")
mlDF <- imputeDF %>% filter(tested) %>%
select(-tested,-`1st Draw`,-`2-3 Minute`,-`5 Minute`,
-HHsInternetPropBG,-pCompletePlumbingFacilitiesBG,
-HHsHasComputerPropBG,-pOccupiedHousesBG,-sequential,
-pRenterOccupiedHousesBG,-`Date Sampled`) %>% #remove redundant features
mutate(overOne_2 = factor(overOne_2),
censusTract = factor(censusTract),
CA = factor(CA),
blockNum = factor(blockNum),
blockGroup = factor(blockGroup),
)
mlDF_grouped <- mlDF %>% group_by(blockNum) %>%
mutate(outcome = mean(as.numeric(overOne_2)-1),
outcome = factor(ifelse(outcome>=0.5,TRUE,FALSE))) %>%
ungroup() %>% distinct(blockNum,.keep_all=T)
mlDF_grouped$overOne_2 <- mlDF_grouped$outcome
mlDF_grouped$outcome <- NULL
# split_df2 <- group_initial_split(mlDF,group=blockNum) #keep blocks in same split
split_df2 <- initial_split(mlDF_grouped)
trainDF2 <- training(split_df2)
testDF2 <- testing(split_df2)
tree_rec2 <- recipe(overOne_2 ~ ., data = trainDF2) %>%
update_role(blockNum, new_role="ID")
#lightgbm specification
tune_spec2 <- boost_tree(
min_n = tune(),
trees = 1000,
tree_depth = tune()) %>%
set_mode("classification") %>%
set_engine("lightgbm") #xgboost or lightgbm
tune_wf2 <- workflow() %>%
#add_formula(overOne_2 ~ .-blockNum) %>%
add_recipe(tree_rec2) %>%
add_model(tune_spec2)
#random forest specification
tune_spec2_rf <- rand_forest(
trees=1000,mtry=tune()
) %>%
set_mode("classification") %>%
set_engine("ranger")
tune_wf2_rf <- workflow() %>%
add_formula(overOne_2 ~ .-blockNum) %>%
add_model(tune_spec2_rf)
#elasticnet specification
tune_spec2_enet <- logistic_reg(penalty = tune(),
mixture = tune()) %>%
set_mode("classification") %>%
set_engine("glmnet")
tune_wf2_enet <- workflow() %>%
add_formula(overOne_2 ~ .-blockNum) %>% #xgb needs formula instead of recipe
add_model(tune_spec2_enet)
##model comparison
trees_folds3 <- group_vfold_cv(trainDF2,group=blockNum,v=3)
trees_folds_final <- group_vfold_cv(trainDF2,group=blockNum,v=5)
doParallel::registerDoParallel()
set.seed(127)
#lightgbm train
tune_res2 <- tune_grid(
tune_wf2,
resamples = trees_folds3,
grid = 5
)
tune_res2 %>%
tune::show_best(metric = "roc_auc",n = 5)
#rf train
tune_res2_rf <- tune_grid(
tune_wf2_rf,
resamples = trees_folds3,
grid = 5
)
tune_res2_rf %>%
tune::show_best(metric = "roc_auc",n = 5)
#glmnet train
tune_res2_enet <- tune_grid(
tune_wf2_enet,
resamples = trees_folds3,
grid = 5
)
tune_res2_enet %>%
tune::show_best(metric = "roc_auc",n = 5)
##retune using 5-fold + more exhaustive grid search
tune_res_final2 <- tune_grid(
tune_wf2,
resamples = trees_folds3,
grid = 15
)
tune_res_final2 %>%
tune::show_best(metric = "roc_auc",n = 5)
best_tree2 <- tune_res2 %>%
select_best("roc_auc")
final_wf2 <-
tune_wf2 %>%
finalize_workflow(best_tree2)
saveRDS(final_wf2,"data/processed/outcomeWF.rds")
final_fit2 <-
final_wf2 %>%
last_fit(split_df2)
final_fit2 %>%
collect_metrics()
myModel2 <- extract_fit_engine(final_fit2)
impObj2 <- lgb.importance(myModel2,percentage = F)
#make calibrated risk predictions
#predSplits <- group_initial_split(mlDF,group=blockNum,prop=1/2)
predSplits <- initial_split(mlDF_grouped,prop=1/2)
split1 <- training(predSplits)
split2 <- testing(predSplits)
splitDF1 <- fitSplit(dataSplit = split1, testData=split2,gridNum=10)
splitDF2 <- fitSplit(dataSplit = split2, testData=split1,gridNum=10)
riskDF <- rbind(splitDF1,splitDF2)
write_csv(riskDF,"data/processed/riskDF_pt1.csv")
#to-do: make grouped prediction code below
#make outcome predictions on blocks without tests
imputeDF <- read_csv("data/processed/imputeDF.csv")
imputeDF <- imputeDF %>%
select(-`1st Draw`,-`2-3 Minute`,-`5 Minute`,
-HHsInternetPropBG,-pCompletePlumbingFacilitiesBG,
-HHsHasComputerPropBG,-pOccupiedHousesBG,-sequential,
-pRenterOccupiedHousesBG) %>% #remove redundant features
mutate(overOne_2 = factor(overOne_2),
censusTract = factor(censusTract),
CA = factor(CA),
blockNum = factor(blockNum),
blockGroup = factor(blockGroup))
imputeDF_grouped <- imputeDF %>% group_by(blockNum) %>%
mutate(outcome = mean(as.numeric(overOne_2)-1),
outcome = factor(ifelse(outcome>=0.5,TRUE,FALSE))) %>%
ungroup() %>% distinct(blockNum,.keep_all=T)
imputeDF_grouped$overOne_2 <- imputeDF_grouped$outcome
imputeDF_grouped$outcome <- NULL
propensity_train <- imputeDF_grouped %>% filter(tested==T) %>%
select(-tested)
propensity_test <- imputeDF_grouped %>% filter(tested==F) %>%
select(-tested)
withoutTestsPreds <- fitSplit(dataSplit = propensity_train,
testData=propensity_test,gridNum=10)
withoutTests <- withoutTestsPreds %>%
rename(preds = rawPreds) %>%
select(blockNum,preds,calibPreds,overOne_2)
withTests <- riskDF %>%
rename(preds = rawPreds) %>%
select(blockNum,preds,calibPreds,overOne_2)
riskDF2 <- rbind(withTests,withoutTests)
write_csv(riskDF2,"data/processed/riskDF.csv")
##construct shap
tree_rec2B <- recipe(overOne_2 ~ ., data = trainDF2) %>%
update_role(blockNum, new_role="ID") %>%
step_integer(all_nominal())
#step_dummy(all_nominal_predictors()) #previously step_integer
baked2 <- bake(
prep(tree_rec2B),
has_role("predictor"),
new_data = trainDF2,
composition = "matrix"
)
library(shapviz)
outcomeShap <- shapviz(myModel2, X_pred = baked2, x=trainDF2)
saveRDS(outcomeShap,file="data/processed/outcomeShap.rds")
write_csv(trainDF2,"data/processed/trainDF2.csv")
#baseline ML predictions
baselineTrainDF2 <- trainDF2 %>% group_by(blockGroup) %>%
mutate(bgMean = mean(as.numeric(overOne_2)),
bgMean = ifelse(bgMean >=0.5,TRUE,FALSE)) %>%
ungroup() %>%
group_by(censusTract) %>%
mutate(tractMean = mean(as.numeric(overOne_2)),
tractMean = ifelse(tractMean >=0.5,TRUE,FALSE)) %>%
ungroup() %>%
group_by(CA) %>%
mutate(caMean = mean(as.numeric(overOne_2)),
caMean = ifelse(caMean >=0.5,TRUE,FALSE))
baselineTrainDF3 <- baselineTrainDF2 %>% select(blockGroup,censusTract,CA,
bgMean,tractMean,caMean)
#grouped ML test
#make calibrated risk predictions
mlDF_grouped$overOne_2 <- mlDF_grouped$outcome
predSplits <- initial_split(mlDF_grouped,prop=1/2)
split1 <- training(predSplits)
split2 <- testing(predSplits)
splitDF1 <- fitSplit(dataSplit = split1, testData=split2,gridNum=5)
splitDF2 <- fitSplit(dataSplit = split2, testData=split1,gridNum=5)
riskDF_grouped <- rbind(splitDF1,splitDF2)
write_csv(riskDF_grouped,"data/processed/riskDF_pt1_grouped.csv")
#make outcome predictions on blocks without tests
imputeDF <- read_csv("data/processed/imputeDF.csv")
imputeDF_grouped <- imputeDF %>%
select(-`1st Draw`,-`2-3 Minute`,-`5 Minute`,
-HHsInternetPropBG,-pCompletePlumbingFacilitiesBG,
-HHsHasComputerPropBG,-pOccupiedHousesBG,-sequential,
-pRenterOccupiedHousesBG,-`Date Sampled`) %>% #remove redundant features
mutate(overOne_2 = factor(overOne_2),
censusTract = factor(censusTract),
CA = factor(CA),
blockNum = factor(blockNum),
blockGroup = factor(blockGroup)) %>% group_by(blockNum) %>%
mutate(outcome = mean(as.numeric(overOne_2)-1),
outcome = factor(ifelse(outcome>=0.5,TRUE,FALSE))) %>%
ungroup() %>% distinct(blockNum,.keep_all=T)
imputeDF_grouped$overOne_2 <- imputeDF_grouped$outcome
imputeDF_grouped$outcome <- NULL
propensity_train <- imputeDF_grouped %>% filter(tested==T) %>%
select(-tested)
propensity_test <- imputeDF_grouped %>% filter(tested==F) %>%
select(-tested)
withoutTestsPreds <- fitSplit(dataSplit = propensity_train,
testData=propensity_test,gridNum=5)
withoutTests_grouped <- withoutTestsPreds %>%
rename(preds = rawPreds) %>%
select(blockNum,preds,calibPreds,overOne_2)
withTests_grouped <- riskDF_grouped %>%
rename(preds = rawPreds) %>%
select(blockNum,preds,calibPreds,overOne_2)
riskDF2_grouped <- rbind(withTests_grouped,withoutTests_grouped)
write_csv(riskDF2_grouped,"data/processed/riskDF_grouped.csv")
riskDF2_grouped$tested <- !is.na(riskDF2_grouped$overOne_2)
riskDF2_grouped$predClass <- ifelse((1-riskDF2_grouped$calibPreds)>0.5,TRUE,FALSE)
cmDF_grouped <- riskDF2_grouped %>% filter(!is.na(overOne_2))
cmDF_grouped$predClass <- factor(cmDF_grouped$predClass)
cmDF_grouped$overOne_2 <- factor(cmDF_grouped$overOne_2,levels=c("FALSE","TRUE"))
cm_grouped <- conf_mat(cmDF_grouped,truth=overOne_2,estimate=predClass)
false_discovery_rate <- cm_grouped$table[2,1]/(cm_grouped$table[2,2]+cm_grouped$table[2,1]) #same as 1-PPV
false_omission_rate <- cm_grouped$table[1,2]/(cm_grouped$table[1,1]+cm_grouped$table[1,2]) #same as 1-NPV
falseNegatives <- riskDF2_grouped %>%
filter(predClass=="FALSE") %>% nrow() * false_omission_rate
falsePositives <- riskDF2_grouped %>%
filter(predClass=="TRUE") %>% nrow() * false_discovery_rate
(riskDF2_grouped %>% filter(predClass=="TRUE") %>% nrow() + falseNegatives-
falsePositives)/nrow(riskDF2_grouped)
riskDF2_grouped %>% filter(tested) %>% filter(predClass=="TRUE") %>% nrow() /
riskDF2_grouped %>% filter(tested) %>% nrow()
riskDF2_grouped %>% filter(!tested) %>% filter(predClass=="TRUE") %>% nrow() /
riskDF2_grouped %>% filter(!tested) %>% nrow()