当前位置: 首页 > 编程学习 > 其它语言 > R语言 > 正文

R语言绘图:时间序列分析 ggplot2绘制ACF PACF

2018-04-22 来源:博客园/嘻呵呵

R语言真是博大精深

方法一

Acf(gold[,2], type = "correlation",lag.max = 100)
Acf(gold[,2], type = "partial")

方法二

library(ggfortify)
autoplot(acf(gold[,2], plot = FALSE))

方法三

bacf <- acf(gold[, 2], plot = FALSE)
bacfdf <- with(bacf, data.frame(lag, acf))
ggplot(data = bacfdf, mapping = aes(x = lag, y = acf)) +
    geom_segment(mapping = aes(xend = lag, yend = 0)) +
    geom_hline(aes(yintercept = 0.05), linetype = 2, color = 'darkblue')
# pacf
bpacf <- pacf(gold[, 2], plot = FALSE)
bpacf <- with(bpacf, data.frame(lag, acf))
ggplot(data = bpacf, mapping = aes(x = lag, y = acf)) +
    geom_segment(mapping = aes(xend = lag, yend = 0)) +
    geom_hline(aes(yintercept = 0.05), linetype = 2, color = 'darkblue')

方法四

library("forecast")
forecast::ggtsdisplay(gold[,2],lag.max=30)

方法五

ggAcf(gold[,2])
ggPacf(gold[,2])