trainer <- function(training, testing){
trControl <- trainControl(method="cv", number=5)
modelFit <- train(training$classe ~ ., method="rf", preProcess="pca",
trControl=trControl, data=training)
confMatrix <- confusionMatrix(testing$classe, predict(modelFit,testing))
output <- list(modelFit, confMatrix)
return(output)
}
The returned value is supposed to be a model, modelFit
, which isn't a list, But when I check class(output[1])
, it reports as a list. Seems somehow the model file is converted to a list. How to retain the original data type without converting it a list, because I need to access the model file in the return.
As indicated above, when output
is a list and you use [ ]
to extract elements, you will always get a list back (with the element or elements for the index you specified). However, when you use [[ ]]
you will extract the element at a particular position and that element may have a different class.
You could do sapply(output, class)
to see the classes of all the items in a given list.
So your function seems to be working fine, just make sure you use output<-trainer(...); output[[1]]
output<-trainer(...); output[[1]]
to extract your model.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.