【问题标题】:plot acf and time series in the same plot在同一图中绘制 acf 和时间序列
【发布时间】:2018-11-19 09:24:33
【问题描述】:

我有一个 24 小时频率的时间序列。我已经提取了 1 天的数据,并分别绘制了 ts 和 acf 的结果。

结果如下:

24小时系列

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

acf 24 小时系列赛

我在想将时间序列和 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()应用到我的系列窗口:

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(或 -1),但数据中的值看起来介于 0 到大约 600 之间。
  • 你能把dput(trainingPeriod.1Day.ts)的输出贴在问题里吗?
  • 谢谢布伦丹。我尝试绘制 100*acf.values ,但仍然 lines() 什么也不打印。

标签: r plot advanced-custom-fields lines


【解决方案1】:

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

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)

【讨论】:

  • @Elena,如果答案正确,请接受并投票。我很乐意提供帮助。
猜你喜欢
  • 2014-07-20
  • 2017-10-08
  • 2021-09-25
  • 2021-02-09
  • 2021-12-17
  • 2021-12-04
  • 2019-12-02
  • 1970-01-01
  • 2013-11-24
相关资源
最近更新 更多