2

I am trying to get an output from my forecast with readable dates. The dates have no column right now and are in year.decimal format.

My output looks like this:

       Point Forecast    Lo 80     Hi 80    Lo 95     Hi 95
2017.154             33 24.42807  41.57193 19.89037  46.10963
2017.173             31 22.42807  39.57193 17.89037  44.10963
2017.192             32 23.42807  40.57193 18.89037  45.10963
2017.212             30 21.42807  38.57193 16.89037  43.10963
2017.231             39 30.42807  47.57193 25.89037  52.10963

I want it to look like this:

Dates     Point Forecast  Lo 80     Hi 80    Lo 95      Hi 95
2017-02-26           33 24.42807  41.57193 19.89037  46.10963
2017-03-05           31 22.42807  39.57193 17.89037  44.10963
2017-03-12           32 23.42807  40.57193 18.89037  45.10963
2017-03-19           30 21.42807  38.57193 16.89037  43.10963
2017-03-26           39 30.42807  47.57193 25.89037  52.10963

How can I get the dates in a row for itself, so I can change the dates decimals to normal dates?

Sotos
  • 51,121
  • 6
  • 32
  • 66

2 Answers2

2

Try this, using lubridate package:

library(lubridate)
 date<-format(date_decimal(as.numeric(row.names(as.data.frame(forecasting_output)))),"%Y-%m-%d")

date
    [1] "2017-08-23" "2017-08-25" "2017-08-25"

After that, you can easily bind this two information:

cbind(date,as.data.frame(forecasting_output))
                date Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
2017.6438 2017-08-23       722.5832 405.3363 1039.830 237.3959 1207.770
2017.6466 2017-08-25       953.0771 635.8292 1270.325 467.8884 1438.266
2017.6493 2017-08-25      1063.1212 745.8714 1380.371 577.9295 1548.313
Terru_theTerror
  • 4,918
  • 2
  • 20
  • 39
0

It appears that the value 2017.154 corresponds to the year 2017 plus a fraction 0.154 of a year. We can try converting the Dates column to actual dates using the following formula:

df$NewDates <- as.Date(paste0(floor(df$Dates), "-01-01")) +
    365.25*(df$Dates - floor(df$Dates))

[1] "2017-02-26" "2017-03-05" "2017-03-12" "2017-03-19" "2017-03-26"

Demo

Note: I may have missed the edge case of dealing with leap years. For a leap year, a year has 366, not 365, days. So you might need to add logic which detects this to what I wrote above.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360