我正在尝试进行判别分析并继续遇到以下错误:
Error in sqrt((n * prior) * fac) * scale(group.means, center = xbar, scale = FALSE) %*% :
non-conformable arrays
Calls: lda -> lda.formula -> lda.default
这是我的代码细分为几行:
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
这可能是微不足道的......
答案 0 :(得分:3)
问题是您的对象prior
属于table
类,但lda
需要您的先验为vector
。
一个简单的解决方法是对表
的结果使用as.vector
prior <- as.vector(counts / sum(counts))
z <- lda(categories ~ values, dat, prior=prior)
predict(z, data)$class
[1] 1 1 2 1 1 1 1 2 2 1 2 3 3 2 2 2 2 2 1 2 3 3 3 3 3 3 3 3 3 3 2 3 3 3 3 1 3 1
[39] 2 3 3 2 1 1 2 1 1 3 3 3 1 1 2 1 2 1 2 1 2 1 1 1 2 1 2 2 2 2 3 3 2 3 3 3 3 2
[77] 3 3 2 3 2 1 1 1 1 1 3 1 3 3 3 3
Levels: 1 2 3