-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3. Elastic Net.R
43 lines (29 loc) · 1.31 KB
/
3. Elastic Net.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
# Elastic net
library(glmnet)
#fit model on training set
e_net_default = glmnet( x = mm[folds < 9,],
y = response[folds < 9],
family="binomial")
#make predictions on test set
p_e_net_default = predict(e_net_default,
newx = mm[folds >= 9,],
type="response")
str(p_e_net_default )
# num [1:98, 1:100] 0.467 0.467 0.467 0.467 0.467 ...
# - attr(*, "dimnames")=List of 2
# ..$ : chr [1:98] "11" "12" "15" "17" ...
# ..$ : chr [1:100] "s0" "s1" "s2" "s3" ...
#get auc for each prediction
e_net_default_auc = aaply(p_e_net_default, 2, function(.x){
auc(predicted_prob = .x, actual_class = response[ folds >= 9])
})
#plot these results
par(mfrow = c(1,2))
plot(e_net_default, xvar = "lambda")
plot(y = e_net_default_auc , x = log(e_net_default$ lambda ), xlab = 'Log Lambda', ylab = 'AUC', type = 'l', lwd = 2)
#put results in a single dataframe
results$ e_net_default_prob = p_e_net_default[,which.max(e_net_default_auc)]
results$e_net_default_class = p_e_net_default[,which.max(e_net_default_auc)] > 0.5
auc(results$e_net_default_prob, results$actual )
#other things: cross validation of lambda (and alpha) using cv.glmnet
#More data transofrmations, interactions