2017-03-12 2 views
1

ich einen maßgeschneiderten ACF und PACF Plot für eine simulierte Zeitreihe zu bauen planen sindHinzufügen Konfidenzintervalle geplottet ACF in ggplot2

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

Im Folgenden wird die Codes ich die Handlung durch ggplot2 zu produzieren schrieb:

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) 

Example ACF and PACF plot

Während dies ist genau das, was ich will, bin ich nicht sicher, wie die confid zu produzieren ENCE Intervalle in acf(ts) und pacf(ts):

ACF TS

Also, meine Frage hat zwei Teile:

  • Wie statistisch die oberen ableiten und die Vertrauensintervalle für Autokorrelation Funktionen und partielle Autokorrelationen in unterer Schranke R?
  • Wie würden Sie es auf die erste Grafik plotten? Ich dachte an geom_ribbon aber jede zusätzliche Idee wird geschätzt!
+0

Für den zweiten Teil können Sie mit 'geom_hline' oder' geom_rect' – yeedle

Antwort

1

Diese arbeiten kann (die Formel für die Vertrauensgrenzen werden von hier https://stats.stackexchange.com/questions/211628/how-is-the-confidence-interval-calculated-for-the-acf-function genommen, einige Optimierungen benötigen):

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

enter image description here

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 

enter image description here

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

enter image description here

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") 

enter image description here