2017-03-06 65 views
0

我正在使用MASS包中的qda函數來練習瑞士銀行券數據集,但是當我在擬合模型上預測()時出現錯誤,問題是,qda返回一個「列表」而不是「qda」類:R qda {MASS}返回一個不是qda類的列表類

`str(swiss)` 
`'data.frame':200 obs. of 7 variables: 
$ Status : Factor w/ 2 levels "counterfeit",..: 2 2 2 2 2 2 2 2 2 2 ... 
$ Length : num 215 215 215 215 215 ... 
$ Left : num 131 130 130 130 130 ... 
$ Right : num 131 130 130 130 130 ... 
$ Bottom : num 9 8.1 8.7 7.5 10.4 9 7.9 7.2 8.2 9.2 ... 
$ Top  : num 9.7 9.5 9.6 10.4 7.7 10.1 9.6 10.7 11 10 ... 
$ Diagonal: num 141 142 142 142 142 ...` 

`qda.fit <- qda(Status ~., data = swiss, prior = c(0.99, 0.01), CV = TRUE) 
test <- data.frame(Length = 214.9, Left = 130.1, Right = 129.9, Bottom = 9, Top = 10.6, Diagonal = 140.5) 
qda.pred <- predict(qda.fit, test) 
Error in UseMethod("predict") : 
    no applicable method for 'predict' applied to an object of class "list" 
Class(qda.fit) 
    [1] "list"` 


`sessionInfo() 
R version 3.3.2 (2016-10-31) 
Platform: x86_64-w64-mingw32/x64 (64-bit) 
Running under: Windows 7 x64 (build 7601) Service Pack 1` 

`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] stats  graphics grDevices utils  datasets methods 
[7] base ` 

`other attached packages: 
[1] MASS_7.3-45` 

`loaded via a namespace (and not attached): 
[1] tools_3.3.2` 

希望你能幫上忙。

謝謝,

XP的

回答

0

注意警告手冊中:

類的對象 「QDA」 含有以下成分:

...

除非CV = TRUE,當返回值是列表 wi個組件:

...

套裝CV = F,然後使用對象predict

qda.fit <- qda(Status ~., data = banknote, prior = c(0.99, 0.01), CV = F) 
test <- data.frame(Length = 214.9, Left = 130.1, 
        Right = 129.9, Bottom = 9, 
        Top = 10.6, Diagonal = 140.5) 
qda.pred <- predict(qda.fit, test) 

qda.pred 
$class 
[1] genuine 
Levels: counterfeit genuine 

$posterior 
    counterfeit genuine 
1 0.02416251 0.9758375 
+0

謝謝你的提示答案。是的,我已經嘗試過了,但是在SAS中'proc discriminm data = combine pool = test crossvalidate testdata = test testout = a;'它通過交叉驗證並給出了後驗概率:假冒:0.000002526,真正的1儘管它不會影響R案例中的分類,但仍是準確的。我想我的問題應該是:我想在這種情況下用qda()進行交叉驗證,並希望得到與SAS輸出中類似的結果。 –

相關問題