In [1]:
## Load library & dataset
library(MASS)
data(iris)
print(head(iris))
In [2]:
lda.iris <- lda(Species ~ ., iris, prior = c(1,1,1)/3)
lda.pred <- predict(lda.iris, iris)
In [5]:
## Plot the data using LD1 & LD2 attributes
plot(lda.pred$x, pch=as.numeric(lda.pred$class)+16, cex=1.6, col=lda.pred$class, cex.axis=1.6, cex.lab=1.6)
In [19]:
## Standardize: center and scale by standard deviation of each column
## mean and sd before standardization:
colMeans(iris[,1:4])
apply(iris[,1:4], 2, sd)
iris.scaled <- scale(iris[,1:4], center=TRUE, scale=TRUE)
## mean and sd after standardization:
colMeans(iris.scaled[,1:4])
apply(iris.scaled[,1:4], 2, sd)
plot(iris.scaled[,1:2], pch=as.numeric(iris[,5])+16, cex=1.6, col=iris[,5], cex.axis=1.6, cex.lab=1.6)
Out[19]:
Out[19]:
Out[19]:
Out[19]:
In [20]:
## bind the class labels to this iris.scaled
iris.scaled <- as.data.frame(cbind(iris.scaled, iris$Species))
colnames(iris.scaled)[5] = "Species"
head(iris.scaled)
## applying LDA to standardized data:
lda.iris.scaled <- lda(Species ~ ., iris.scaled, prior = c(1,1,1)/3)
lda.pred.scaled <- predict(lda.iris.scaled, iris.scaled)
## Plot the data using LD1 & LD2 attributes
plot(lda.pred.scaled$x, pch=as.numeric(lda.pred.scaled$class)+16, cex=1.6, col=lda.pred.scaled$class, cex.axis=1.6, cex.lab=1.6)
Out[20]:
In [ ]: