Guest User

Untitled

a guest
May 21st, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. # Polynomial Regression
  2.  
  3. rm(list = ls())
  4.  
  5. # Importing the dataset
  6. dataset <- read.csv(file.path(getwd(),'Data/Position_Salaries.csv'))
  7. dataset <- dataset[2:3]
  8.  
  9. plot(x = dataset$Level, y = dataset$Salary) # ploting data on the fly ;)
  10.  
  11. # no need to split data because the dataset is small
  12. # no need for feature scaling with regression
  13.  
  14. # Fitting Linear Regression to the dataset
  15. # instead of using "." we can just use the name of the column since it is only one column
  16. lin_reg <- lm(formula = Salary ~ .,
  17. data = dataset)
  18.  
  19.  
  20. # Fitting Polynomial Regression to the dataset
  21. dataset$Level2 <- dataset$Level^2
  22. dataset$Level3 <- dataset$Level^3
  23. dataset$Level4 <- dataset$Level^4
  24. poly_reg <- lm(formula = Salary ~ ., data = dataset)
  25.  
  26. # However, i found an easier way to do polynomial regression
  27. # the keyword "poly" help us decide the level of polynomial
  28. # in the previous code we are doing it manually , stupid!
  29. poly_reg <- lm(formula = Salary ~ poly (Level, 4), data = dataset)
  30.  
  31.  
  32. # the above code is using orthogonal poly regression
  33. # however, if we dont want that, we can use:
  34. poly_reg <- lm(formula = Salary ~ poly (Level, 4, raw = TRUE), data = dataset)
  35.  
  36. # I added this part
  37. # Plot of fitted vs residuals.
  38. # No clear pattern should show in the residual plot if the model is a good fit
  39.  
  40. # for the simple linear regression
  41. plot(fitted(lin_reg), residuals(lin_reg))
  42.  
  43. # for the polynomial linear regression
  44. plot(fitted(poly_reg), residuals(poly_reg))
  45.  
  46. # Visualising the Linear Regression results
  47. if (!require("ggplot2"))
  48. install.packages("ggplot2") # to install the package if you don't have it
  49. ggplot() +
  50. geom_point(aes(x = dataset$Level, y = dataset$Salary),
  51. colour = 'red') +
  52. geom_line(aes(x = dataset$Level, y = predict(lin_reg, newdata = dataset)),
  53. colour = 'blue') +
  54. ggtitle('Truth or Bluff (Linear Regression)') +
  55. xlab('Level') +
  56. ylab('Salary')
  57.  
  58. # Visualising the Polynomial Regression results
  59. # install.packages('ggplot2')
  60. library(ggplot2)
  61. ggplot() +
  62. geom_point(aes(x = dataset$Level, y = dataset$Salary),
  63. colour = 'red') +
  64. geom_line(aes(x = dataset$Level, y = predict(poly_reg, newdata = dataset)),
  65. colour = 'blue') +
  66. ggtitle('Truth or Bluff (Polynomial Regression)') +
  67. xlab('Level') +
  68. ylab('Salary') +
  69. theme_bw() +
  70. theme(plot.title = element_text(hjust = 0.5))
  71.  
  72. # Visualising the Regression Model results (for higher resolution and smoother curve)
  73. # install.packages('ggplot2')
  74. library(ggplot2)
  75. x_grid = seq(min(dataset$Level), max(dataset$Level), 0.1)
  76. ggplot() +
  77. geom_point(aes(x = dataset$Level, y = dataset$Salary),
  78. colour = 'red') +
  79. geom_line(aes(x = x_grid, y = predict(poly_reg,
  80. newdata = data.frame(Level = x_grid,
  81. Level2 = x_grid^2,
  82. Level3 = x_grid^3,
  83. Level4 = x_grid^4))),
  84. colour = 'blue') +
  85. ggtitle('Truth or Bluff (Polynomial Regression)') +
  86. xlab('Level') +
  87. ylab('Salary') +
  88. theme_bw() +
  89. theme(plot.title = element_text(hjust = 0.5))
  90.  
  91. # Predicting a new result with Linear Regression
  92. predict(lin_reg, data.frame(Level = 6.5))
  93.  
  94. # Predicting a new result with Polynomial Regression
  95. predict(poly_reg, data.frame(Level = 6.5,
  96. Level2 = 6.5^2,
  97. Level3 = 6.5^3,
  98. Level4 = 6.5^4))
Add Comment
Please, Sign In to add comment