Skip to content
Search
Generic filters
Exact matches only

Linear Classification in R

Last Updated on August 22, 2019

In this post you will discover recipes for 3 linear classification algorithms in R.

All recipes in this post use the iris flowers dataset provided with R in the datasets package. The dataset describes the measurements if iris flowers and requires classification of each observation to one of three flower species.

Discover how to prepare data, fit machine learning models and evaluate their predictions in R with my new book, including 14 step-by-step tutorials, 3 projects, and full source code.

Let’s get started.

binary classification

Red vs Blue
Photo by Robert Couse-Baker, some rights reserved

Logistic Regression

Logistic Regression is a classification method that models the probability of an observation belonging to one of two classes. As such, normally logistic regression is demonstrated with binary classification problem (2 classes). Logistic Regression can also be used on problems with more than two classes (multinomial), as in this case.

This recipe demonstrates multinomial logistic regression method on the iris dataset.

# load the package
library(VGAM)
# load data
data(iris)
# fit model
fit <- vglm(Species~., family=multinomial, data=iris)
# summarize the fit
summary(fit)
# make predictions
probabilities <- predict(fit, iris[,1:4], type=”response”)
predictions <- apply(probabilities, 1, which.max)
predictions[which(predictions==”1″)] <- levels(iris$Species)[1]
predictions[which(predictions==”2″)] <- levels(iris$Species)[2]
predictions[which(predictions==”3″)] <- levels(iris$Species)[3]
# summarize accuracy
table(predictions, iris$Species)

# load the package

library(VGAM)

# load data

data(iris)

# fit model

fit <- vglm(Species~., family=multinomial, data=iris)

# summarize the fit

summary(fit)

# make predictions

probabilities <- predict(fit, iris[,1:4], type=”response”)

predictions <- apply(probabilities, 1, which.max)

predictions[which(predictions==”1″)] <- levels(iris$Species)[1]

predictions[which(predictions==”2″)] <- levels(iris$Species)[2]

predictions[which(predictions==”3″)] <- levels(iris$Species)[3]

# summarize accuracy

table(predictions, iris$Species)

Learn more about the vglm function in the VGAM package.

Need more Help with R for Machine Learning?

Take my free 14-day email course and discover how to use R on your project (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

Start Your FREE Mini-Course Now!

Linear Discriminant Analysis

LDA is a classification method that finds a linear combination of data attributes that best separate the data into classes.

This recipes demonstrates the LDA method on the iris dataset.

# load the package
library(MASS)
data(iris)
# fit model
fit <- lda(Species~., data=iris)
# summarize the fit
summary(fit)
# make predictions
predictions <- predict(fit, iris[,1:4])$class
# summarize accuracy
table(predictions, iris$Species)

# load the package

library(MASS)

data(iris)

# fit model

fit <- lda(Species~., data=iris)

# summarize the fit

summary(fit)

# make predictions

predictions <- predict(fit, iris[,1:4])$class

# summarize accuracy

table(predictions, iris$Species)

Learn more about the lda function the MASS package.

Partial Least Squares Discriminant Analysis

Partial Least Squares Discriminate Analysis is the application of LDA on a dimension-reducing projection of the input data (partial least squares).

This recipe demonstrates the PLSDA method on the iris dataset.

# load the package
library(caret)
data(iris)
x <- iris[,1:4]
y <- iris[,5]
# fit model
fit <- plsda(x, y, probMethod=”Bayes”)
# summarize the fit
summary(fit)
# make predictions
predictions <- predict(fit, iris[,1:4])
# summarize accuracy
table(predictions, iris$Species)

# load the package

library(caret)

data(iris)

x <- iris[,1:4]

y <- iris[,5]

# fit model

fit <- plsda(x, y, probMethod=”Bayes”)

# summarize the fit

summary(fit)

# make predictions

predictions <- predict(fit, iris[,1:4])

# summarize accuracy

table(predictions, iris$Species)

Learn more about the plsda function in the caret package.

Summary

In this post, you discovered 3 recipes for linear classification that you can copy and paste into your own problem.

Discover Faster Machine Learning in R!

Master Machine Learning With R

Develop Your Own Models in Minutes

…with just a few lines of R code

Discover how in my new Ebook:
Machine Learning Mastery With R

Covers self-study tutorials and end-to-end projects like:
Loading data, visualization, build models, tuning, and much more…

Finally Bring Machine Learning To Your Own Projects

Skip the Academics. Just Results.

See What’s Inside

About Jason Brownlee

Jason Brownlee, PhD is a machine learning specialist who teaches developers how to get results with modern machine learning methods via hands-on tutorials.

error: Content is protected !!