I’m trying to conduct a discriminant analysis and keep running into the following error:
Error in sqrt((n * prior) * fac) * scale(group.means, center = xbar, scale = FALSE) %*% :
non-conformable arrays
Calls: lda -> lda.formula -> lda.default
Here is my code broken down to a few lines:
categories <- c(1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 3, 1, 1, 3, 3, 2, 1, 1, 3, 1, 1, 3, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3)
values <- c(1, 2, 3, 2, 0, 2, 2, 3, 3, 1, 3, 5, 5, 4, 3, 3, 4, 4, 2, 4, 7, 6, 7, 5, 7, 7, 7, 7, 7, 5, 3, 6, 7, 7, 7, 2, 5, 0, 3, 7, 6, 3, 2, 2, 4, 2, 2, 5, 5, 6, 2, 2, 4, 1, 3, 0, 3, 1, 4, 1, 1, 2, 4, 2, 4, 3, 3, 4, 7, 6, 4, 7, 6, 7, 7, 3, 6, 7, 4, 7, 3, 1, 2, 0, 2, 2, 5, 2, 7, 6, 6, 7)
data <- data.frame(categories=categories, values=values)
counts <- table(data[["categories"]])
prior <- counts / sum(counts)
z <- lda(categories ~ values, data, prior=prior)
predict(z, data)$class
It is probably something trivial…
The problem is that your object
prioris of classtable, butldaneeds your priors to be avector.A simple workaround is to use
as.vectoron the results of table