How and when to use polynomial regression in R

How and when to use polynomial regression in R

Recipe Objective

How and when to use polynomial regression.

Regression is a measure used for examining the relation between a dependent and independent variable. A polynomial regression is used when the data doesn't follow a linear relation, i.e., it is non-linear in nature. The dependent variable is related to the independent variable which has an nth degree. Polynomial equation — **y= b0+b1x + b2x2+ b3x3+....+ bnxn** The actual difference between a linear regression and a polynomial regression is that, for a linear regression, the dependent and independent variables are linearly related to each other, while using a polynomial regression, a better fit can be achieved when the higher degree of the independent variable term is used in the equation. This recipe demonstrates an example on salaries of 10 employees differing according to their positions in a company — and we use polynomial regression in it.

Access Avocado Machine Learning Project for Price Prediction

Step 1 - Install the necessary packages

install.packages('ggplot2')
install.packages('caret')
install.packages("caTools") # For Linear regression
library(tidyverse) # to illustrate polynomial regression
library(caret)
library(ggplot2)
library(caTools)

Step 2 - Read the data

data <- read.csv("/content/Position_Salaries.csv")
head(data)
dim(data)
# Visualize the data
ggplot(data) + geom_point(aes(Position,Salary),size=3) + theme_bw()

Step 3 - Split the data into train and test data

split <- sample.split(data, SplitRatio = 0.8)
split
train <- subset(data, split == "TRUE")
test <- subset(data, split == "FALSE")
dim(train) # dimension/shape of train dataset
dim(test) # dimension/shape of test dataset

Step 4 - Compute a polynomial regression model

model <- lm(Salary ~ poly(Level, 3, raw = TRUE), # degree of polunomial = 2 data = train)
summary(model)

Step 5 - Predictions on test data

pred = predict(model,test)
print(pred)
test$Salary

Step 6 - Evaluate the performance of the model

rmse_val <- sqrt(mean(pred-test$Salary)^2)
rmse_val
SSE = sum((pred-test$Salary)^2)
SST = sum((pred-mean(test$Salary))^2)
r2_test = 1 - SSE/SST
print(r2_test)

R-squared value of 92 % indicates that our model (degree — 3) made a good prediction over the salaries of the employees.

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Multilabel Classification Project for Predicting Shipment Modes
Multilabel Classification Project to build a machine learning model that predicts the appropriate mode of transport for each shipment, using a transport dataset with 2000 unique products. The project explores and compares four different approaches to multilabel classification, including naive independent models, classifier chains, natively multilabel models, and multilabel to multiclass approaches.

Personalized Medicine: Redefining Cancer Treatment
In this Personalized Medicine Machine Learning Project you will learn to classify genetic mutations on the basis of medical literature into 9 classes.

MLOps Project for a Mask R-CNN on GCP using uWSGI Flask
MLOps on GCP - Solved end-to-end MLOps Project to deploy a Mask RCNN Model for Image Segmentation as a Web Application using uWSGI Flask, Docker, and TensorFlow.

Build Customer Propensity to Purchase Model in Python
In this machine learning project, you will learn to build a machine learning model to estimate customer propensity to purchase.

MLOps using Azure Devops to Deploy a Classification Model
In this MLOps Azure project, you will learn how to deploy a classification machine learning model to predict the customer's license status on Azure through scalable CI/CD ML pipelines.

Build a Similar Images Finder with Python, Keras, and Tensorflow
Build your own image similarity application using Python to search and find images of products that are similar to any given product. You will implement the K-Nearest Neighbor algorithm to find products with maximum similarity.

Build Piecewise and Spline Regression Models in Python
In this Regression Project, you will learn how to build a piecewise and spline regression model from scratch in Python to predict the points scored by a sports team.

Learn Object Tracking (SOT, MOT) using OpenCV and Python
Get Started with Object Tracking using OpenCV and Python - Learn to implement Multiple Instance Learning Tracker (MIL) algorithm, Generic Object Tracking Using Regression Networks Tracker (GOTURN) algorithm, Kernelized Correlation Filters Tracker (KCF) algorithm, Tracking, Learning, Detection Tracker (TLD) algorithm for single and multiple object tracking from various video clips.

Build a Credit Default Risk Prediction Model with LightGBM
In this Machine Learning Project, you will build a classification model for default prediction with LightGBM.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.