在一个图中绘制多个时间序列的 acf [英] plot acf of several timeseries in one plot

查看:86
本文介绍了在一个图中绘制多个时间序列的 acf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to alter the acf plot produced in R and am having no luck. My goal is to plot several autocorrelations in one plot and, instead of using the standard histogram, I would like to plot the autocorrelations as lines using different colors, so it is easy to distinguish between the different autocorrelations. The plot should also include the 95% confidence interval (similar as in the picture).

我的目标是这样的:

如您所见,第 0 天的 acf 结果也被排除在外.

As you can see, the acf result for the 0 day are also excluded.

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

So far my code looks like the following:

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

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

So basically I only have the acf results and no clear idea of how to plot the acf results in a combined plot with colored lines.

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 元素是一个数组,其中最后两个维度控制引用哪个系列 acf 或 ccf 结果.然后 plot(ACF) 显示默认绘图机制将多个绘图放在同一页面上(您试图避免这种情况.)所以运行:

You see that the $acf element is an array with the last two dimensions controlling which series acf or ccf result is being referenced. Then plot(ACF) which shows you that the default plotting mechanism puts multiple plots on the same page (which you are trying to avoid.) So run:

> 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 系列的数据:

So "back up" to the first plot using your user interface for the interactive plot device, and then add the data from the otehr series using whatever colors and line widths you choose:

> 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 函数选择的置信区间,而不是尝试自己构建它们.您将需要更改标题并可能添加图例,但如果您了解基本图形命令,这应该是微不足道的.

I did not omit the first period but rather limited the y-range for plotting. That was simpler and allowed me to accept the default plot.acf function's choice of confidence bands rather than trying to construct them myself. You will need to change the title and probably put in a legend, but that should be trivial if you understand the base-graphic commands.

这篇关于在一个图中绘制多个时间序列的 acf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆