在同一图中绘制acf和时间序列

时间:2018-06-09 14:50:47

标签: r plot advanced-custom-fields lines

我有24小时频率的时间序列。我已经提取了1整天的数据,并且我已经单独绘制了ts和acf的结果。

结果如下:

24小时的时间系列赛 enter image description here

然后我执行了acf()并绘制结果:

24小时后的意甲 enter image description here

我在想,将时间系列和acf()结果放在同一个图中是有用的,只是为了理解acf()的结果。我没有看到任何一个例子,所以也许根本没用,但事实是我不明白为什么这不起作用

这是我的代码:

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)

acf()应用到我的系列窗口:<​​/ p>

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

准备数据以添加acf()信息并使用lines()功能:

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')

添加最后一个命令行()时,没有任何内容被绘制,我在控制台窗口中没有任何错误。 你知道它可能会发生什么吗?

这是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 个答案:

答案 0 :(得分:0)

问题在于第一个图的水平轴与时间序列有关,而不是与小时序列(0到23)有关。如果将acf值乘以固定垂直比例(由Brendan A.提及),并且对x轴使用相同的时间段,则应该得到一个图。这是我的代码来生成以下图表。

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)
相关问题