在插入符号包中使用glmnet时发现问题。原始glmnet适用于相同的输入数据。随机森林适用于插入符号中的相同数据。
示例代码
library(caret)
library(glmnet)
data(iris)
head(iris)
x = iris[,1:3]
y = iris[, 4]
fit = glmnet(as.matrix(x), y)
print(fit)
#plot(fit, xvar = "lambda", label = TRUE)
rfFit <- caret::train( x=x, y=y, method = 'rf', verbose = TRUE)
rfFit
glmFit <- caret::train( x=x, y=y, method = 'glmnet', verbose = TRUE)
#glmFit
sessionInfo()
输出:
fit = glmnet(as.matrix(x), y)
str(fit, max.level = 1)
List of 12
$ a0 : Named num [1:75] 1.199 1.061 0.934 0.819 0.714 ...
..- attr(*, "names")= chr [1:75] "s0" "s1" "s2" "s3" ...
$ beta :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
$ df : int [1:75] 0 1 1 1 1 1 1 1 1 1 ...
$ dim : int [1:2] 3 75
$ lambda : num [1:75] 0.731 0.666 0.607 0.553 0.504 ...
$ dev.ratio: num [1:75] 0 0.157 0.288 0.397 0.487 ...
$ nulldev : num 86.6
$ npasses : int 493
$ jerr : int 0
$ offset : logi FALSE
$ call : language glmnet(x = as.matrix(x), y = y)
$ nobs : int 150
- attr(*, "class")= chr [1:2] "elnet" "glmnet"
plot(fit, xvar = "lambda", label = TRUE)
rfFit <- caret::train( x=x, y=y, method = 'rf', verbose = TRUE)
note: only 2 unique complexity parameters in default grid. Truncating the grid to 2 .
rfFit
Random Forest
150 samples
3 predictors
No pre-processing
Resampling: Bootstrapped (25 reps)
Summary of sample sizes: 150, 150, 150, 150, 150, 150, ...
Resampling results across tuning parameters:
mtry RMSE Rsquared RMSE SD Rsquared SD
2 0.2042819 0.9299771 0.02026926 0.01435902
3 0.2073273 0.9285442 0.02054641 0.01473437
RMSE was used to select the optimal model using the smallest value.
The final value used for the model was mtry = 2.
glmFit <- caret::train( x=x, y=y, method = 'glmnet', verbose = TRUE)
Something is wrong; all the RMSE metric values are missing:
RMSE Rsquared
Min. : NA Min. : NA
1st Qu.: NA 1st Qu.: NA
Median : NA Median : NA
Mean :NaN Mean :NaN
3rd Qu.: NA 3rd Qu.: NA
Max. : NA Max. : NA
NA's :9 NA's :9
Error in train.default(x = x, y = y, method = "glmnet", verbose = TRUE) :
Stopping
In addition: There were 50 or more warnings (use warnings() to see the first 50)
sessionInfo()
R version 3.1.3 (2015-03-09)
Platform: x86_64-unknown-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats4 parallel stats graphics grDevices utils datasets
[8] methods base
other attached packages:
[1] glmnet_2.0-2 Matrix_1.1-5 elasticnet_1.1
[4] lars_1.2 caret_6.0-64 lattice_0.20-30
[7] plyr_1.8.3 optparse_1.3.2 GenomicRanges_1.18.4
[10] GenomeInfoDb_1.2.5 IRanges_2.0.1 S4Vectors_0.4.0
[13] BiocGenerics_0.12.1 stringr_1.0.0 doMC_1.3.4
[16] iterators_1.0.8 foreach_1.4.3 mclust_5.0.2
[19] randomForest_4.6-10 ggplot2_1.0.1
loaded via a namespace (and not attached):
[1] car_2.0-25 codetools_0.2-10 colorspace_1.2-6 compiler_3.1.3
[5] digest_0.6.8 getopt_1.20.0 grid_3.1.3 gtable_0.1.2
[9] lme4_1.1-10 magrittr_1.5 MASS_7.3-39 MatrixModels_0.4-1
[13] mgcv_1.8-4 minqa_1.2.4 munsell_0.4.2 nlme_3.1-120
[17] nloptr_1.0.4 nnet_7.3-9 pbkrtest_0.4-2 proto_0.3-10
[21] quantreg_5.19 Rcpp_0.12.0 reshape2_1.4.1 scales_0.3.0
[25] SparseM_1.7 splines_3.1.3 stringi_0.5-5 tools_3.1.3
[29] XVector_0.6.0
=====================================
任何建议表示赞赏。提前谢谢。