Consider the data.frame
:
set.seed(143)
dfs <- data.frame(
x = replicate(n = 3, expr = runif(n = 6, min = 1, max = 7)),
groups = c(1, 2, 3, 1, 2, 3)
)
And analysis:
library(MASS)
lda(
groups ~ ., data = dfs
)
#Call:
#lda(groups ~ ., data = dfs)
#Prior probabilities of groups:
# 1 2 3
#0.3333333 0.3333333 0.3333333
#Group means:
# x.1 x.2 x.3
#1 5.079522 3.999876 6.575102
#2 1.252230 4.056481 2.908588
#3 6.077131 3.346027 4.457434
#Coefficients of linear discriminants:
# LD1 LD2
#x.1 -0.7956836 0.4144415
#x.2 1.8004719 0.2432832
#x.3 -1.6165556 -0.5778179
#Proportion of trace:
# LD1 LD2
#0.977 0.023
I used candisc
to output the discriminant scores:
library(candisc)
reg <- lm(as.matrix(cbind(dfs[c(1:3)])) ~ groups, dfs)
candisc(reg)[["scores"]]
# groups Can1
#1 1 0.18926009
#2 2 -0.34437654
#3 3 0.34961281
#4 1 1.76549348
#5 2 -0.02758652
#6 3 -1.93240332
Consider the formula:
I tried to calculate the discriminant scores manually for each case. Consider the first case of dfs
only:
dfs[1, 1:3]
x.1 x.2 x.3
1 6.659352 4.623022 6.736206
(-0.7956836 * 6.659352) + (1.8004719 * 4.623022) + (-1.6165556 * 6.736206)
The result is: -7.864568
But:
candisc(reg)[["scores"]]
# groups Can1
#1 1 0.18926009
#2 2 -0.34437654
#3 3 0.34961281
#4 1 1.76549348
#5 2 -0.02758652
#6 3 -1.93240332
Does not contain the value -7.864568
.
How do I calculate the discriminant score manually using the data emitted by the MASS::lda
function?