Doubts about discriminant analysis in R

Asked

Viewed 384 times

2

I’m trying to reproduce the accounts of example 5 of this pdf about discriminant analysis. When doing the accounts without using any package, get the same results of the file, as code below. However, when using the package MASS of the software R and the function lda() accounts don’t add up. What mistake am I making using the function of this package?

rm(list = ls())
cat("\014")
RacaA <- data.frame(x1=c(6.36, 5.92, 5.92,6.44,6.40,6.56,6.64,6.68,6.72,6.76,6.72),
x2=c(5.24,5.12,5.36,5.64,5.16,5.56,5.36,4.96,5.48,5.60,5.08))
n1 <- length(RacaA$x1)

RacaB <- data.frame(x1=c(6.00,5.60,5.64,5.76,5.96,5.72,5.64,5.44,5.04,4.56,5.48,5.76),
x2=c(4.88,4.64,4.96,4.80,5.08,5.04,4.96,4.88,4.44,4.04,4.20,4.80))
n2 <- length(RacaB$x1)

barx1A <- mean(RacaA$x1)
barx2A <- mean(RacaA$x2)
v1 <- rbind(barx1A,barx2A)
CovRA <- cov(RacaA)
barx1B <- mean(RacaB$x1)
barx2B <- mean(RacaB$x2)
v2 <- rbind(barx1B,barx2B)
CovRB <- cov(RacaB)

Sc <- ((n1-1)/((n1-1)+(n2-1)))*CovRA+((n2-1)/((n1-1)+(n2-1)))*CovRB
Scinv <- solve(Sc)
x1_x2 <- cbind(v1-v2)
Dx <- t(x1_x2)%*%Scinv

DxA <- Dx%*%v1
DxB <- Dx%*%v2

m <- (1/2)*(DxA+DxB)
m

library(MASS)

m.X <- rbind(RacaA,RacaB)
RA = rep("RacaA", 11)
RB = rep("RacaB", 12)
Raca <- c(RA,RB)
m.Raca <- data.frame(m.X,Raca)

result.ad <- lda(Raca~., m.Raca)
pred<-predict(result.ad)$class
data.pred <- Raca
table(data.pred,pred)
plot(result.ad)
result.ad
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.