Skip to content

Instantly share code, notes, and snippets.

@RamiKrispin
Created May 19, 2021 21:16
Show Gist options
  • Select an option

  • Save RamiKrispin/93d5628fbb4e3996fa14dc160ebb4525 to your computer and use it in GitHub Desktop.

Select an option

Save RamiKrispin/93d5628fbb4e3996fa14dc160ebb4525 to your computer and use it in GitHub Desktop.
Example of plotting multiple forecasts in a single plot
library(cdcfluview)
national_ili <- ilinet("national", years = c(1997:2017))
mydata.ts <- ts(data = national_ili$total_patients, end = c(2018,39), frequency= 52)
plot(mydata.ts,type="o",col="blue")
plot(forecast(mydata.ts, h = 65))
library(coronavirus)
library(dplyr)
library(forecast)
library(ggplot2)
coronavirus %>% filter(!is.na(a))
set_data <- function(country_name) {
DT <-coronavirus %>% filter(type == "confirmed") %>% group_by(country) %>%
filter(country==country_name)
vector_time_sries <- ts(data = DT$cases,start = c(2020, 01),frequency = 365.5)
return(forecast(vector_time_sries, 92))
}
country_name <- "US"
DT <-coronavirus %>% filter(type == "confirmed") %>% group_by(country) %>%
filter(country=="US")
vector_time_sries <- ts(data = DT$cases,start = c(2020, 01),frequency = 365.5)
#fit <- HoltWinters(vector_time_sries, beta=FALSE, gamma=FALSE)
forecast(vector_time_sries, 3)
plot(forecast(vector_time_sries, 3))
DT_US <- set_data("US")
DT_Brazil <- set_data("Brazil")
DT_India <- set_data("India")
DT_Israel <- set_data("Israel")
DT_Russia <- set_data("Russia")
#DT_Canada <- set_data("Canada")
plot(DT_US)
lines(DT_Brazil)
plot(unlist(DT_US),unlist(DT_Brazil))
library(TSstudio)
library(plotly)
p1 <- plot_forecast(DT_US)
p2 <- plot_forecast(DT_Brazil)
p3 <- plot_forecast(DT_India)
p4 <- plot_forecast(DT_Israel)
p5 <- plot_forecast(DT_Russia)
subplot(p1, p2, p3, p4, p5,
nrows = 2,
shareY = TRUE,
shareX = TRUE) %>%
hide_legend()
main_color <- "#457b9d"
us_color <- "pink"
india_color <- "purple"
brazil_color <- "green"
israel_color <- "#457b9d"
russia_color <- "orange"
upper_color <- "rgba(202, 240, 248, 0.25)"
lower_color <- "rgba(173, 232, 244, 0.25)"
plot_ly() %>%
add_lines(x = time(DT_US$x),
y = as.numeric(DT_US$x),
line = list(color = us_color),
name = "US",
legendgroup = "us",
showlegend = TRUE) %>%
add_ribbons(x = time(DT_US$lower),
ymin = as.numeric(DT_US$lower[,2]),
ymax = as.numeric(DT_US$upper[,2]),
line = list(color = upper_color),
fillcolor = upper_color,
name = "95% PI",
legendgroup = "us",
showlegend = FALSE) %>%
add_ribbons(x = time(DT_US$lower),
ymin = as.numeric(DT_US$lower[,1]),
ymax = as.numeric(DT_US$upper[,1]),
line = list(color = lower_color),
fillcolor = lower_color,
name = "80% PI",
legendgroup = "us",
showlegend = FALSE) %>%
add_lines(x = time(DT_US$mean),
y = as.numeric(DT_US$mean),
line = list(color = us_color, dash = "dash"),
name = "Forecast",
legendgroup = "us",
showlegend = FALSE) %>%
add_lines(x = time(DT_Brazil$x),
y = as.numeric(DT_Brazil$x),
line = list(color = brazil_color),
name = "Brazil",
legendgroup = "brazil",
showlegend = TRUE) %>%
add_ribbons(x = time(DT_Brazil$lower),
ymin = as.numeric(DT_Brazil$lower[,2]),
ymax = as.numeric(DT_Brazil$upper[,2]),
line = list(color = upper_color),
fillcolor = upper_color,
name = "95% PI",
legendgroup = "brazil",
showlegend = FALSE) %>%
add_ribbons(x = time(DT_Brazil$lower),
ymin = as.numeric(DT_Brazil$lower[,1]),
ymax = as.numeric(DT_Brazil$upper[,1]),
line = list(color = lower_color),
fillcolor = lower_color,
name = "80% PI",
legendgroup = "brazil",
showlegend = FALSE) %>%
add_lines(x = time(DT_Brazil$mean),
y = as.numeric(DT_Brazil$mean),
line = list(color = brazil_color, dash = "dash"),
name = "Forecast",
legendgroup = "brazil",
showlegend = FALSE) %>%
add_lines(x = time(DT_India$x),
y = as.numeric(DT_India$x),
line = list(color = india_color),
name = "India",
legendgroup = "india",
showlegend = TRUE) %>%
add_ribbons(x = time(DT_India$lower),
ymin = as.numeric(DT_India$lower[,2]),
ymax = as.numeric(DT_India$upper[,2]),
line = list(color = upper_color),
fillcolor = upper_color,
name = "95% PI",
legendgroup = "india",
showlegend = FALSE) %>%
add_ribbons(x = time(DT_India$lower),
ymin = as.numeric(DT_India$lower[,1]),
ymax = as.numeric(DT_India$upper[,1]),
line = list(color = lower_color),
fillcolor = lower_color,
name = "80% PI",
legendgroup = "india",
showlegend = FALSE) %>%
add_lines(x = time(DT_India$mean),
y = as.numeric(DT_India$mean),
line = list(color = india_color, dash = "dash"),
name = "Forecast",
legendgroup = "india",
showlegend = FALSE) %>%
add_lines(x = time(DT_Israel$x),
y = as.numeric(DT_Israel$x),
line = list(color = israel_color),
name = "Israel",
legendgroup = "israel",
showlegend = TRUE) %>%
add_ribbons(x = time(DT_Israel$lower),
ymin = as.numeric(DT_Israel$lower[,2]),
ymax = as.numeric(DT_Israel$upper[,2]),
line = list(color = upper_color),
fillcolor = upper_color,
name = "95% PI",
legendgroup = "israel",
showlegend = FALSE) %>%
add_ribbons(x = time(DT_Israel$lower),
ymin = as.numeric(DT_Israel$lower[,1]),
ymax = as.numeric(DT_Israel$upper[,1]),
line = list(color = lower_color),
fillcolor = lower_color,
name = "80% PI",
legendgroup = "israel",
showlegend = FALSE) %>%
add_lines(x = time(DT_Israel$mean),
y = as.numeric(DT_Israel$mean),
line = list(color = israel_color, dash = "dash"),
name = "Forecast",
legendgroup = "israel",
showlegend = FALSE) %>%
add_lines(x = time(DT_Russia$x),
y = as.numeric(DT_Russia$x),
line = list(color = russia_color),
name = "Russia",
legendgroup = "russia",
showlegend = TRUE) %>%
add_ribbons(x = time(DT_Russia$lower),
ymin = as.numeric(DT_Russia$lower[,2]),
ymax = as.numeric(DT_Russia$upper[,2]),
line = list(color = upper_color),
fillcolor = upper_color,
name = "95% PI",
legendgroup = "russia",
showlegend = FALSE) %>%
add_ribbons(x = time(DT_Russia$lower),
ymin = as.numeric(DT_Russia$lower[,1]),
ymax = as.numeric(DT_Russia$upper[,1]),
line = list(color = lower_color),
fillcolor = lower_color,
name = "80% PI",
legendgroup = "russia",
showlegend = FALSE) %>%
add_lines(x = time(DT_Russia$mean),
y = as.numeric(DT_Russia$mean),
line = list(color = russia_color, dash = "dash"),
name = "Forecast",
legendgroup = "russia",
showlegend = FALSE) %>%
layout(title = "Your Title...",
yaxis = list(title = "Number of Cases"),
margin = list(t = 60))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment