2015-06-08 10 views
1

我試圖在R中找到具有支持向量機的新輸入向量的類概率。 訓練模型不會顯示錯誤。必須包含支持向量機中R的所有因子的數據集R

fit <-svm(device~.,data=dataframetrain, 
    kernel="polynomial",probability=TRUE) 

但是預測一些輸入向量會顯示一些錯誤。

predict(fit,dataframetest,probability=prob) 
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
contrasts can be applied only to factors with 2 or more levels 

dataframetrain樣子:

> str(dataframetrain) 
'data.frame': 24577 obs. of 5 variables: 
$ device : Factor w/ 3 levels "mob","pc","tab": 1 1 1 1 1 1 1 1 1 1 ... 
$ geslacht : Factor w/ 2 levels "M","V": 1 1 1 1 1 1 1 1 1 1 ... 
$ leeftijd : num 77 67 67 66 64 64 63 61 61 58 ... 
$ invultijd: num 12 12 12 12 12 12 12 12 12 12 ... 
$ type  : Factor w/ 8 levels "A","B","C","D",..: 5 5 5 5 5 5 5 5 5 5 ... 

和dataframetest樣子:

> str(dataframetest) 
'data.frame': 8 obs. of 4 variables: 
$ geslacht : Factor w/ 1 level "M": 1 1 1 1 1 1 1 1 
$ leeftijd : num 20 60 30 25 36 52 145 25 
$ invultijd: num 6 12 2 5 6 8 69 7 
$ type  : Factor w/ 8 levels "A","B","C","D",..: 1 2 3 4 5 6 7 8 

我訓練的模型2個因素 'geslacht' 但有時我只預測數據1個因素「geslacht」。 也許可能只用1個因子'geslacht'來測試類概率?

我希望有人能幫助我!

回答

2

geslacht添加另一個級別(但不是數據)。

x <- factor(c("A", "A"), levels = c("A", "B")) 
x 
[1] A A 
Levels: A B 

x <- factor(c("A", "A")) 
levels(x) <- c("A", "B") 
x 
[1] A A 
Levels: A B 
+0

謝謝!它工作完美:D – karmabob