plot acf and time series in the same plot

557 Views Asked by At

I've a time series with 24h frequency. I've extracted 1 full day of the data and I've plotted individually the ts and the result of the acf.

Here is the results:

24h time serie enter image description here

then I've executed acf() and plot the results:

acf on 24h time serie enter image description here

I was thinking that It could be useful to pot the time serie and the acf() result in the same plot, just to the purpose of understanding the result of acf(). I've not seen any example, and so maybe it is not useful at all, but the fact is that I not understand why this is not working

here is my code:

plot(trainingPeriod.1Day.ts, xaxt='n', col='blue', ylim=c(-100, 700))
tt <- time(trainingPeriod.1Day.ts)
ix <- seq(0, length(tt) - 1, by=1)
axis(side = 1, at = tt[ix], labels = FALSE, xlab='Hour of the day')
labs <- hour(date_decimal(index(trainingPeriod.1Day.ts)))
axis(side = 1, at = tt[ix], labels = labs[ix], tcl = -0.7, cex.axis = 0.7)

Apply acf() to the window of my series:

acf.24h <- acf(trainingPeriod.1Month.ts, lag.max = 24, plot = FALSE)

Prepare data to add acf() information and use lines() function:

acf.values <-acf.24h$acf[-1]
acf.sequence <- length(acf.values)
lines(seq(from=0 , by = 1, length.out = acf.sequence), acf.values, type='h')

When adding the last command lines(), nothing gets plotted and I do not have any error in the console window. Do you have an idea what it may be going on?

Here is the output of dput()

> dput(trainingPeriod.1Day.ts)
structure(c(19L, 10L, 32L, 24L, 65L, 279L, 437L, 543L, 293L, 
188L, 280L, 252L, 209L, 181L, 203L, 214L, 264L, 229L, 148L, 108L, 
55L, 72L, 47L, 32L), .Tsp = c(2018.08767123288, 2018.09029680365, 
8760), class = "ts")
1

There are 1 best solutions below

1
Calvin On

The problem is that the horizontal axis for the first plot is related to the time series, not the sequence of hours (0 to 23). If you multiply the acf values to fix the vertical scale (mentioned by Brendan A.) and use the same time periods for the x axis, you should get the plot you one. Here is my code to generate the following plot.

enter image description here

trainingPeriod.1Day.ts <- structure(c(19L, 10L, 32L, 24L, 65L, 279L, 437L, 543L, 293L, 
            188L, 280L, 252L, 209L, 181L, 203L, 214L, 264L, 229L, 148L, 108L, 
            55L, 72L, 47L, 32L), .Tsp = c(2018.08767123288, 2018.09029680365, 
                                          8760), class = "ts")
par(mar=c(4,4,2,4))
plot(trainingPeriod.1Day.ts
     ,xaxt='n'
     ,col='blue'
     ,ylim=c(-100, 700)
     )
tt <- time(trainingPeriod.1Day.ts)
ix <- seq(0, length(tt) - 1, by=1)
axis(side = 1, at = tt[ix], labels = FALSE, xlab='Hour of the day')
labs <- hour(date_decimal(index(trainingPeriod.1Day.ts)))
axis(side = 1
     , at = tt
     ,labels = ix
     ,tcl = -0.7
     ,cex.axis = 0.7)

acf.24h <- acf(trainingPeriod.1Day.ts
               ,lag.max = 24
               ,plot = FALSE)

acf.values <-acf.24h$acf[-1]
acf.sequence <- length(acf.values)
lines(tt[1+seq(1,acf.sequence,1)]
      ,acf.values*500
      ,type='h'
      ,col='red'
      )
axis(side=4
     ,at=500*seq(-0.2,1,0.2)
     ,labels=seq(-0.2,1,0.2)
     ,main='Correlation'
     )
mtext("Correlation", side = 4, line = 3)