gpt4 book ai didi

r - 在一个图中绘制多个时间序列的 acf

转载 作者:行者123 更新时间:2023-12-04 19:06:11 26 4
gpt4 key购买 nike

我正在尝试更改在 R 中生成的 acf 图并且我没有运气。我的目标是在一个图中绘制多个自相关,而不是使用标准直方图,我想将自相关绘制为使用不同颜色的线,因此很容易区分不同的自相关。该图还应包括 95% 置信区间(与图片类似)。

我的目标是这样的:

Example picture

编辑:如您所见,0 天的 acf 结果也被排除在外。

到目前为止,我的代码如下所示:

ACFdata <- merge(returns$companyA, returns$companyB)
ACF <- acf(ACFdata, na.action=na.pass, plot=FALSE)

所以基本上我只有 acf 结果,并不清楚如何在带有彩色线条的组合图中绘制 acf 结果。

编辑:
dput(ACF)
structure(list(acf = structure(c(1, 0.145125809377954, 0.142861039994255,
0.0290589250361852, 0.124017821439246, 0.143011895498405, 0.105734336151885,
0.0788661257638103, 0.0273805239429181, -0.118479508798021, 0.101475240804517,
0.107529091607734, 0.0325071547524698, 0.15248825917752, 0.0345632600693495,
0.105214927797195, 0.121820119834598, 0.106869630726315, 0.0957839598194307,
-0.0908719122532893, -0.00734593289915199, 0.0178894474261508,
0.0499571905134495, 0.0780855846282789, 0.0493591013094398, -0.0749535131984232,
0.357086608389703, 0.246585751931129, -0.0629762920537067, 0.0395286467626801,
0.0419665673763051, 0.00328571836147342, -0.00519232466623128,
0.00483533922926756, -0.0250664920310689, -0.0876036092345946,
0.0627421774389966, 0.135479194083771, 0.0626078698366847, 0.101742576940549,
0.168581486338436, 0.0471250703324634, 0.0340518458280056, 0.0758087712436733,
0.0124645208996951, -0.0277606211509939, -0.0341158520505214,
-0.0644578776612549, -0.045110487814526, -0.0623504592674428,
-0.0351696262152127, 0.058995956134521, 0.357086608389703, 0.0252501548107572,
0.0611739122500323, 0.215137916544862, 0.183625254355587, 0.124460309708319,
0.138507997600327, 0.040228791497421, 0.0140766070862445, -0.0799271843641712,
0.017348973311441, 0.0952746355608701, 0.0404310918206657, 0.0632714503581609,
-0.0257358208892062, 0.0599565925085307, 0.0384859490239319,
0.0886012309614729, 0.0596889523276417, 0.0533055470088723, 0.0770419303845914,
0.0840758532202191, 0.0518662906637178, 0.0399131621778747, 0.0202505502465014,
-0.0105112241804381, 1, 0.12202126664333, -0.0380896874570601,
0.171699455089945, 0.0921701048038319, -0.107621049165039, 0.0206611931650316,
-0.00519190992729939, -0.0631090559052638, -0.0978803261385059,
-0.0277111483321292, 0.064129198291785, -0.0932937679361303,
0.0798459519613646, 0.0889483107174154, -0.0116665547060194,
0.00663627461258374, 0.135982611207688, -0.0258901243417071,
0.11835604048827, 0.100938356006999, 0.0132499377804722, 0.0534896127278462,
0.00128064337860851, -0.0690617100695171, 0.0814839944828229), .Dim = c(26L,
2L, 2L)), type = "correlation", n.used = 778L, lag = structure(c(0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 0, -1, -2, -3, -4, -5, -6, -7, -8,
-9, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -20, -21,
-22, -23, -24, -25, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 1, 2,
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25), .Dim = c(26L, 2L, 2L)), series = "test",
snames = c("returns$companyA", "returns$companyB"
)), .Names = c("acf", "type", "n.used", "lag", "series",
"snames"), class = "acf")

最佳答案

首先你看str(ACF) :

> str(ACF)
List of 6
$ acf : num [1:26, 1:2, 1:2] 1 0.1451 0.1429 0.0291 0.124 ...
$ type : chr "correlation"
$ n.used: int 778
$ lag : num [1:26, 1:2, 1:2] 0 1 2 3 4 5 6 7 8 9 ...
$ series: chr "test"
$ snames: chr [1:2] "returns$companyA" "returns$companyB"
- attr(*, "class")= chr "acf"

你看到 $acf element 是一个数组,最后两个维度控制引用哪个系列 acf 或 ccf 结果。然后 plot(ACF)这表明默认绘图机制将多个绘图放在同一页面上(您试图避免这种情况。)所以运行:
> plot(ACF, type="l", max.mfrow=1, ylim=c(-.2,.4))
Hit <Return> to see next plot:
Hit <Return> to see next plot:
Hit <Return> to see next plot:
Hit <Return> to see next plot:

因此,使用交互式绘图设备的用户界面“备份​​”到第一个绘图,然后使用您选择的任何颜色和线宽添加来自 otehr 系列的数据:
> lines(ACF$acf[-1, 2,1], lty=3, col="red", lwd=3)
> lines(ACF$acf[-1, 2,2], lty=2, col="orange", lwd=3)
> lines(ACF$acf[-1, 1,2], lty=2, col="blue", lwd=2)

我没有省略第一个时期,而是限制了绘图的 y 范围。这更简单,并允许我接受默认值 plot.acf函数对置信区间的选择,而不是尝试自己构建它们。您将需要更改标题并可能添加图例,但如果您了解基本图形命令,这应该是微不足道的。

enter image description here

关于r - 在一个图中绘制多个时间序列的 acf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23965892/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com