Reputation: 2508
I have one data frame, which is composed of three columns(AJT,NET and SAT). My intention is to make forecast with each of this three times series with forecast package. For that reason I convert data frame into ts object and make forecasting with snaive function,so I wrote this lines of code:
#CODE
library(forecast)
# Data set
DATA_SET<-data.frame(
AJT=seq(1, 48, by = 2),
NET=seq(1, 24, by = 1),
SAT=seq(1, 94, by = 4)
)
# Making TS object
TS_SALES<-ts(DATA_SET,start=c(2016,1),frequency = 12)
# Making forecasting with Forecast package
SNAIVE_AJT<-snaive(TS_SALES[, 'AJT'],h=5)
SNAIVE_NET<-snaive(TS_SALES[, 'NET'],h=5)
SNAIVE_SAT<-snaive(TS_SALES[, 'SAT'],h=5)
# Union forecast in list
SNAIVE_UNION<-mapply(SNAIVE_AJT, SNAIVE_NET,SNAIVE_SAT, FUN=list, SIMPLIFY=FALSE)
All outputs from snaive function I put into SNAIVE_UNION which contain all results from forecasting. So here most important component is "mean" which contain results of forecatig by months.
SNAIVE_UNION[["mean"]]
#
# # [[1]]
# # Jan Feb Mar Apr May
# # 2018 25 27 29 31 33
# #
# # [[2]]
# # Jan Feb Mar Apr May
# # 2018 13 14 15 16 17
# #
# # [[3]]
# # Jan Feb Mar Apr May
# # 2018 49 53 57 61 65
So here, my intention is to put results form SNAIVE_UNION[["mean"]] ,into data table like table below with some function loop,for or other function
Jan Feb Mar Apr May
------------------------------
AJT 25 27 29 31 33
NET 13 14 15 16 17
SAT 49 53 57 61 65
I am asking this because this time series is only small part of whole series and I would like to automate this code.
Upvotes: 0
Views: 62
Reputation: 24252
You can use:
pred <- do.call(rbind, SNAIVE_UNION[["mean"]])
# [,1] [,2] [,3] [,4] [,5]
# [1,] 25 27 29 31 33
# [2,] 13 14 15 16 17
# [3,] 49 53 57 61 65
Upvotes: 2