问题
I am currently doing regression analysis on a dataset of mine, and thought that in order to compare different regression models, I could use a table. I would like the table to have the names of the model in the first column, and the predicted values on 1 test point on in the second column.
What I have done now is systematically named these models as follows:
library(caret)
model.lm <- train(formula, data=train, method='lm',...)
model.glmnet<- train(formula, data=train, method='glmnet',...)
...
Modelnames <- c('lm', 'glmnet',...)
results <- setNames(as.data.frame(matrix(nrow=n, ncol=2)), c('Model', 'Prediction'))
results$Model <- Modelnames
results$Prediction <- predict(model. , test) (?)
and so on for several models.
Now, I was wondering if there is any way I could use Modelnames
in order to quickly do predictions per model, rather than having to type predict()
for each model by hand. Is there some way to convert the Modelnames
to the model.
format? Ideally it would be a oneliner, like
results$Prediction <- predict(model.results[,1], test)
or something of the sort. It's probably wrong, but I hope you get the gist of what I meant with that line.
回答1:
When working with multiple models I prefer to work with a dataframe with list columns
example:
require(caret)
require(tidyverse)
dt <- data.frame(method = c('lm', 'glmnet')) %>%
mutate(model = map(method, ~ train(Sepal.Length ~ Petal.Length + Petal.Width,
data = iris,
method = .x))) %>%
mutate(predicted = map(model, predict))
dt %>% select(method,predicted) %>%
unnest()
The last line gives all predicted values of both models in a dataframe. This can easily be altered to give the value of only one prediction.
来源:https://stackoverflow.com/questions/57767226/how-to-use-a-list-of-model-names-and-variables-for-computing-a-table-with-its-pr