2

I want to make a plot with days in x axis from the forecast process. I made an example using the guides from here: https://stackoverflow.com/a/10347205/2366057

The example from the link:

 Lines <- "Date        Used
 2011-11-1/00:00:00   587
 2011-11-2/01:00:00   578
 2011-11-3/02:00:00   600
 2011-11-4/03:00:00   599
 2011-11-5/04:00:00   678
 2011-11-6/05:00:00   555
 2011-11-7/06:00:00   650"

 dm <- read.table(text = Lines, header = TRUE)
 x = dm
require(lubridate)
library(forecast)
 y = ts(x$Used, start=c(2011, yday("2011-11-01")), frequency=365)
 fcast = forecast(ets(y), 10)
 plot(fcast, xaxt="n")
  a3 = strptime(x$Date, "%Y-%m-%d/%H:%M:%S")
 axis(1, at = decimal_date(a3), labels = format(a3, "%Y-%b-%d %H:%M:%S"), cex.axis=0.3, las=2)

My data:

"day","price"
"2010-02-12 00:00:00",12
"2010-02-12 01:00:00",14
"2010-02-12 02:00:00",15
"2010-02-12 03:00:00",14
"2010-02-12 04:00:00",13
"2010-02-12 05:00:00",16

I have my data into a csv file and as the above:

 df = read.csv(filepath, header=TRUE, sep=",")

 require(lubridate)
 library(forecast)
 y = ts(df$price)
 fcast = forecast(ets(y), 10)

 plot(fcast, xaxt="n")
 a3 = strptime(df$day, "%Y-%m-%d %H:%M:%S")
 axis(1, at = decimal_date(a3), labels = format(a3, "%Y-%b-%d %H:%M:%S"), cex.axis=0.6, las=2)

In the second snippet in the x-axis the days doesn't appear. What is it wrong?

Thank you in advance.

Community
  • 1
  • 1

1 Answers1

2

In your first snippet, note this line:

y = ts(x$Used, start=c(2011, yday("2011-11-01")), frequency=365)

A time series is created with the dates of the actual observations. Decimal representations of these dates are stored in y and used to plot the time series, and when you later plot the axis and pass in decimal_date(a3), things match up.

unclass(y)
# [1] 587 578 600 599 678 555 650
# attr(,"tsp")
# [1] 2011.833 2011.849  365.000
decimal_date(a3)
# [1] 2011.833 2011.836 2011.838 2011.841 2011.844 2011.847 2011.850

See how the numbers are similar?

But you don't include these dates in your second snippet:

y = ts(df$price)

So the observations are just plotted at 1, 2, 3, 4, 5, and 6. But you again pass in decimal_date(a3)--values way off-screen.

unclass(y)
# [1] 12 14 15 14 13 16
# attr(,"tsp")
# [1] 1 6 1
decimal_date(a3)
# [1] 2010.115 2010.115 2010.115 2010.115 2010.116 2010.116

But notice something else: many of these decimal_date values are the same. That's because your observations in the second snippet differ by hours, not days. This is not the function you want.

One workaround is just to stick with the 1-6 numbering when creating your axis:

axis(1, at = seq_along(a3), labels = format(a3, "%Y-%b-%d %H:%M:%S"), cex.axis=0.6, las=2)

Another method is to convert the dates to seconds, both when you create the time series and when you draw the labels:

df$day <- as.POSIXlt(df$day)

y = zoo(df$price, df$day)

axis(1, at = as.numeric(df$day), labels = format(a3, "%Y-%b-%d %H:%M:%S"), cex.axis=0.6, las=2)

(At this point, you should probably not name the variable day though.)

Peyton
  • 7,266
  • 2
  • 29
  • 29