R 向ggplot2中绘制的ACF添加置信区间
我计划为模拟时间序列构建一个定制的R 向ggplot2中绘制的ACF添加置信区间,r,plot,time-series,confidence-interval,R,Plot,Time Series,Confidence Interval,我计划为模拟时间序列构建一个定制的ACF和PACF绘图 ts <- arima.sim(n=5300,list(order=c(2,0,1), ar=c(0.4,0.3), ma=-0.2)) 虽然这正是我想要的,但我不确定如何在acf(ts)和pacf(ts)中生成置信区间: 因此,我的问题分为两部分: 如何从统计上推导出R中自相关函数和偏自相关函数的置信区间的上下界 你将如何把它绘制到第一张图上?我正在考虑geom_ribbon,但如果有任何其他想法,我们将不胜感激 这可能有效
ACF
和PACF
绘图
ts <- arima.sim(n=5300,list(order=c(2,0,1), ar=c(0.4,0.3), ma=-0.2))
虽然这正是我想要的,但我不确定如何在acf(ts)
和pacf(ts)
中生成置信区间:
因此,我的问题分为两部分:
- 如何从统计上推导出R中自相关函数和偏自相关函数的置信区间的上下界
- 你将如何把它绘制到第一张图上?我正在考虑
,但如果有任何其他想法,我们将不胜感激李>geom_ribbon
ts.acf%
ggplot(aes(x=滞后,y=V1))+标度x连续(中断=顺序(0,41,4))+
geom_hline(yintercept=conf.lims,lty=2,col='blue')+
实验室(y=“自动相关”,x=“滞后”,title=“时间序列,ACF”)+
geom_段(aes(xend=lags,yend=0))+geom_点()+主题设置
ts.pacf%
ggplot(aes(x=滞后,y=V1))+
geom_段(aes(xend=lags,yend=0))+geom_点()+主题设置+
比例x连续(中断=顺序(0,41,4))+
geom_hline(yintercept=conf.lims,lty=2,col='blue')+
实验室(y=“部分自相关”,x=“滞后”,title=“时间序列,PACF”)
对于第二部分,您可以使用
geom\u hline
或geom\u rect
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)
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")