【发布时间】:2021-02-18 05:21:59
【问题描述】:
我有以下数据(从 1 到 1032),我正在尝试绘制自相关和偏自相关的相关图:
prp:数据框
prp$Log.prp.Standardized:我要绘制的列
数据:
prp$Log.prp.Standardized(列名 - 我有 1 列有 1032 个值)
1 1.7923928339
2 0.7792383013
3 -0.2033400303
4 -1.7016479357
5 0.8002357419
6 0.3575677621
7 1.0209246410
8 0.7188631605
9 -0.5320108464
10 -0.2190886401
.
.
.
.
(till 1032)
我正在使用的功能:
correlogram <- function(x, type = "correlation"){
gacf = acf(x, plot=FALSE, lag.max=120, type = type)
gacf.df = with(gacf, data.frame(lag, acf))
gacf.df$sig = qnorm((1 + 0.95)/2)/sqrt(length(x))
q <- ggplot(data = gacf.df, mapping = aes(x = lag, y = acf))
q <- q + xlim(c(0,120)) + theme_bw()
q <- q + geom_hline(aes(yintercept = 0))
q <- q + geom_segment(mapping = aes(xend = lag), yend = 0, lwd = 1)
q <- q + geom_hline(aes(yintercept = c(sig, -1*sig)), linetype = 2, colour = "#e51843")
if(type == "partial"){
q <- q + ylab(expression(alpha[k]))
} else {
q <- q + ylab(expression(rho[k]))
}
q <- q + xlab("lag k")
}
然后是我运行的代码:
require(gridExtra)
library(gridExtra)
library(ggplot2)
library(grid)
q1 <- correlogram(prp$Log.prp.Standardized) + xlab(" ") + ggtitle("Total and Partial Correlograms")
q2 <- correlogram(prp$Log.prp.Standardized, type = "partial")
grid.arrange (q1, q2, nrow = 2)
grid
但我收到以下错误:
错误:美学长度必须为 1 或与数据 (121) 相同:yintercept
任何帮助将不胜感激!
【问题讨论】:
-
在
alpha[k]和rho[k]中。alpha和rho是什么?
标签: r ggplot2 autocorrelation