-
-
Save thigm85/8424654 to your computer and use it in GitHub Desktop.
require(MASS) | |
require(ggplot2) | |
require(scales) | |
require(gridExtra) | |
pca <- prcomp(iris[,-5], | |
center = TRUE, | |
scale. = TRUE) | |
prop.pca = pca$sdev^2/sum(pca$sdev^2) | |
lda <- lda(Species ~ ., | |
iris, | |
prior = c(1,1,1)/3) | |
prop.lda = r$svd^2/sum(r$svd^2) | |
plda <- predict(object = lda, | |
newdata = iris) | |
dataset = data.frame(species = iris[,"Species"], | |
pca = pca$x, lda = plda$x) | |
p1 <- ggplot(dataset) + geom_point(aes(lda.LD1, lda.LD2, colour = species, shape = species), size = 2.5) + | |
labs(x = paste("LD1 (", percent(prop.lda[1]), ")", sep=""), | |
y = paste("LD2 (", percent(prop.lda[2]), ")", sep="")) | |
p2 <- ggplot(dataset) + geom_point(aes(pca.PC1, pca.PC2, colour = species, shape = species), size = 2.5) + | |
labs(x = paste("PC1 (", percent(prop.pca[1]), ")", sep=""), | |
y = paste("PC2 (", percent(prop.pca[2]), ")", sep="")) | |
grid.arrange(p1, p2) |
there is some fault here in line 12
it should be 'r' in place of 'lda' on left side
> lda <- lda(Species ~ .,
> iris,
> prior = c(1,1,1)/3)
>
should be like
>
> r <- lda(Species ~ .,
> iris,
> prior = c(1,1,1)/3)
>
Dear @thigm85 ,
I'm getting this error (Error in UseMethod("predict") :
no applicable method for 'predict' applied to an object of class "function") when running
plda <- predict(object = lda,
newdata = iris)
Any idea how to fix this?
Works fine for me.
Did you load the package correctly?
Dear @thigm85 ,
I'm getting this error (Error in UseMethod("predict") :
no applicable method for 'predict' applied to an object of class "function") when runningplda <- predict(object = lda, newdata = iris)
Any idea how to fix this?
Hi, it's a cool code to visualize the PCA and LDA.
But I wonder whether you also have an approach to add the arrows of different discriminators onto the LDA plot, so that the contribution and relationship of these discriminators can be partly read simultaneously.
KR - Qing
yes, you can simply add "labels = yourcolumname"
at least this will work for ggbiplot()