简体   繁体   English

使用ggplot2的ACF图的延迟

[英]Lags of an ACF plot using ggplot2

I am trying to produce acf plots using ggplot2 . 我正在尝试使用ggplot2生成acf图。 My code is as follows: 我的代码如下:

library(ggplot2)
x = lh
conf.level = 0.95
ciline = qnorm((1 - conf.level)/2)/sqrt(length(x))
bacf = acf(x, plot = FALSE)
bacfdf = with(bacf, data.frame(lag, acf))
ggplot(data=bacfdf, mapping=aes(x=lag, y=acf)) + 
  geom_bar(stat="identity", position = "identity") + 
  ggtitle("Orders")

With this I am able to produce a ggplot2 acf plot of autocorrelations with lags that range from zero up to 20. 有了这个,我能够产生一个自相关的ggplot2 acf图,其滞后范围从零到20。

How can I edit this to have ggplot2 plot lags ranging from -10 through 10 instead? 我如何编辑它以使ggplot2绘图滞后从-10到10不等?

I essentially used the code from this source to come up with my code posted above: http://ask.programmershare.com/387_17805747/ 我本质上使用了来自此来源的代码来提出上面发布的代码: http : //ask.programmershare.com/387_17805747/

Autocorrelation function for lag = 1 is calculated as 滞后= 1的自相关函数 计算

mx <- mean(x)
sum((x[1:(N-1)] - mx)*(x[2:N] - mx)) / sum((x-mx)^2)

for lag = -1 the only thing that would change is x[1:(N-1)] and x[2:N] changing their places. 对于滞后= -1,唯一会改变的是x[1:(N-1)]x[2:N]更改其位置。 It is symmetric , so comparing x[t] with x[th] is the same as x[t+h] with x[t] , because the idea is to compare t -th value with another value that is distant by h steps. 它是对称的 ,因此将x[t]x[th]比较与x[t+h]x[t]进行比较是相同的,因为其思想是将第t个值与另一个相距h个步长的值进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM