0% found this document useful (0 votes)
4 views4 pages

07 - Polynomial Regression

The document performs and compares polynomial regression and linear regression on a dataset containing job position levels and salaries. Polynomial regression is fitted to the dataset and plotted along with the actual datapoints. Linear regression is also fitted and plotted for comparison. The regression models are visualized with different plots and code is shown to make predictions on new data using both models.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

07 - Polynomial Regression

The document performs and compares polynomial regression and linear regression on a dataset containing job position levels and salaries. Polynomial regression is fitted to the dataset and plotted along with the actual datapoints. Linear regression is also fitted and plotted for comparison. The regression models are visualized with different plots and code is shown to make predictions on new data using both models.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

7.

Polynomial Regression
A dataset consisting of Job Positions and their corresponding Salary is given. Perform Polynomial Regression and Plot the graph
of Actual Data points and the predicted polynomial curve. Compare the result with Linear Regression.
# Polynomial Regression
setwd("C:/Users/Praahas/Projects/R-Lab/Polynomial-Regression")
# Importing the dataset
dataset = read.csv("Position_Salaries.csv")
dataset = dataset[2:3]

# Fitting Linear Regression to the dataset


lin_reg = lm(formula = Salary ~ .,
data = dataset)

# Fitting Polynomial Regression to the dataset


dataset$Level2 = dataset$Level^2
dataset$Level3 = dataset$Level^3
dataset$Level4 = dataset$Level^4
poly_reg = lm(formula = Salary ~ .,
data = dataset)

# Visualising the Linear Regression results


# install.packages('ggplot2')
library(ggplot2)
ggplot() +
geom_point(aes(x = dataset$Level, y = dataset$Salary),
colour = 'red') +
geom_line(aes(x = dataset$Level, y = predict(lin_reg, newdata = dataset)),
colour = 'blue') +
ggtitle('Linear Regression') +
xlab('Level') +
ylab('Salary')

28 | P a g e
# Visualising the Polynomial Regression results
# install.packages('ggplot2')
library(ggplot2)
ggplot() +
geom_point(aes(x = dataset$Level, y = dataset$Salary),
colour = 'red') +
geom_line(aes(x = dataset$Level, y = predict(poly_reg, newdata = dataset)),
colour = 'blue') +
ggtitle('Polynomial Regression') +
xlab('Level') +
ylab('Salary')

29 | P a g e
# Visualising the Regression Model results (for higher resolution and smoother curve)
# install.packages('ggplot2')
library(ggplot2)
x_grid = seq(min(dataset$Level), max(dataset$Level), 0.1)
ggplot() +
geom_point(aes(x = dataset$Level, y = dataset$Salary),
colour = 'red') +
geom_line(aes(x = x_grid, y = predict(poly_reg,
newdata = data.frame(Level = x_grid,
Level2 = x_grid^2,
Level3 = x_grid^3,
Level4 = x_grid^4))),
colour = 'blue') +
ggtitle('Polynomial Regression') +
xlab('Level') +
ylab('Salary')

30 | P a g e
# Predicting a new result with Linear Regression
predict(lin_reg, data.frame(Level = 6.5))

# Predicting a new result with Polynomial Regression


predict(poly_reg, data.frame(Level = 6.5,
Level2 = 6.5^2,
Level3 = 6.5^3,
Level4 = 6.5^4))

31 | P a g e

You might also like