目的
我想通过plot
来可视化SVMLinear分类模型。我使用了kernlab
包中提供的示例代码和数据,注意到caret
实际上是通过ksvm
函数训练SVM的(参考源代码(https://github.com/topepo/caret/blob/master/models/files/svmLinear.R))。
问题
当我绘制caret模型对象的最终模型时,未显示图像。经过尝试三种方法后,我仍然无法解决。
代码
require(caret)
require(kernlab)
# ===== sample codes from ksvm
x <- rbind(matrix(rnorm(120),ncol = 2), matrix(rnorm(120, mean = 3), ncol = 2))
y <- matrix(c(rep(1, 60), rep(-1, 60)))
# ===== train linear svm model via three wrappers
#===
#dedicated to caret because if I am not preparing data in this way,
#caret reports errors and stop training with following messages:
#Error in train.default(x, y, weights = w, ...) :
#At least one of the class levels is not a valid R variable name; This will cause errors when class
#probabilities are generated because the variables names will be converted to X.1, X1 . Please use
#factor levels that can be used as valid R variable names (see ?make.names for help).
dat0 <- data.frame(x = x, y= factor(y, levels = c(-1, 1), labels = c('c0', 'c1')))
svp0 <- ksvm(y~., dat0, type = 'C-svc', kernel = 'vanilladot')
dat <- data.frame(x = x, y = as.factor(y))
svp <- ksvm(y~., dat, type = 'C-svc', kernel = 'vanilladot')
svp1 <- ksvm(x, y, kernel = 'vanilladot', type = 'C-svc')
kernlab::plot(svp0, data = dat0) # works
kernlab::plot(svp, data = dat) # works
kernlab::plot(svp1, data = x) # works
ctr <- trainControl(method='cv',
number=5,
classProbs=TRUE,
summaryFunction=twoClassSummary
)
svp.c <- train(y ~., dat0, method = "svmLinear",
trControl = ctr,
preProcess = c('center', 'scale'),
metric = "ROC")
kernlab::plot(svp.c$finalModel, data = dat0) #Not working
# Error in seq.default(min(sub[, 2]), max(sub[, 2]), length = grid) :
# 'from' cannot be NA, NaN or infinite
< p > svp.c$finalModel
的结构(由 caret 训练)与其他使用原始 ksvm
训练的 svp
相同,我认为。但是为什么前者的 plot
不起作用呢?
总之,我想知道是否有人成功地使用 caret
包训练的 SVM 进行了 plot
。
谢谢。
编辑2:附上我的
session.Info()
。希望它有助于确定我错过了什么。R版本3.2.2(2015年8月14日),平台:x86_64-apple-darwin13.4.0(64位),运行在:OS X 10.11.1(El Capitan)。语言环境:1 en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8。已连接的基础包:1 stats graphics grDevices utils datasets methods base。其他相关软件包:1 kernlab_0.9-22 caret_6.0-62 ggplot2_1.0.1 lattice_0.20-33。通过命名空间加载的软件包(未连接):1 Rcpp_0.12.2 magrittr_1.5 splines_3.2.2 MASS_7.3-44 munsell_0.4.2 colorspace_1.2-6 foreach_1.4.3 minqa_1.2.4 stringr_1.0.0 car_2.1-0 plyr_1.8.3 tools_3.2.2 parallel_3.2.2 nnet_7.3-11 pbkrtest_0.4-2 grid_3.2.2 gtable_0.1.2 nlme_3.1-122 mgcv_1.8-7 quantreg_5.19 MatrixModels_0.4-1 iterators_1.0.8 lme4_1.1-9 digest_0.6.8 Matrix_1.2-2 nloptr_1.0.4 reshape2_1.4.1 codetools_0.2-14 stringi_1.0-1 compiler_3.2.0 pROC_1.8 scales_0.3.0.9000 stats4_3.2.2 SparseM_1.7 proto_0.3-10。