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

library(VGAM)
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)
predictions[which(predictions==”2″)] <- levels(iris\$Species)
predictions[which(predictions==”3″)] <- levels(iris\$Species)
# summarize accuracy
table(predictions, iris\$Species)

library(VGAM)

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)

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

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

# summarize accuracy

table(predictions, iris\$Species)

### 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.

## 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.

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)

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)

## 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.

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)

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)

## 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! #### 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:

#### Finally Bring Machine Learning To Your Own Projects 