I am trying to do Holt's forecast for multiple timeseries and combine them with my original data.frame. Consider the following data.frame, where I have two population groups:
library("forecast")
d <- data.frame(SEX = c("MALE","MALE","MALE","FEMALE","FEMALE","FEMALE"),
EDUCATION = c("01","01","01","01","01","01"),
TIME = c("2000","2001","2002","2000","2001","2002"),
VALUE = c(120,150,140,90,75,60))
Then I am doing the Holt's forecast for the two time series:
male <- ts(as.numeric(d[1:3,]$VALUE),start=c(2000))
female <- ts(as.numeric(d[4:6,]$VALUE),start=c(2000))
forecastmale <- holt(male,h = 3,damped = FALSE)
forecastfemale <- holt(female,h = 3,damped = FALSE)
Then I save the result and combine with my original data.frame:
forecastmale <- data.frame(forecastmale[["mean"]])
forecastfemale <- data.frame(forecastfemale[["mean"]])
forecastmale$SEX <- c("MALE","MALE","MALE")
forecastmale$EDUCATION <- c("01","01","01")
forecastmale$TIME <- c("2003","2004","2005")
colnames(forecastmale)[1] <- "VALUE"
forecastmale <- forecastmale[, c(2,3,4,1)]
forecastfemale$SEX <- c("FEMALE","FEMALE","FEMALE")
forecastfemale$EDUCATION <- c("01","01","01")
forecastfemale$TIME <- c("2003","2004","2005")
colnames(forecastfemale)[1] <- "VALUE"
forecastfemale <- forecastfemale[, c(2,3,4,1)]
d <- rbind(d,forecastmale,forecastfemale)
This works when I only have two time series. But if I have like 100 time series that has to be forecasted, then it is not a very efficient way do to it. Can anyone help with make the coder more efficient, so if I for instance include an extra population group in my data.frame, then I do not have change anything in the code?
This is what the
fable
package is designed to handle. Here is an example using the same data structure that you have.Created on 2020-09-05 by the reprex package (v0.3.0)