2015-10-08 115 views
1

我有一個包含117個變量和1000個案例(即行)的數據框。我使用varImp{caret}以100次迭代運行PLS模型。將varImp輸出存儲到矩陣

我想存儲變量的名稱和來自varImp()輸出的整體值以117x100的矩陣。

如何將varImp()輸出存儲在矩陣中。我想:

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

可以爲用戶提供數據的一小部分重複的例子(使用'您data.frame dput') – pcantalupo

回答

1

這是很容易:

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