Last active
October 29, 2017 16:14
-
-
Save doron2402/5252006a8a061148dffa35d9b09064bc to your computer and use it in GitHub Desktop.
Polynomial Regression - Predict tip after X number of vodka shots
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Polynomial Regression | |
# Number Of Vodka Shots Vs Tip | |
dataset = read.csv('./shots_tip.csv') | |
# Let's fit the data to a Linear Regression Model | |
linear_regressor = lm(formula = Tip ~ NumberOfShots, | |
data = dataset) | |
# Let's fit the data to a Polynomial Regression | |
dataset$NumberOfShots2 = dataset$NumberOfShots^2 | |
dataset$NumberOfShots3 = dataset$NumberOfShots^3 | |
dataset$NumberOfShots4 = dataset$NumberOfShots^4 | |
dataset$NumberOfShots5 = dataset$NumberOfShots^5 | |
polynomial_regressor = lm(formula = Tip ~ ., | |
data = dataset) | |
# Visualize | |
# install plot package, this will help us | |
# visualize the data | |
install.packages('ggplot2') | |
library(ggplot2) | |
# Linear | |
ggplot() + | |
geom_point(aes(x = dataset$NumberOfShots, y = dataset$Tip), | |
colour = 'red') + | |
geom_line(aes(x = dataset$NumberOfShots, y = predict(linear_regressor, newdata = dataset)), | |
colour = 'blue') + | |
ggtitle('More shots Bigger tip?') + | |
xlab('Number of shots') + | |
ylab('Tip') | |
# Polynomial Visualization | |
ggplot() + | |
geom_point(aes(x = dataset$NumberOfShots, y = dataset$Tip), | |
colour = 'red') + | |
geom_line(aes(x = dataset$NumberOfShots, y = predict(polynomial_regressor, newdata = dataset)), | |
colour = 'blue') + | |
ggtitle('[Polynomial Regression] More shots Bigger tip?') + | |
xlab('Number of shots') + | |
ylab('Tip') | |
# Let's validate the model | |
# predict tip accoriding the linear model | |
# What will be the tip after 7.5 shots of Vodka = 37.9 | |
tip_predict_linear = predict(linear_regressor, | |
data.frame(NumberOfShots = 7.5)) | |
# predict tip accoriding the polynomial model | |
# What will be the tip after 7.5 shots of Vodka = 20.3 | |
tip_predict_polynomial = predict(polynomial_regressor, | |
data.frame(NumberOfShots = 7.5, | |
NumberOfShots2 = 7.5^2, | |
NumberOfShots3 = 7.5^3, | |
NumberOfShots4 = 7.5^4, | |
NumberOfShots5 = 7.5^5)) | |
# For Summary | |
# Our Polynomial model is by far more accurate than our linear model |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment