Guest User

Untitled

a guest
Jan 18th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. #### Load libraries ###
  2. library(ISLR)
  3. library(plotly)
  4. library(dplyr)
  5.  
  6. #### Pull in the Wage dataset from the ISLR package ####
  7. data("Wage")
  8.  
  9. ##### EDA ####
  10.  
  11. # Prepare a Loess smoother model for the Wage Vs Age plot
  12. wageAgeLoessModel <- loess(wage~age, data = Wage)
  13. wageAgeLoessPredict <- predict(wageAgeLoessModel, newdata = seq(from = min(Wage$age), to = max(Wage$age), by = 1))
  14. wageAgeLoess <- data.frame(
  15. age = seq(from = 18, to = 80, by = 1),
  16. wage = wageAgeLoessPredict
  17. )
  18.  
  19. # Make the plot wage vs age with the loess model overlaid
  20. wageVsAgePlot <- plot_ly(Wage) %>%
  21. add_trace(x = ~age, y = ~wage, type = "scatter", mode = "markers", name = "Sample",
  22. marker = list(color = "darkgrey"), showlegend = FALSE) %>%
  23. add_trace(data = wageAgeLoess, x = ~age, y = ~wageAgeLoessPredict, type = "scatter", mode = "lines", name = "Loess Smoother",
  24. line = list(
  25. color = "darkblue",
  26. width = 4
  27. ), showlegend = FALSE)
  28.  
  29. # Prepare a lm model for the Wage Vs Age plot
  30. wageYearLmModel <- lm(wage~year, data = Wage)
  31. wageYearPredictData <- data.frame(year = seq(from = min(Wage$year), to = max(Wage$year), by = 1))
  32. wageYearPredict <- predict(wageYearLmModel, newdata = wageYearPredictData)
  33. wageYearLm <- data.frame(
  34. year = seq(from = min(Wage$year), to = max(Wage$year), by = 1),
  35. wage = wageYearPredict
  36. )
  37.  
  38. # Make the plot wage vs year with the lm model overlaid
  39. wageVsYearPlot <- plot_ly(Wage) %>%
  40. add_trace(x = ~year, y = ~wage, type = "scatter", mode = "markers", name = "Sample",
  41. marker = list(color = "darkgrey"), showlegend = FALSE) %>%
  42. add_trace(data = wageYearLm, x = ~year, y = ~wage, type = "scatter", mode = "lines", name = "Linear model",
  43. line = list(
  44. color = "darkblue",
  45. width = 4
  46. ), showlegend = FALSE)
  47.  
  48. # Make the plot wage vs education level
  49. wageVsEducationPlot <- plot_ly(Wage) %>%
  50. add_trace(x = ~education, y = ~wage, color = ~education, type = "box", showlegend = FALSE)
  51.  
  52.  
  53.  
  54.  
  55. # Join the plots
  56. s1 <- subplot(wageVsAgePlot, wageVsYearPlot, wageVsEducationPlot)
Add Comment
Please, Sign In to add comment