Skip to content

Instantly share code, notes, and snippets.

@alexhallam
Created January 15, 2019 21:39
Show Gist options
  • Save alexhallam/989c95eb8ab953e7c9da434b15b5489f to your computer and use it in GitHub Desktop.
Save alexhallam/989c95eb8ab953e7c9da434b15b5489f to your computer and use it in GitHub Desktop.
library(tidyverse)
# naive forecast method example
sales <- tibble(g = c(rep("insample", 23), rep("outsample", 12)),
y = c(0,2,0,1,0,10,0,0,0,2,0,6,3,0,0,0,0,0,7,0,0,0,0, 0, 0, 0, 3, 1, 0, 0, 1, 0, 1, 0, 0),
y_hat = ifelse(g == "insample", lag(y), last(y)),
error = abs(y - y_hat))
sales
sales %>%
slice(-1) %>% # to remove NA row
mutate(denominator = mean(abs(y[g == "insample"] - y_hat[g == "insample"]), na.rm = TRUE)) %>%
group_by(g) %>%
summarise(mase = mean( abs( ( y - y_hat ) / denominator) ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment