2016-07-31 24 views

回答

6

有很多方法可以做到這一點,你可以找到他們,如果你花幾分鐘搜索或搜索本網站。

單程是forecast包中的ggAcfggPacf。他們創建了ggplot2對象,然後您可以使用ggplot語法和參數進行自定義。

series <- rnorm(300) 
plot <- ggAcf(series) 

enter image description here

+0

謝謝。實際上這些功能在預測包的以前版本中並不存在。 – Chaatak

4

除了forecast::ggAcf功能,它也相當快,以自己與ggplot做。唯一令人討厭的是acf沒有返回置信區間的界限,所以你必須自己計算它們。

與ggplot繪圖ACF並且無需其它包

# plot acf with ggplot 
ggacf <- function(series) { 
    significance_level <- qnorm((1 + 0.95)/2)/sqrt(sum(!is.na(series))) 
    a<-acf(series, plot=F) 
    a.2<-with(a, data.frame(lag, acf)) 
    g<- ggplot(a.2[-1,], aes(x=lag,y=acf)) + 
      geom_bar(stat = "identity", position = "identity") + xlab('Lag') + ylab('ACF') + 
      geom_hline(yintercept=c(significance_level,-significance_level), lty=3); 

    # fix scale for integer lags 
    if (all(a.2$lag%%1 == 0)) { 
    g<- g + scale_x_discrete(limits = seq(1, max(a.2$lag))); 
    } 
    return(g); 
} 

#example: plot correlogram of an AR(1) process 
require('stats'); 
ggacf(arima.sim(model=list(ar=0.9), n=300)); 

實施例:ACF標繪ggplot,對於AR(1)過程

在下面可以看到從代碼段的輸出。積

  • 包含自相關係數的95%置信區間
  • 不顯示在0的自相關,這始終是1(並且使情節更難我認爲閱讀)

acf plotted with ggplot