将置信区间添加到 ggplot2 中绘制的 ACF [英] Adding Confidence Intervals to plotted ACF in ggplot2

查看:98
本文介绍了将置信区间添加到 ggplot2 中绘制的 ACF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划为模拟时间序列构建一个定制的ACFPACF

ts <- arima.sim(n=5300,list(order=c(2,0,1), ar=c(0.4,0.3), ma=-0.2))

以下是我通过ggplot2编写的用于生成绘图的代码:

库(gridExtra)theme_setting <- 主题(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="时间序列,ACF") +geom_segment(aes(xend=lags,yend=0)) +geom_point() + theme_settingpacf_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))+实验室(y =部分自相关",x =滞后",标题=时间序列,PACF")grid.arrange(acf_ver_conf, pacf_ver_conf, ncol=2)

虽然这正是我想要的,但我不确定如何在 acf(ts)pacf(ts) 中产生置信区间:

所以,我的问题有两个部分:

  • 如何从统计上推导出 R 中自相关函数和偏自相关的置信区间的上限和下限?
  • 您将如何将其绘制到第一个图形上?我正在考虑 geom_ribbon,但如果有任何其他想法,我们将不胜感激!

解决方案

这可能有效(置信限的公式取自此处

alpha <- 0.95conf.lims <- c(-1,1)*qnorm((1 + alpha)/2)/sqrt(ts.acf$n.used)ts.acf$acf%>%as_tibble() %>% mutate(lags = 1:n()) %>%ggplot(aes(x=lags, y = V1)) + scale_x_continuous(breaks=seq(0,41,4)) +geom_hline(yintercept=conf.lims, lty=2, col='blue') +labs(y="Autocorrelations", x="Lag", title="时间序列,ACF") +geom_segment(aes(xend=lags,yend=0)) +geom_point() + theme_setting

ts.pacf <- pacf(ts, main=NULL,plot=TRUE)

alpha <- 0.95conf.lims <- c(-1,1)*qnorm((1 + alpha)/2)/sqrt(ts.pacf$n.used)ts.pacf$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))+geom_hline(yintercept=conf.lims, lty=2, col='blue') +实验室(y =部分自相关",x =滞后",标题=时间序列,PACF")

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!

解决方案

This may work (the formula for the confidence limits are taken from here https://stats.stackexchange.com/questions/211628/how-is-the-confidence-interval-calculated-for-the-acf-function, may need some tweaking):

ts.acf <- acf(ts, plot=TRUE)

alpha <- 0.95
conf.lims <- c(-1,1)*qnorm((1 + alpha)/2)/sqrt(ts.acf$n.used)

ts.acf$acf %>% 
  as_tibble() %>% mutate(lags = 1:n()) %>% 
  ggplot(aes(x=lags, y = V1)) + scale_x_continuous(breaks=seq(0,41,4)) +
  geom_hline(yintercept=conf.lims, lty=2, col='blue') +
  labs(y="Autocorrelations", x="Lag", title= "Time Series, ACF") +
  geom_segment(aes(xend=lags, yend=0)) +geom_point() + theme_setting

ts.pacf <- pacf(ts, main=NULL,plot=TRUE)

alpha <- 0.95
conf.lims <- c(-1,1)*qnorm((1 + alpha)/2)/sqrt(ts.pacf$n.used)

ts.pacf$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))+ 
  geom_hline(yintercept=conf.lims, lty=2, col='blue') +
  labs(y="Partial Autocorrelations", x="Lag", title= "Time Series, PACF")

这篇关于将置信区间添加到 ggplot2 中绘制的 ACF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆