[R] using acf() for multiple columns
Paul Gilbert
pgilbert at bank-banque-canada.ca
Mon Aug 11 10:50:44 CEST 2008
rcoder wrote:
> Hi Gary,
>
> Thanks for your reply. This works fine. Is there any way to send the ACF
> data to a matrix, so I could analyse the data in excel for e.g.?
>
The coefficients are returned in the acf element of the result, as
mentioned in the documentation (?acf). The side affect of plotting can
be controlled by the plot argument:
> A <- matrix(rnorm(1500),nrow=500)
> z <-acf(A, lag.max=10, plot=FALSE)$acf
> dim(z)
[1] 11 3 3
I think what you are looking for are the elements z[,1,1], z[[,2,2] and
z[,3,3].
> z[,1,1]
[1] 1.000000000 0.080656537 0.041652217 -0.051640091 -0.032176245
[6] -0.063410768 0.033985699 0.011444829 -0.027779925 0.007840191
[11] -0.042187141
This is doing some extra calculation compared to what you want, because
it is calculating all the cross correlations. The trade-off is that the
acf calculation gets passed to C code and is fast, whereas splitting out
the rows may not be. For a small number of columns there is probably not
much difference.
If you are going to transfer this to something else by copying and
pasting the result, you should print it to a higher precision. I'm not
sure why you would do that to analyse ... the usual problem is getting
the data into R.
Paul Gilbert
> Thanks,
>
> rcoder
>
> Ling, Gary (Electronic Trading) wrote:
>
>> Hi, here is one possible solution ...
>> -gary
>>
>> ### example ###
>>
>> # create a 500x3 multi-ts
>> A <- matrix(rnorm(1500),nrow=500)
>>
>> # misc. graphical setting
>> par(mfrow=c(ncol(A),1))
>>
>> # then our friend -- lapply(split(...))
>> lapply(split(A,col(A)), acf)
>> #Or
>> lapply(split(A,col(A)), function(ts) acf(ts, lag.max=10))
>>
>>
>>
>>
>>
>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
>> On Behalf Of rcoder
>> Sent: Wednesday, August 06, 2008 4:13 PM
>> To: r-help at r-project.org
>> Subject: [R] using acf() for multiple columns
>>
>>
>>
>> Hi everyone,
>>
>> I'm trying to use the acf() function to calculate the autocorrelation of
>> each column in a matrix. The trouble is that I can only seem to get the
>> function to work if I extract the data in the column into a separate
>> matrix
>> and then apply the acf() function to this column.
>>
>> I have something like this: acf(mat,lag.max=10,na.action=na.pass)
>>
>> ...but I would really like to apply the function to 'mat' where 'mat' is
>> a
>> matrix as opposed to a vector. The function actually doesn't return an
>> acf
>> coefficient, but instead plots the data. So, in addition to handling
>> matrices with multiple columns, would anyone know how to coerce the
>> function
>> to output the underlying data?
>>
>> Finally, when working with a matrix, is there a way I can specify how
>> many
>> plots I can display after the function executes? I managed to generate a
>> multiple plot when I was experimenting, but the titles suggested the acf
>> was
>> calculated between adjacent columns in the matrix, which is something I
>> was
>> puzzled about.
>>
>> Thanks,
>>
>> rcoder
>> --
>> View this message in context:
>> http://www.nabble.com/using-acf%28%29-for-multiple-columns-tp18858672p18
>> 858672.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>> --------------------------------------------------------
>>
>> This message w/attachments (message) may be privileged, confidential or
>> proprietary, and if you are not an intended recipient, please notify the
>> sender, do not use or share it and delete it. Unless specifically
>> indicated, this message is not an offer to sell or a solicitation of any
>> investment products or other financial product or service, an official
>> confirmation of any transaction, or an official statement of Merrill
>> Lynch. Subject to applicable law, Merrill Lynch may monitor, review and
>> retain e-communications (EC) traveling through its networks/systems. The
>> laws of the country of each sender/recipient may impact the handling of
>> EC, and EC may be archived, supervised and produced in countries other
>> than the country in which you are located. This message cannot be
>> guaranteed to be secure or error-free. This message is subject to terms
>> available at the following link:
>> http://www.ml.com/e-communications_terms/. By messaging with Merrill Lynch
>> you consent to the foregoing.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>>
>
>
====================================================================================
La version française suit le texte anglais.
------------------------------------------------------------------------------------
This email may contain privileged and/or confidential in...{{dropped:26}}
More information about the R-help
mailing list