R qda {MASS}返回列表类而不是qda类

时间:2017-03-06 00:52:13

标签: r

我使用MASS包中的qda函数来练习瑞士钞票数据集,但是当我在拟合模型上预测()时出现错误,问题是qda返回“list”而不是“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的

1 个答案:

答案 0 :(得分:0)

请注意手册中的警告:

  

班级的对象&#34; qda&#34;包含以下组件:

     

...

     

除非CV = TRUE,否则返回值为列表且包含组件:

     

...

设置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