Hiva
Hiva

Reputation: 13

Store varImp output to a matrix

I have a dataframe with 117 variables and 1000 cases (i.e. rows). I'm using varImp{caret} to run a PLS model with 100 iterations.

I want to store variables' name and Overall value from the varImp() output in a matrix of 117x100.

How can I store varImp() output in a matrix. I tried:

a1 <- matrix(0,127,100)
a2 <- varImp(model.D60, scale = TRUE)
a3 <- varImp(model.D60, scale = TRUE)$importance

Upvotes: 1

Views: 2765

Answers (1)

topepo
topepo

Reputation: 14331

It is pretty easy:

> library(caret)
> 
> set.seed(1)
> dat <- SLC14_1(200)
> 
> set.seed(2)
> mod <- train(y ~ ., data = dat, 
+              method = "pls",
+              preProc = c("center", "scale"),
+              tuneLength = 10)
> 
> ## what's inside?
> str(varImp(mod))
List of 3
$ importance:'data.frame':  20 obs. of  1 variable:
  ..$ Overall: num [1:20] 24.936 0.174 27.584 21.314 34.648 ...
$ model     : chr "pls"
$ calledFrom: chr "varImp"
- attr(*, "class")= chr "varImp.train"
> 
> ## This is a data frame:
> str(varImp(mod)$importance)
'data.frame':   20 obs. of  1 variable:
  $ Overall: num  24.936 0.174 27.584 21.314 34.648 ...
> 
> ## convert to matrix
> imps <- as.matrix(varImp(mod)$importance)

Upvotes: 4

Related Questions