gpt4 book ai didi

r - 无法绘制 svm 图。 terms.default(x) 错误 : no terms component nor attribute

转载 作者:行者123 更新时间:2023-12-01 09:53:40 31 4
gpt4 key购买 nike

我可以在我的数据集上使用 R 包“e1071”运行 svm,但我无法使用任何两个预测变量绘制图形。即使谷歌搜索了很多,我也无法找到它的解决方案。请高手帮我解决这个问题:

我有一个具有以下属性的数据集:

> dim(fulldata)
[1] 153 2915

> str(fulldata)
'data.frame': 153 obs. of 2915 variables:
$ label : Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 ...
$ V12 : num 1.372 -1.541 0.201 1.06 1.815 ...
$ V14 : num 0.052 -1.442 1.022 -0.35 0.192 ...
$ V17 : num 0.885 -2.569 0.326 1.574 1.394 ...
$ V37 : num 0.356 2.884 -0.452 0.067 0.282 ...
$ V51 : num -0.018 -1.71 1.084 -0.21 0.091 ...
$ V66 : num 0.178 0.264 -3.189 0.451 0.831 ...
$ V67 : num 0.693 -0.698 -0.335 0.076 1.016 ...
$ V69 : num -0.383 -0.316 1.332 -0.643 0.195 ...
$ V70 : num 0.203 0.196 -0.83 0.024 0.257 -0.33 -0.731 0.668 0.39 -0.12 ...

我可以使用 e1071 包运行 svm,但无法使用任何两个预测变量进行绘图

svm(y=fulldata[,1], x=fulldata[,-1], probability=T,na.rm=T, kernel="linear")

Call:
svm.default(x = fulldata[, -1], y = fulldata[, 1], kernel = "linear",
probability = T, na.rm = T)


Parameters:
SVM-Type: C-classification
SVM-Kernel: linear
cost: 1
gamma: 0.0003431709

Number of Support Vectors: 60

但是

> plot(svm(y=fulldata[,1], x=fulldata[,-1], probability=T,na.rm=T, 
kernel="linear"),fulldata,fulldata[,2]~fulldata[,3])

Error in terms.default(x) : no terms component nor attribute

这是关于我的 session 的信息

sessionInfo() R version 3.0.1 (2013-05-16) Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
[1] LC_CTYPE=en_IN LC_NUMERIC=C LC_TIME=en_IN
[4] LC_COLLATE=en_IN LC_MONETARY=en_IN LC_MESSAGES=en_IN
[7] LC_PAPER=C LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=en_IN LC_IDENTIFICATION=C

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] MASS_7.3-26 e1071_1.6-3

loaded via a namespace (and not attached):
[1] class_7.3-7 tcltk_3.0.1 tools_3.0.1

最佳答案

来自 ?formula

The models fit by, e.g., the lm and glm functions are specified in a compact symbolic form. The ~ operator is basic in the formation of such models. An expression of the form y ~ model is interpreted as a specification that the response y is modelled by a linear predictor specified symbolically by model. Such a model consists of a series of terms separated by + operators. The terms themselves consist of variable and factor names.

使用带有变量名的公式可以解决这个问题......下面是一个假的例子

library(e1071)
fulldata <- data.frame("label" = gl(2,150),
"V1" = rnorm(300),
"V2" = rnorm(300),
"V3" = rnorm(300),
"V4" = rnorm(300),
"V5" = rnorm(300),
"V6" = rnorm(300),
"V7" = rnorm(300),
"V8" = rnorm(300),
"V9" = rnorm(300),
"V10" = rnorm(300))

str(fulldata)

my.svm <- svm(label ~ .,
probability=TRUE,
na.rm=TRUE,
kernel="linear",
data = fulldata)
my.svm
plot(my.svm,
data = fulldata,
formula = V1 ~ V2)

关于r - 无法绘制 svm 图。 terms.default(x) 错误 : no terms component nor attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22729866/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com