将varImp输出存储到矩阵

时间:2015-10-08 08:09:21

标签: r r-caret

我有一个包含117个变量和1000个案例(即行)的数据帧。我使用varImp{caret}运行100次迭代的PLS模型。

我想存储变量'来自varImp()输出的名称和总值,格式为117x100。

如何将varImp()输出存储在矩阵中。我试过了:

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

1 个答案:

答案 0 :(得分:3)

这很简单:

> 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)