# How to perform a backward elimination approach using R to increase the accuracy of a linear regression model in R? Explain with an example

819    Asked by Deepabhawana in Data Science , Asked on Dec 21, 2019

For explaining backward elimination approach, let us explain with a model.

First we import the dataset

# Importing the dataset

We encode categorical data

# Encoding categorical data

dataset\$State = factor(dataset\$State,levels = c('New York', 'California', 'Florida'), labels = c(1, 2, 3))

Now we split the data for training and testing

# Splitting the dataset into the Training set and Test set

# install.packages('caTools')

library(caTools)

set.seed(123)

split = sample.split(dataset\$Profit, SplitRatio = 0.8)

training_set = subset(dataset, split == TRUE)

test_set = subset(dataset, split == FALSE)

# Feature Scaling

# training_set = scale(training_set)

# test_set = scale(test_set)

Now we fit the model

# Fitting Multiple Linear Regression to the Training set

regressor = lm(formula = Profit ~ .,

data = training_set)

# Predicting the Test set results

y_pred = predict(regressor, newdata = test_set)

Now we will see how to build the optimal model using backward elimination model

# Building the optimal model using Backward Elimination

regressor = lm(formula = Profit ~ R.D.Spend + Administration + Marketing.Spend + State,

data = dataset)

summary(regressor)

# Optional Step: Remove State2 only (as opposed to removing State directly)

# regressor = lm(formula = Profit ~ R.D.Spend + Administration + Marketing.Spend + factor(State, exclude = 2),

# data = dataset)

# summary(regressor)

regressor = lm(formula = Profit ~ R.D.Spend + Administration + Marketing.Spend,

data = dataset)

summary(regressor)

regressor = lm(formula = Profit ~ R.D.Spend + Marketing.Spend,

data = dataset)

summary(regressor)

regressor = lm(formula = Profit ~ R.D.Spend,

data = dataset)

summary(regressor)