-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabm_utils.R
280 lines (178 loc) · 6.87 KB
/
abm_utils.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
# functions for amb algorithms
preparte_layout <- function(tag_ls,sampleWeight_ls,predictor_ls,setID_ls){
layout <- list()
layout$tag_ls <- tag_ls
layout$sampleWeight_ls <- sampleWeight_ls
layout$predictor_ls <- predictor_ls
layout$setID_ls <- setID_ls
return(layout)
}
prepare_abm_data <- function(data,taget_ls,sampleWeight_ls,predictor_ls,setID_ls){
tag_ls <- copy(taget_ls)
sampleWeight <- copy(sampleWeight_ls)
num_var_ls <- copy(predictor_ls)
setID <- copy(setID_ls)
train <- which(data[[setID]]==1)
tag_ddt <- cbind(data[,..tag_ls],data[,..sampleWeight])
num_ddt <- data[,..num_var_ls]
num_ddt_train <- cbind(tag_ddt[train,],num_ddt[train,])
num_ddt_validaton <- cbind(tag_ddt[-train,],num_ddt[-train,])
abm_data <- list()
abm_data$train_data <- num_ddt_train
abm_data$validation_data <- num_ddt_validaton
return(abm_data)
}
prepare_coarse_bin_abm_data <- function(abm_data,layout,nbins){
train_data <- abm_data$train_data
validation_data <- abm_data$validation_data
ls1 <- layout$predictor_ls
ls2 <- layout$tag_ls
ls3 <- layout$sampleWeight_ls
train_X_dt <- (abm_data$train_data)[,..ls1]
train_Y_dt <- (abm_data$train_data)[,..ls2]
train_W_dt <- (abm_data$train_data)[,..ls3]
validation_X_dt <- (abm_data$validation_data)[,..ls1]
validation_Y_dt <- (abm_data$validation_data)[,..ls2]
validation_W_dt <- (abm_data$validation_data)[,..ls3]
bin_split_pts <- get_split_pts(origin_bin_library(train_X_dt,nbins))
# apply bining for train and test dataset
train_out <- do_cut_with_split_pts(train_X_dt,bin_split_pts)
validation_out <- do_cut_with_split_pts(validation_X_dt,bin_split_pts)
abm_data_bined <- list()
abm_data_bined$train_X_dt_bined <- train_out$bine_dt
abm_data_bined$train_Y_dt <- train_Y_dt
abm_data_bined$train_W_dt <- train_W_dt
abm_data_bined$validation_X_dt_bined <- validation_out$bine_dt
abm_data_bined$validation_Y_dt <- validation_Y_dt
abm_data_bined$validation_W_dt <- validation_W_dt
abm_data_bined$group <- do_cut_with_split_pts(train_X_dt,bin_split_pts)$bined_group
abm_data_bined$vnames <- colnames(abm_data_bined$train_X_dt_bined)
return(abm_data_bined)
}
prepare_params <- function(lambda1,lambda2){
params <- list()
params$lambda1 <- lambda1
params$lambda2 <- lambda2
return(params)
}
train_fg_lasso_model_v1 <- function(abm_data_bined,params){
lambda1 <- params$lambda1
lambda2 <- params$lambda2
X <- as.matrix(abm_data_bined$train_X_dt_bined)
Y <- as.matrix(abm_data_bined$train_Y_dt)
W <- as.matrix(abm_data_bined$train_W_dt)
group <- abm_data_bined$group
vnames <- abm_data_bined$vnames
coef <- round(c(get_wt_group_lasso_and_group_fused_lasso_logistic_beta_with_cvxr(X,Y,W,group,lambda1,lambda2)),3)
names(coef) <- vnames
# model performance part
train_X <- as.matrix(abm_data_bined$train_X_dt_bined)
train_Y <- c(as.matrix(abm_data_bined$train_Y_dt))
train_W <- c(as.matrix(abm_data_bined$train_W_dt))
idx1 <- c()
for(i in c(1:length(train_W))){
wgt <- round(train_W[i])
idx1 <- c(idx1,rep(i,wgt))
}
train_ot <- test_logistic_model(coef,train_X[idx1,],train_Y[idx1])
test_X <- as.matrix(abm_data_bined$validation_X_dt_bined)
test_Y <- c(as.matrix(abm_data_bined$validation_Y_dt))
test_W <- c(as.matrix(abm_data_bined$validation_W_dt))
idx2 <- c()
for(i in c(1:length(test_W))){
wgt <- round(test_W[i])
idx2 <- c(idx2,rep(i,wgt))
}
test_ot <- test_logistic_model(coef,test_X[idx2,],test_Y[idx2])
cat('train\t',train_ot$prt_line)
cat('test\t',test_ot$prt_line)
train_report <- list()
train_report$acc <- train_ot$acc
train_report$auc <- train_ot$auc
train_report$ks <- train_ot$ks
train_report$loss <- train_ot$logit_loss
test_report <- list()
test_report$acc <- test_ot$acc
test_report$auc <- test_ot$auc
test_report$ks <- test_ot$ks
test_report$loss <- test_ot$logit_loss
model <- list()
model$coef <- coef
model$vnames <- vnames
model$group <- group
model$train_report <- train_report
model$test_report <- test_report
return(model)
}
train_fg_lasso_model_v2 <- function(abm_data_bined,params){
lambda1 <- params$lambda1
lambda2 <- params$lambda2
X <- as.matrix(abm_data_bined$train_X_dt_bined)
Y <- as.matrix(abm_data_bined$train_Y_dt)
W <- as.matrix(abm_data_bined$train_W_dt)
group <- abm_data_bined$group
vnames <- abm_data_bined$vnames
coef <- round(c(get_wt_group_lasso_and_group_fused_lasso_linear_regression_beta_with_cvxr(X,Y,W,group,lambda1,lambda2)),3)
names(coef) <- vnames
# model performance part
train_X <- as.matrix(abm_data_bined$train_X_dt_bined)
train_Y <- c(as.matrix(abm_data_bined$train_Y_dt))
train_W <- c(as.matrix(abm_data_bined$train_W_dt))
idx1 <- c()
for(i in c(1:length(train_W))){
wgt <- round(train_W[i])
idx1 <- c(idx1,rep(i,wgt))
}
train_ot <- test_logistic_model(coef,train_X[idx1,],train_Y[idx1])
test_X <- as.matrix(abm_data_bined$validation_X_dt_bined)
test_Y <- c(as.matrix(abm_data_bined$validation_Y_dt))
test_W <- c(as.matrix(abm_data_bined$validation_W_dt))
idx2 <- c()
for(i in c(1:length(test_W))){
wgt <- round(test_W[i])
idx2 <- c(idx2,rep(i,wgt))
}
test_ot <- test_logistic_model(coef,test_X[idx2,],test_Y[idx2])
cat('train\t',train_ot$prt_line)
cat('test\t',test_ot$prt_line)
train_report <- list()
train_report$acc <- train_ot$acc
train_report$auc <- train_ot$auc
train_report$ks <- train_ot$ks
train_report$loss <- train_ot$logit_loss
test_report <- list()
test_report$acc <- test_ot$acc
test_report$auc <- test_ot$auc
test_report$ks <- test_ot$ks
test_report$loss <- test_ot$logit_loss
model <- list()
model$coef <- coef
model$vnames <- vnames
model$group <- group
model$train_report <- train_report
model$test_report <- test_report
return(model)
}
show_bin <- function(model){
coef <- model$coef
grp <- levels(factor(model$group))
drop_list <- get_drop_list(model$coef,model$group)
idx <- which(model$group %in% drop_list == FALSE)
ccoef <- coef[idx]
print(ccoef)
return(data.table(bin_nm<-names(ccoef),coef<-ccoef))
}
show_bin_plot <- function(model){
coef <- model$coef
plot(c(1:length(coef)),coef)
}
show_drop_list <- function(model){
drop_list <- get_drop_list(model$coef,model$group)
print(drop_list)
}
show_keep_list <- function(model){
drop_list <- get_drop_list(model$coef,model$group)
grp <- levels(factor(model$group))
keep_list <- grp[grp %in% drop_list == FALSE]
print(keep_list)
}