I perform elastic network regularization in the carriage with glmnet.
I pass a sequence of values trainControlfor alpha and lambda, then I execute repeatedcvto get the optimal alpha and lambda settings.
Here is an example where the optimal settings for alpha and lambda are 0.7 and 0.5, respectively:
age <- c(4, 8, 7, 12, 6, 9, 10, 14, 7, 6, 8, 11, 11, 6, 2, 10, 14, 7, 12, 6, 9, 10, 14, 7)
gender <- make.names(as.factor(c(1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1)))
bmi_p <- c(0.86, 0.45, 0.99, 0.84, 0.85, 0.67, 0.91, 0.29, 0.88, 0.83, 0.48, 0.99, 0.80, 0.85,
0.50, 0.91, 0.29, 0.88, 0.99, 0.84, 0.80, 0.85, 0.88, 0.99)
m_edu <- make.names(as.factor(c(0, 1, 1, 2, 2, 3, 2, 0, 1, 1, 0, 1, 2, 2, 1, 2, 0, 1, 1, 2, 2, 0 , 1, 0)))
p_edu <- make.names(as.factor(c(0, 2, 2, 2, 2, 3, 2, 0, 0, 0, 1, 2, 2, 1, 3, 2, 3, 0, 0, 2, 0, 1, 0, 1)))
f_color <- make.names(as.factor(c("blue", "blue", "yellow", "red", "red", "yellow",
"yellow", "red", "yellow","blue", "blue", "yellow", "red", "red", "yellow",
"yellow", "red", "yellow", "yellow", "red", "blue", "yellow", "yellow", "red")))
asthma <- make.names(as.factor(c(1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1)))
x <- data.frame(age, gender, bmi_p, m_edu, p_edu, f_color, asthma)
tuneGrid <- expand.grid(alpha = seq(0, 1, 0.05), lambda = seq(0, 0.5, 0.05))
fitControl <- trainControl(method = 'repeatedcv', number = 3, repeats = 5, classProbs = TRUE, summaryFunction = twoClassSummary)
set.seed(1352)
model.test <- caret::train(asthma ~ age + gender + bmi_p + m_edu + p_edu + f_color, data = x, method = "glmnet",
family = "binomial", trControl = fitControl, tuneGrid = tuneGrid,
metric = "ROC")
model.test$bestTune
My question
When I run as.matrix(coef(model.test$finalModel)), which I would suggest, give me the coefficients that match the best model, I get 100 different sets of coefficients.
So, how do I get the odds for the best setting?
, coef(model.test$finalModel, model.test$bestTune$lambda) NULL- , , .
EDIT:
, , , , , , model.test$finalModel , -, coef(model.test$finalModel, model.caret$bestTune$lambda) , . , . , , , , . - , model.test$finalModel , ? , . !