我正在尝试更改R中产生的acf图,并且没有运气。我的目标是在一个图中绘制多个自相关,而不是使用标准直方图,我想使用不同的颜色将自相关绘制为线条,因此很容易区分不同的自相关。该图还应包括95%的置信区间(与图中相似)。
我的目标看起来像这样:
编辑:如您所见,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
元素是一个数组,最后两个维度控制着要引用哪个系列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
函数对置信带的选择,而不是尝试自己构造它们。您将需要更改标题并可能放入图例,但是如果您了解基本图形命令,那将是微不足道的。