我看过一些与我有类似问题的帖子。与其他帖子不同,我的帖子带有重现错误的代表。我一直在使用 kernlab 生成带有拉普拉斯内核的最小二乘 SVM 模型。因变量是二元的,所有自变量都是分类的。我可以拟合模型,lssvm
并且可以用来predict
为所有数据生成预测。但是,如果我尝试仅预测数据的一个子集,例如,使用head
. 任何帮助将不胜感激。
重复:
#Load needed packages
suppressMessages(library(dplyr))
library(kernlab)
#Set seed for reproducability
set.seed(1)
#Generate some dummy data
ex_data <- tibble(
a = letters[sample.int(length(letters), 100, T)],
b = letters[sample.int(length(letters), 100, T)],
c = letters[sample.int(length(letters), 100, T)],
y = runif(100, (a<'r')/2)*runif(100, (b<'t')/4)+runif(100, (c<'t')/2)+runif(100, (b<'g')/4)
) %>%
mutate(
y = factor(y>1.5)
)
#Fit the model
model <- lssvm(y~., ex_data, kernal = 'laplacedot')
#> Using automatic sigma estimation (sigest) for RBF or laplace kernel
#Works to predict on all data
invisible(predict(model, ex_data))
#Fails to predict on the head of the data
predict(model, head(ex_data))
#> Error in .local(object, ...): test vector does not match model !
##Some posts suggest removing the dependent variable
predict(model, head(ex_data[-4]))
#> Error in .local(object, ...): test vector does not match model !
由reprex 包于 2021-08-16 创建 (v2.0.0 )