Created
November 14, 2025 10:03
-
-
Save mitchelloharawild/54bc31f7c9aaad2bc9c4ecb513e0c164 to your computer and use it in GitHub Desktop.
Comparing ETS and ARIMA forecasts of cyclical patterns in annual sunspots
This file contains hidden or 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
| library(tsibble) | |
| library(fable) | |
| library(ggplot2) | |
| # Sunspot data from https://www.sidc.be/SILSO/datafiles | |
| sunspots <- tsibble( | |
| Year = seq(1700, 2024, by = 1), | |
| Sunspots = c( | |
| 8.3, 18.3, 26.7, 38.3, 60, 96.7, 48.3, 33.3, 16.7, 13.3, 5, 0, 0, 3.3, 18.3, | |
| 45, 78.3, 105, 100, 65, 46.7, 43.3, 36.7, 18.3, 35, 66.7, 130, 203.3, 171.7, | |
| 121.7, 78.3, 58.3, 18.3, 8.3, 26.7, 56.7, 116.7, 135, 185, 168.3, 121.7, 66.7, | |
| 33.3, 26.7, 8.3, 18.3, 36.7, 66.7, 100, 134.8, 139, 79.5, 79.7, 51.2, 20.3, | |
| 16, 17, 54, 79.3, 90, 104.8, 143.2, 102, 75.2, 60.7, 34.8, 19, 63, 116.3, | |
| 176.8, 168, 136, 110.8, 58, 51, 11.7, 33, 154.2, 257.3, 209.8, 141.3, 113.5, | |
| 64.2, 38, 17, 40.2, 138.2, 220, 218.2, 196.8, 149.8, 111, 100, 78.2, 68.3, | |
| 35.5, 26.7, 10.7, 6.8, 11.3, 24.2, 56.7, 75, 71.8, 79.2, 70.3, 46.8, 16.8, | |
| 13.5, 4.2, 0, 2.3, 8.3, 20.3, 23.2, 59, 76.3, 68.3, 52.9, 38.5, 24.2, 9.2, | |
| 6.3, 2.2, 11.4, 28.2, 59.9, 83, 108.5, 115.2, 117.4, 80.8, 44.3, 13.4, 19.5, | |
| 85.8, 192.7, 227.3, 168.7, 143, 105.5, 63.3, 40.3, 18.1, 25.1, 65.8, 102.7, | |
| 166.3, 208.3, 182.5, 126.3, 122, 102.7, 74.1, 39, 12.7, 8.2, 43.4, 104.4, | |
| 178.3, 182.2, 146.6, 112.1, 83.5, 89.2, 57.8, 30.7, 13.9, 62.8, 123.6, 232, | |
| 185.3, 169.2, 110.1, 74.5, 28.3, 18.9, 20.7, 5.7, 10, 53.7, 90.5, 99, 106.1, | |
| 105.8, 86.3, 42.4, 21.8, 11.2, 10.4, 11.8, 59.5, 121.7, 142, 130, 106.6, 69.4, | |
| 43.8, 44.4, 20.2, 15.7, 4.6, 8.5, 40.8, 70.1, 105.5, 90.1, 102.8, 80.9, 73.2, | |
| 30.9, 9.5, 6, 2.4, 16.1, 79, 95, 173.6, 134.6, 105.7, 62.7, 43.5, 23.7, 9.7, | |
| 27.9, 74, 106.5, 114.7, 129.7, 108.2, 59.4, 35.1, 18.6, 9.2, 14.6, 60.2, | |
| 132.8, 190.6, 182.6, 148, 113, 79.2, 50.8, 27.1, 16.1, 55.3, 154.3, 214.7, | |
| 193, 190.7, 118.9, 98.3, 45, 20.1, 6.6, 54.2, 200.7, 269.3, 261.7, 225.1, 159, | |
| 76.4, 53.4, 39.9, 15, 22, 66.8, 132.9, 150, 149.4, 148, 94.4, 97.6, 54.1, | |
| 49.2, 22.5, 18.4, 39.3, 131, 220.1, 218.9, 198.9, 162.4, 91, 60.5, 20.6, 14.8, | |
| 33.9, 123, 211.1, 191.8, 203.3, 133, 76.1, 44.9, 25.1, 11.6, 28.9, 88.3, | |
| 136.3, 173.9, 170.4, 163.6, 99.3, 65.3, 45.8, 24.7, 12.6, 4.2, 4.8, 24.9, | |
| 80.8, 84.5, 94, 113.3, 69.8, 39.8, 21.7, 7, 3.6, 8.8, 29.6, 83.2, 125.5, | |
| 154.7 | |
| ), | |
| index = Year | |
| ) | |
| cols <- c(ETS = "#2b8cbe", ARIMA = "#f16913") | |
| sunspots |> | |
| model( | |
| ETS = ETS(Sunspots), | |
| ARIMA = ARIMA(Sunspots) | |
| ) |> | |
| forecast(h = 30) |> | |
| autoplot(tail(sunspots, 80), level = 50) + | |
| coord_cartesian(ylim = c(0, 300)) + | |
| guides(fill_ramp = "none") + | |
| scale_fill_manual(values = cols, name = "Model") + | |
| scale_colour_manual(values = cols, name = "Model") + | |
| labs( | |
| title = "Forecasting cyclical data with <span style='color:#2b8cbe'>**ETS**</span> and <span style='color:#f16913'>**ARIMA**</span>", | |
| fill = "Model", colour = "Model", | |
| y = "Number of Sunspots", x = "Year" | |
| ) + | |
| theme_minimal() + | |
| theme( | |
| legend.position = "none", | |
| plot.title.position = "panel", | |
| plot.title = ggtext::element_markdown(size = 14, hjust = 0) | |
| ) | |
| ggsave( | |
| filename = "sunspots-ets-arima-forecast.png", | |
| width = 6, height = 4 | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment