我正在浏览 Max Kuhn 和 Kjell Johnson 的非常好的书“应用预测建模”中的示例,不幸的是,我陷入了使用该train()
函数的示例之一和支持交叉验证包GermanCredit
提供的数据集之一caret
向量机:
library(AppliedPredictiveModeling)
library(caret)
# preparing the data
data(GermanCredit)
GermanCredit <- GermanCredit[, -nearZeroVar(GermanCredit)]
GermanCredit$CheckingAccountStatus.lt.0 <- NULL
GermanCredit$SavingsAccountBonds.lt.100 <- NULL
GermanCredit$EmploymentDuration.lt.1 <- NULL
GermanCredit$EmploymentDuration.Unemployed <- NULL
GermanCredit$Personal.Male.Married.Widowed <- NULL
GermanCredit$Property.Unknown <- NULL
GermanCredit$Housing.ForFree <- NULL
set.seed(100)
inTrain <- createDataPartition(GermanCredit$Class, p = .8)[[1]]
GermanCreditTrain <- GermanCredit[ inTrain, ]
GermanCreditTest <- GermanCredit[-inTrain, ]
# Grid selection for `sigma` and `cost` tuning parameters:
library(kernlab)
set.seed(231)
sigDist <- sigest(Class ~ ., data = GermanCreditTrain, frac = 1)
svmTuneGrid <- data.frame(.sigma = sigDist[1], .C = 2^(-2:7))
# SVM classification and cross-validation
svmFit <- train(Class ~ .,
data = GermanCreditTrain,
method = "svmRadial",
preProc = c("center", "scale"),
tuneGrid = svmTuneGrid,
trControl = trainControl(method = "repeatedcv", repeats = 5,
classProbs = TRUE))
它抛出了这个错误:
Error in comp(expr, env = envir, options = list(suppressUndefined = TRUE)) :
could not find function "makeCenv"
有时这个错误信息:
Loading required package: class
Warning: namespace ‘compiler’ is not available and has been replaced
by .GlobalEnv when processing object ‘GermanCredit’
Error in comp(expr, env = envir, options = list(suppressUndefined = TRUE)) :
could not find function "makeCenv"
In addition: Warning message:
executing %dopar% sequentially: no parallel backend registered
然后我了解到这makeCenv()
是在doMC
建议作为并行计算或并行处理的替代方案的包中,但我不会选择这个包,因为它在 Windows 平台上不可用,我猜。有什么选择吗?
更新:
这些错误仅在代码运行时出现Rstudio IDE
,默认的 R 控制台一切正常,所以问题出在 Rstudio 本地,我猜。R 控制台的时间有点长(大约 8 分钟),不过,鉴于下面提到的硬件规格,我想知道如何加快速度。
我的 sessioninfo() 输出在这里(Rstudio):
R version 3.0.2 (2013-09-25)
Platform: i386-w64-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] datasets grid splines utils stats graphics grDevices methods
[9] base
other attached packages:
[1] proxy_0.4-10 e1071_1.6-1
[3] class_7.3-9 kernlab_0.9-19
[5] caret_5.17-7 foreach_1.4.1
[7] AppliedPredictiveModeling_1.1-4 CORElearn_0.9.42
[9] rpart_4.1-3 xtable_1.7-1
[11] knitr_1.5 texreg_1.30
[13] pastecs_1.3-15 boot_1.3-9
[15] gridExtra_0.9.1 reshape2_1.2.2
[17] plyr_1.8 scales_0.2.3
[19] ggplot2_0.9.3.1 vcdExtra_0.5-11
[21] gnm_1.0-6 vcd_1.3-1
[23] corrplot_0.73 RColorBrewer_1.0-5
[25] car_2.0-19 Hmisc_3.13-0
[27] Formula_1.1-1 cluster_1.14.4
[29] xlsx_0.5.5 xlsxjars_0.5.0
[31] rJava_0.9-5 lmPerm_1.1-2
[33] coin_1.0-23 survival_2.37-4
[35] GPArotation_2012.3-1 psych_1.3.12
[37] sos_1.3-8 brew_1.0-6
[39] data.table_1.8.10 mice_2.18
[41] nnet_7.3-7 MASS_7.3-29
[43] lattice_0.20-23
loaded via a namespace (and not attached):
[1] codetools_0.2-8 colorspace_1.2-4 dichromat_2.0-0 digest_0.6.4
[5] evaluate_0.5.1 formatR_0.10 gtable_0.1.2 iterators_1.0.6
[9] labeling_0.2 Matrix_1.1-0 modeltools_0.2-21 munsell_0.4.2
[13] mvtnorm_0.9-9996 proto_0.3-10 qvcalc_0.8-8 relimp_1.0-3
[17] stats4_3.0.2 stringr_0.6.2 tcltk_3.0.2 tools_3.0.2
默认 R 控制台的 sessionInfo() 输出:
R version 3.0.2 (2013-09-25)
Platform: i386-w64-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] datasets grDevices grid splines graphics utils stats
[8] methods base
other attached packages:
[1] e1071_1.6-1 class_7.3-9 kernlab_0.9-19 caret_5.17-7
[5] foreach_1.4.1 cluster_1.14.4 lattice_0.20-23 reshape2_1.2.2
[9] plyr_1.8 scales_0.2.3 ggplot2_0.9.3.1 lmPerm_1.1-2
[13] coin_1.0-23 survival_2.37-4 sos_1.3-8 brew_1.0-6
loaded via a namespace (and not attached):
[1] codetools_0.2-8 colorspace_1.2-4 compiler_3.0.2 dichromat_2.0-0
[5] digest_0.6.3 gtable_0.1.2 iterators_1.0.6 labeling_0.2
[9] MASS_7.3-29 modeltools_0.2-21 munsell_0.4.2 mvtnorm_0.9-9996
[13] proto_0.3-10 RColorBrewer_1.0-5 stats4_3.0.2 stringr_0.6.2
[17] tools_3.0.2
问题:
必须有一个交互,
Rstudio
因为它在默认 R 控制台中运行良好,基于默认 R 控制台和 Rstudio 的两个 sessionInfo() 输出,区别在于compiler
包。奇怪,在 CRAN 中找不到这个 pkg,我在这里找到了一个注释: http://www.inside-r.org/r-doc/compiler/compile 说 load(compiler) 就足够了,当我这样做时Rstudio:无法使用此错误消息:错误:包“编译器”是在 R 3.0.0 之前构建的:请重新安装它
更新
在将编译器包库从默认R lib路径复制并粘贴到Rstudio lib路径之后,它终于从withing Rstudio工作了,但是时间仍然太长(大约8分钟),我会发布一个单独的并行处理问题鉴于下面的硬件和 Windows,如果这有助于更快地找到答案。
- 我的笔记本电脑是 2.1GHz 双核处理器,3GB,windows 32bit,知道如何用
train()
函数进行并行处理吗?您能否为此发出 R 代码,我将非常感激。