3
votes

I'm using caret to train models over resamples and tune learning parameters, and I can interrogate the probabilities for each test, which is great. But I'm also keen to retain the model objects and use them later without retraining -- is this possible? Basically rather than just the mdl$finalModel object, I'd like the model object for each iteration of tuning.

2

2 Answers

4
votes

Not really. You could write a custom method and modify the fit function to save them out to a file. Inside the fit function, you would know the tuning parameter value but not what resample that the model was build with.

Max

3
votes

Thanks Max. I'm using your suggestion so I'm posting my code here should anyone else want to try this. I am working out the resample later by also saving rownames(x).

# Copy all model structure info from existing model type
cust.mdl <- getModelInfo("rf", regex=FALSE)[[1]]

# Override fit function so that we can save the iteration
cust.mdl$fit <- function(x=x, y=y, wts=wts, param=param, lev=lev, last=last, classProbs=classProbs, ...) {
  # Dont save the final pass (dont train the final model across the entire training set)
  if(last == TRUE) return(NULL) 

  # Fit the model
  fit.obj <- getModelInfo("rf", regex=FALSE)[[1]]$fit(x, y, wts, param, lev, last, classProbs, ...)

  # Create an object with data to save and save it
  fit.data <- list(resample=rownames(x),
                   mdl=fit.obj,
                   #x, y, wts,
                   param=param, lev=lev, last=last, classProbs=classProbs, 
                   other=list(...))

  # Create a string representing the tuning params
  param.str <- paste(lapply(1:ncol(param), function(x) {
                     paste0(names(param)[x], param[1,x])
                    }), collapse="-")

  save(fit.data, file=paste0("rf_modeliter_", sample(1000:9999,1), "_", param.str, ".RData"))
  return (fit.obj)
}