train
doesn't save the model information within a fold. You can save the models out to the file system using a custom model:
glmn_funcs <- getModelInfo("glmnet", regex = FALSE)[[1]]
glmn_funcs$fit <- function(x, y, wts, param, lev, last, classProbs, ...) {
theDots <- list(...)
if(all(names(theDots) != "family")) theDots$family <- "multinomial"
modelArgs <- c(list(x = as.matrix(x), y = y, alpha = param$alpha),
theDots)
out <- do.call("glmnet", modelArgs)
if(!is.na(param$lambda[1])) out$lambdaOpt <- param$lambda[1]
save(out, file = paste("~/tmp/glmn", param$alpha,
floor(runif(1, 0, 1)*100), ## to help uniqueness
format(Sys.time(), "%H_%M_%S.RData"),
sep = "_")
out
}
model <- train(x = iris[,-5],
y = iris$Species,
method = glmn_funcs,
type.gaussian = "naive",
tuneGrid = grid,
trControl = ctrl,
preProc = c("center", "scale"))
You can use the coef
function on each model to get the slopes. Note that train
did not fit all possible models, which is
> length(model$control$index)*nrow(grid)
[1] 5500
(omitting the one for the final model). It fits one per unique alpha per fold:
> length(unique(grid$.alpha))*length(model$control$index)
[1] 275
> length(list.files("~/tmp", pattern = "glmn_")) ##includes the final model
[1] 276
So you will have to do some looping using something like:
> params <- coef(out, s = unique(grid$.lambda), type = "nonzeo")
> names(params) ## a matrix per class
[1] "setosa" "versicolor" "virginica"
> lapply(params, dim)
$setosa
[1] 5 20
$versicolor
[1] 5 20
$virginica
[1] 5 20
Lastly, you don't need to prefix a period before the parameter names using recent versions of caret
.
Max
Best Answer
You can specify
method="none"
intrainControl
. For example:I'm not sure when this was implemented.