candisc {lazy.tree} | R Documentation |
Canonical Discriminant Analysis
candisc(y, X, scale = 0, print = 0)
y |
The categorical criterion variable |
X |
The regressor matrix |
scale |
= 1 to make the dispersion of the canonical variable I. |
print |
= 1 to print the result |
This function returns NULL if all the eigenvalues are small.
In order to obtain the canonical discriminant variable, multiply the eigenvectors to X from the right.
A list of:
ndim
# of positive eigen values
eve
Eigen vector matrix
## Not run:
coef1 <- MASS::lda(Species ~ ., iris)$scaling
coef2 <- candisc( iris$Species, iris[,1:4], print=1 )$eve
Print(coef1,coef2)
# iris: categorical y, categorical X
Species <- iris$Species
levels(Species) <- c("set","ver","vir")
Xc <- iris[,-5]
Xc[,3] <- cut(Xc[,3],9, labels=1:9)
Xc[,4] <- cut(Xc[,4],9, labels=1:9)
y <- Species
x <- as.numeric(Xc[,3])
temp <- Unique(x)
ux <- temp$value
loc <- temp$loc
# provide numeric design matrix to lda
# Xd <- model.matrix(~as.factor(x)-1)
# drop the intercept and the first level of x
Xd <- model.matrix(~as.factor(x))[,, drop=0][,-1]
reslda <- MASS::lda( y~Xd, prior=c(1/3,1/3,1/3) )
pred1 <- Xd%*%reslda$scaling
eve <- candisc( y, Xd, print=1)$eve
pred2 <- Xd%*%eve
Print(reslda$scaling,eve)
cor(pred1,pred2)
## End(Not run)