[R] How to get the date generated by the forecast function as another field in a data frame

Paul Bernal paulbernal07 at gmail.com
Wed Mar 15 16:50:19 CET 2017


Dear all,

I am currently using R for windows Version 3.3.3 (I will provide the
sessionInfo() output below)

> library("Rcmdr")
Loading required package: splines
Loading required package: RcmdrMisc
Loading required package: car
Loading required package: sandwich

Rcmdr Version 2.3-2

> library(forecast)
>
> library(tseries)

    'tseries' version: 0.10-35

    'tseries' is a package for time series analysis and computational
    finance.

    See 'library(help="tseries")' for details.

> library(stats)
>
> library(stats4)
>
> Data<-read.csv("ContainerDataNEW.csv")
>
> TSData<-ts(Data[,1], start=c(1985,10), frequency=12)
>
> TSeriesModel1<-ets(TSData)
>
> TSSeriesModel1Forecast<-forecast(TSeriesModel1,h=24)

Now the output from forecasts is the following:

                Point Forecast    Lo 80    Hi 80       Lo 95           Hi 95
Apr 2017       67.62845 30.73747 104.5194 11.20856 124.0483
May 2017       67.62845 30.73747 104.5194 11.20856 124.0483
Jun 2017       67.62845 30.73747 104.5194 11.20856 124.0483
Jul 2017       67.62845 30.73747 104.5194 11.20856 124.0483
Aug 2017       67.62845 30.73747 104.5194 11.20856 124.0483
Sep 2017       67.62845 30.73747 104.5194 11.20856 124.0483
Oct 2017       67.62845 30.73747 104.5194 11.20856 124.0483
Nov 2017       67.62845 30.73747 104.5194 11.20856 124.0483
Dec 2017       67.62845 30.73747 104.5194 11.20856 124.0483
Jan 2018       67.62845 30.73747 104.5194 11.20856 124.0483
Feb 2018       67.62845 30.73747 104.5194 11.20856 124.0483
Mar 2018       67.62845 30.73747 104.5194 11.20856 124.0483
Apr 2018       67.62845 30.73747 104.5194 11.20856 124.0483
May 2018       67.62845 30.73747 104.5194 11.20856 124.0483
Jun 2018       67.62845 30.73747 104.5194 11.20856 124.0483
Jul 2018       67.62845 30.73747 104.5194 11.20856 124.0484
Aug 2018       67.62845 30.73747 104.5194 11.20856 124.0484
Sep 2018       67.62845 30.73747 104.5194 11.20856 124.0484
Oct 2018       67.62845 30.73747 104.5194 11.20856 124.0484
Nov 2018       67.62845 30.73747 104.5194 11.20856 124.0484
Dec 2018       67.62845 30.73747 104.5194 11.20856 124.0484
Jan 2019       67.62845 30.73747 104.5194 11.20856 124.0484
Feb 2019       67.62845 30.73747 104.5194 11.20856 124.0484
Mar 2019       67.62845 30.73747 104.5194 11.20856 124.0484

However, as you can see, the first "column" contains the dates for the
forecasts, but it appears as a field with no name.

What I would like to do is to get those dates, add them as an additional
column to the forecasts so that when I use the data.frame(Forecasts) I can
have a result in this fashion:

Date              Point Forecast    Lo 80         Hi 80       Lo 95
  Hi 95
Apr 2017       67.62845           30.73747 104.5194 11.20856     124.0483

Is there a way to do this?

Here is the sessionInfo() output:

> sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8.1 x64 (build 9600)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United
States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C
                  LC_TIME=English_United States.1252

attached base packages:
[1] stats4    splines   stats     graphics  grDevices utils     datasets
 methods   base

other attached packages:
[1] tseries_0.10-35 forecast_8.0    Rcmdr_2.3-2     RcmdrMisc_1.0-5
sandwich_2.3-4  car_2.1-3

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.7         lattice_0.20-34     tcltk2_1.2-11
class_7.3-14        zoo_1.7-13          lmtest_0.9-35       relimp_1.0-5
     assertthat_0.1      digest_0.6.11       R6_2.2.0            plyr_1.8.4

[12] acepack_1.4.1       MatrixModels_0.4-1  e1071_1.6-7         httr_1.2.1
         ggplot2_2.2.0       lazyeval_0.2.0      AzureML_0.2.13
 curl_2.2            uuid_0.1-2          readxl_0.1.1        minqa_1.2.4

[23] data.table_1.10.4   SparseM_1.74        fracdiff_1.4-2
 nloptr_1.0.4        rpart_4.1-10        Matrix_1.2-8        lme4_1.1-12
      stringr_1.1.0       foreign_0.8-67      munsell_0.4.3
base64enc_0.1-3
[34] mgcv_1.8-17         htmltools_0.3.5     tcltk_3.3.3
nnet_7.3-12         tibble_1.2          gridExtra_2.2.1     htmlTable_1.7
    quadprog_1.5-5      Hmisc_4.0-2         codetools_0.2-15
 XML_3.98-1.4
[45] MASS_7.3-45         grid_3.3.3          nlme_3.1-131
 jsonlite_1.1        gtable_0.2.0        magrittr_1.5        scales_0.4.1
     stringi_1.1.2       timeDate_3012.100   latticeExtra_0.6-28
Formula_1.2-1
[56] RColorBrewer_1.1-2  tools_3.3.3         abind_1.4-5
parallel_3.3.3      pbkrtest_0.4-6      survival_2.40-1
colorspace_1.3-0    cluster_2.0.5       miniCRAN_0.2.7      knitr_1.15
     quantreg_5.29
>

I have also attached the file that I used to train the model and generate
forecasts.

Any help will be greatly appreciated,

Best regards,

Paul


More information about the R-help mailing list