How to perform polynomial regression in R

This recipe helps you perform polynomial regression in R

Recipe Objective

How to perform polynomial regression in R.

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. For eg: If we use a quadratic equation, the line into a curve that better fits the data. This recipe demonstrates an example of polynomial regression.

Access Linear Regression ML Project for Beginners with Source Code

Step 1 - Install the necessary packages

install.packages('ggplot2') library(ggplot2)

Step 2 - Generate random data

x <- runif(50, min=0, max=1) print(x) y <- runif(50, min=10, max=50) print(y) df = data.frame(x = x, y = y) head(df)

Step 3 - Visualize the data

plot(df$x, df$y, pch=20, col="black")

Step 4 - Fit the model

model = lm(y~x+I(x^4)+I(x^3)+I(x^2), data = df) # fit the model with 4 degree equation summary(model)

Step 5 -Make predictions on the model and plot the results

pred = predict(model,data=df) lines(df$x, pred, lwd = 3, col = "blue") # using plot() ggplot(data=df, aes(x,y)) + # using ggplot2 geom_point() + geom_smooth(method="lm", formula=y~(x^4)+I(x^3)+I(x^2)) {"mode":"full","isActive":false}

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Learn Hyperparameter Tuning for Neural Networks with PyTorch
In this Deep Learning Project, you will learn how to optimally tune the hyperparameters (learning rate, epochs, dropout, early stopping) of a neural network model in PyTorch to improve model performance.

Build a Multi-Class Classification Model in Python on Saturn Cloud
In this machine learning classification project, you will build a multi-class classification model in Python on Saturn Cloud to predict the license status of a business.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

MLOps Project to Deploy Resume Parser Model on Paperspace
In this MLOps project, you will learn how to deploy a Resume Parser Streamlit Application on Paperspace Private Cloud.

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

Deploy Transformer BART Model for Text summarization on GCP
Learn to Deploy a Machine Learning Model for the Abstractive Text Summarization on Google Cloud Platform (GCP)

Build an Image Segmentation Model using Amazon SageMaker
In this Machine Learning Project, you will learn to implement the UNet Architecture and build an Image Segmentation Model using Amazon SageMaker

LLM Project to Build and Fine Tune a Large Language Model
In this LLM project for beginners, you will learn to build a knowledge-grounded chatbot using LLM's and learn how to fine tune it.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.