简体   繁体   中英

Lags of an ACF plot using ggplot2

I am trying to produce acf plots using ggplot2 . 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.

How can I edit this to have ggplot2 plot lags ranging from -10 through 10 instead?

I essentially used the code from this source to come up with my code posted above: http://ask.programmershare.com/387_17805747/

Autocorrelation function for lag = 1 is calculated as

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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