Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Visualize the difference between PCA and LDA on the iris dataset.
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)
@thekeele

This comment has been minimized.

Show comment Hide comment
@thekeele

thekeele Nov 4, 2014

Great example! This helped me wrap my head around the lda function. While running this code I noticed a small typo.

16 prop.lda = lda$svd^2/sum(lda$svd^2) <- prop.lda = r$svd^2/sum(r$svd^2)

thekeele commented Nov 4, 2014

Great example! This helped me wrap my head around the lda function. While running this code I noticed a small typo.

16 prop.lda = lda$svd^2/sum(lda$svd^2) <- prop.lda = r$svd^2/sum(r$svd^2)

@castudil

This comment has been minimized.

Show comment Hide comment
@castudil

castudil Nov 17, 2014

Great example! Thanks Keele for the correction on line 16
prop.lda = lda$svd^2/sum(lda$svd^2)

Great example! Thanks Keele for the correction on line 16
prop.lda = lda$svd^2/sum(lda$svd^2)

@AnahitaNodehi

This comment has been minimized.

Show comment Hide comment
@AnahitaNodehi

AnahitaNodehi May 6, 2015

thanks for you example!
I have a question

this example compare PCA and LDA
if i want to compare other type of PCA like kernel PCA or PGA with PCA and see its result on LDA,
what can i do?
i mean comparing pca and pga and see what happen in lda result ???

thanks for you example!
I have a question

this example compare PCA and LDA
if i want to compare other type of PCA like kernel PCA or PGA with PCA and see its result on LDA,
what can i do?
i mean comparing pca and pga and see what happen in lda result ???

@trstno1

This comment has been minimized.

Show comment Hide comment
@trstno1

trstno1 Oct 24, 2016

Hi,
I have a question.

My data.frame have a colum with species names (the first colum) and I want to insert these names in each point. Is it possible?

trstno1 commented Oct 24, 2016

Hi,
I have a question.

My data.frame have a colum with species names (the first colum) and I want to insert these names in each point. Is it possible?

@kelsimarie7

This comment has been minimized.

Show comment Hide comment
@kelsimarie7

kelsimarie7 Mar 11, 2018

yes, you can simply add "labels = yourcolumname"
at least this will work for ggbiplot()

kelsimarie7 commented Mar 11, 2018

yes, you can simply add "labels = yourcolumname"
at least this will work for ggbiplot()

@Vikas-Bhaneriya

This comment has been minimized.

Show comment Hide comment
@Vikas-Bhaneriya

Vikas-Bhaneriya Mar 16, 2018

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)
> 

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)
> 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment