1

From this question, I was wondering if it's possible to extract the Quadratic discriminant analysis (QDA's) scores and reuse them after like PCA scores.

## follow example from ?lda
Iris <- data.frame(rbind(iris3[,,1], iris3[,,2], iris3[,,3]),
                   Sp = rep(c("s","c","v"), rep(50,3)))
set.seed(1) ## remove this line if you want it to be pseudo random
train <- sample(1:150, 75)
table(Iris$Sp[train])
## your answer may differ
##  c  s  v
## 22 23 30

Using the QDA here

z <- qda(Sp ~ ., Iris, prior = c(1,1,1)/3, subset = train)

## get the whole prediction object
pred <- predict(z)
## show first few sample scores on LDs

Here, you can see that it's not working.

head(pred$x)
# NULL
plot(LD2 ~ LD1, data = pred$x)
# Error in eval(expr, envir, enclos) : object 'LD2' not found
Community
  • 1
  • 1
M. Beausoleil
  • 3,141
  • 6
  • 29
  • 61
  • That's why I ask the question, I don't know where or how to get the scores that usually are stored in the `x` of the `predict` function. – M. Beausoleil Oct 03 '16 at 23:50
  • The help for `predict.qda` _clearly_ _states_ that it returns `class` (The MAP classification) and `posterior` (posterior probabilities for the classes). – hrbrmstr Oct 04 '16 at 01:41
  • Yeah, but I wonder where are the ordination scores... – M. Beausoleil Oct 04 '16 at 04:46

1 Answers1

0

NOTE: Too long/formatted for a comment. NOT AN ANSWER

You may want to try the rrcov package:

library(rrcov)

z <- QdaCov(Sp ~ ., Iris[train,], prior = c(1,1,1)/3)
pred <- predict(z)
str(pred)
## Formal class 'PredictQda' [package "rrcov"] with 4 slots
##   ..@ classification: Factor w/ 3 levels "c","s","v": 2 2 2 1 3 2 2 1 3 2 ...
##   ..@ posterior     : num [1:41, 1:3] 5.84e-45 5.28e-50 1.16e-25 1.00 1.48e-03 ...
##   ..@ x             : num [1:41, 1:3] -97.15 -109.44 -54.03 2.9 -3.37 ...
##   ..@ ct            : 'table' int [1:3, 1:3] 13 0 1 0 16 0 0 0 11
##   .. ..- attr(*, "dimnames")=List of 2
##   .. .. ..$ Actual   : chr [1:3] "c" "s" "v"
##   .. .. ..$ Predicted: chr [1:3] "c" "s" "v"

It also has robust PCA methods that may be useful.

Unfortunately, not every model in R conforms to the same object structure/API and this won't be a linear model, so it is unlikely to conform to linear model fit structure APIs.

There's an example of how to visualize the qda results here — http://ramhiser.com/2013/07/02/a-brief-look-at-mixture-discriminant-analysis/

And, you can do:

library(klaR)

partimat(Sp ~ ., data=Iris, method="qda", subset=train)

enter image description here

for a partition plot of the qda results.

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • When using your example, I have this error: `Error in solve.default(getCov(mcd)) : system is computationally singular: reciprocal condition number = 3.75922e-17 In addition: Warning message: In covMcd(x = x, raw.only = raw.only, alpha = alpha, nsamp = nsamp, : The 15-th order statistic of the absolute deviation of variable 4 is zero. There are 15 observations (in the entire dataset of 26 obs.) lying on the hyperplane with equation a_1*(x_i1 - m_1) + ... + a_p*(x_ip - m_p) = 0 with (m_1, ..., m_p) the mean of these observations and coefficients a_i from the vector a <- c(0, 0, 0, 1)` – M. Beausoleil Oct 04 '16 at 04:39
  • Also, the `klaR` plot are not showing the ordination space, but the bivariate combinaison of trait values. – M. Beausoleil Oct 04 '16 at 04:47
  • Do I have to conclude that the LDA scores and QDA Scores are identical? – M. Beausoleil Nov 15 '16 at 20:36