Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plot acf of several timeseries in one plot

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).

My goal would look something like this:

Example picture

Edit: 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)

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.

Edit:

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")
like image 670
Olorun Avatar asked May 31 '14 02:05

Olorun


1 Answers

First you look at 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"

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: 

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)

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.

enter image description here

like image 88
IRTFM Avatar answered Sep 23 '22 02:09

IRTFM