Guest User

Untitled

a guest
Mar 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. ipi.df <- read_csv('data/ipi.csv')
  2.  
  3. #----------------------------------------------------------------------------
  4. # Exercise 1. Transform ipi.df from:
  5. #
  6. # date ipi
  7. # <chr> <dbl>
  8. # 2017M11 104
  9. # to:
  10. #
  11. # date ipi
  12. # <date> <dbl>
  13. # 2017-11-01 104
  14. #----------------------------------------------------------------------------
  15.  
  16. # Sol:
  17. ipi.df %<>%
  18. separate(date, into = c("year", "month"), sep = "M") %>%
  19. mutate(date = as.Date(paste(year, month, 01, sep = "-"))) %>%
  20. select(date, ipi)
  21.  
  22. #----------------------------------------------------------------------------
  23. # Exercise 2. Plot the data as a time series using ggplot2
  24. #----------------------------------------------------------------------------
  25.  
  26. ipi.df %>%
  27. ggplot(aes(x = date, y = ipi)) +
  28. geom_line(size = 1, color = palette_light()[[1]]) +
  29. geom_smooth(method = "loess") +
  30. labs(title = "Indice De Produccion Industrial De Cantabria", x = "", y = "IPI") +
  31. scale_y_continuous() +
  32. scale_x_date(date_breaks = "1 year", date_labels = "%Y") +
  33. theme_tq()
  34.  
  35. #----------------------------------------------------------------------------
  36. # Exercise 3. Using ggplot, plot ipi.df IPI series with the the
  37. # short_term_mean and the long_term_mean
  38. #----------------------------------------------------------------------------
  39.  
  40. ipi.df %>%
  41. mutate(short_mean = short_term_mean(ipi),
  42. long_term_mean = long_term_mean(ipi)) %>%
  43. gather(key = "indice", value ='val', -date) %>%
  44. ggplot(aes(x = date, y = val, color=indice)) +
  45. geom_line(size = .5, color = palette_light()[[1]]) +
  46. geom_smooth(method = "loess") +
  47. labs(title = "Indice De Produccion Industrial De Cantabria", x = "", y = "IPI") +
  48. scale_y_continuous() +
  49. scale_x_date(date_breaks = "1 year", date_labels = "%Y") +
  50. theme_tq()
  51.  
  52. #----------------------------------------------------------------------------
  53. # Exercise 4. Fit a Holt-Winters leaving all parameter in blank.
  54. # Get the proposed α, β and γ.
  55. # Check the final sum of squared errors achieved in optimizing
  56. #----------------------------------------------------------------------------
  57. fit_hw <- HoltWinters(ipi_train)
  58. round(fit_hw$alpha, 2)
  59. round(fit_hw$beta, 2)
  60. round(fit_hw$gamma, 2)
  61. plot(fit_hw)
  62. fit_hw$SSE
Add Comment
Please, Sign In to add comment