Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #### Load libraries ###
- library(ISLR)
- library(plotly)
- library(dplyr)
- #### Pull in the Wage dataset from the ISLR package ####
- data("Wage")
- ##### EDA ####
- # Prepare a Loess smoother model for the Wage Vs Age plot
- wageAgeLoessModel <- loess(wage~age, data = Wage)
- wageAgeLoessPredict <- predict(wageAgeLoessModel, newdata = seq(from = min(Wage$age), to = max(Wage$age), by = 1))
- wageAgeLoess <- data.frame(
- age = seq(from = 18, to = 80, by = 1),
- wage = wageAgeLoessPredict
- )
- # Make the plot wage vs age with the loess model overlaid
- wageVsAgePlot <- plot_ly(Wage) %>%
- add_trace(x = ~age, y = ~wage, type = "scatter", mode = "markers", name = "Sample",
- marker = list(color = "darkgrey"), showlegend = FALSE) %>%
- add_trace(data = wageAgeLoess, x = ~age, y = ~wageAgeLoessPredict, type = "scatter", mode = "lines", name = "Loess Smoother",
- line = list(
- color = "darkblue",
- width = 4
- ), showlegend = FALSE)
- # Prepare a lm model for the Wage Vs Age plot
- wageYearLmModel <- lm(wage~year, data = Wage)
- wageYearPredictData <- data.frame(year = seq(from = min(Wage$year), to = max(Wage$year), by = 1))
- wageYearPredict <- predict(wageYearLmModel, newdata = wageYearPredictData)
- wageYearLm <- data.frame(
- year = seq(from = min(Wage$year), to = max(Wage$year), by = 1),
- wage = wageYearPredict
- )
- # Make the plot wage vs year with the lm model overlaid
- wageVsYearPlot <- plot_ly(Wage) %>%
- add_trace(x = ~year, y = ~wage, type = "scatter", mode = "markers", name = "Sample",
- marker = list(color = "darkgrey"), showlegend = FALSE) %>%
- add_trace(data = wageYearLm, x = ~year, y = ~wage, type = "scatter", mode = "lines", name = "Linear model",
- line = list(
- color = "darkblue",
- width = 4
- ), showlegend = FALSE)
- # Make the plot wage vs education level
- wageVsEducationPlot <- plot_ly(Wage) %>%
- add_trace(x = ~education, y = ~wage, color = ~education, type = "box", showlegend = FALSE)
- # Join the plots
- s1 <- subplot(wageVsAgePlot, wageVsYearPlot, wageVsEducationPlot)
Add Comment
Please, Sign In to add comment