I plan to build a customized ACF
and PACF
plot for a simulated time series
ts <- arima.sim(n=5300,list(order=c(2,0,1), ar=c(0.4,0.3), ma=-0.2))
Below are the codes I wrote to produce the plot through ggplot2
:
library(gridExtra)
theme_setting <- theme(
panel.background = element_blank(),
panel.grid.major.y = element_line(color="grey90", size=0.5),
panel.grid.major.x = element_blank(),
panel.border = element_rect(fill=NA, color="grey20"),
axis.text = element_text(family="Times"),
axis.title = element_text(family="Times"),
plot.title = element_text(size=10, hjust=0.5, family="Times"))
acf_ver_conf <- acf(ts, plot=FALSE)$acf %>%
as_tibble() %>% mutate(lags = 1:n()) %>%
ggplot(aes(x=lags, y = V1)) + scale_x_continuous(breaks=seq(0,41,4)) +
labs(y="Autocorrelations", x="Lag", title= "Time Series, ACF") +
geom_segment(aes(xend=lags, yend=0)) +geom_point() + theme_setting
pacf_ver_conf <- pacf(ts, main=NULL,plot=FALSE)$acf %>%
as_tibble() %>% mutate(lags = 1:n()) %>%
ggplot(aes(x=lags, y = V1)) +
geom_segment(aes(xend=lags, yend=0)) +geom_point() + theme_setting +
scale_x_continuous(breaks=seq(0,41,4))+
labs(y="Partial Autocorrelations", x="Lag", title= "Time Series, PACF")
grid.arrange(acf_ver_conf, pacf_ver_conf, ncol=2)
While this is exactly what I want, I am not sure how to produce the confidence intervals in acf(ts)
and pacf(ts)
:
So, my question has two parts:
- How to statistically derive the upper and lower bound of the confidence intervals for Autocorrelated Functions and Partial Autocorrelations in R?
- How would you plot it onto the first graph? I was thinking about
geom_ribbon
but any additional idea will be appreciated!