Search code examples
rmatrixxtseigenvaluewavelet-transform

Convert large XTS object to array to calculate Eignevalues and Eigenvectors


I have a large XTS object which is the result of a correlation calculation using a sliding window across an XTS ZOO object of 12 x 12 variables for 1343 data points in time.

My large XTS object is structured as follows, rows represent time and columns represent the correlation combinations. A simplified example is shown below:

    AA  BA  CA   AB  BB  CB   AC  BC  CC
t1  1   .1  -.4  .1  1   .3  -.4  .3   1
t2  1   .4  .8   .4  1   .2   .8  .2   1
t3  1   .5  .5   .5  1   .3   .5  .3   1
t4  1   .6  .1   .6  1   .7   .1  .7  .1

Correct me if I'm wrong but I believe the eigen() function in R requires a square matrix to calculate the eigenvalues of the matrix lambda 1,2 and 3?

How can I square the xts object above to find the eigenvalues and vectors with each matrix across time?

I'm guessing I'll have a matrix for each time period (1-4) in the XTS object above and the matrix should be constructed by taking the first 3 values (1 .1 -.4) and putting them into the first column, again the next three values (.1 1 .3) and this goes into the second column and finally the last three values of row one (-.4 .3 1) go into the last column to make up the 3 x 3 matrix which is shown below:

matrix for t1

   A   B   C
A   1 .1 -.4
B  .1  1  .3
C -.4 .3   1

Maybe the transformation from the XTS object is not required to calculate the eigenvalues but if I step through it in my head these are the steps required to calculate the eigenvalues for my XTS object.

Ideally the eigenvalues from each matrix would then be stored in a dataframe or matrix, in the case above I would have a dataframe of 12 observations of 3 variables or a matrix of 3 x 4.

Can anyone tell me if I'm going about this the wrong way and whether eigen() can take the XTS object in it's current form and calculate the eigenvalues?

dput


Solution

  • For demonstration purposes, I'm only returning the greatest of the eigenvalues set, but you can modify the code to return anything you need.

    library(xts)
    dfx <- structure(c(1, 1, 1, 1, 0.1, 0.4, 0.5, 0.6, -0.4, 0.8, 0.5, 0.1, 
    0.1, 0.4, 0.5, 0.6, 1, 1, 1, 1, 0.3, 0.2, 0.3, 0.7, -0.4, 0.8, 
    0.5, 0.1, 0.3, 0.2, 0.3, 0.7, 1, 1, 1, 1), .Dim = c(4L, 9L), .Dimnames = list(
        NULL, c("AA", "BA", "CA", "AB", "BB", "CB", "AC", "BC", "CC"
        )), index = structure(c(1167685200, 1167771600, 1167858000, 
    1167944400), tzone = "", tclass = c("POSIXct", "POSIXt")), .indexCLASS = c("POSIXct", 
    "POSIXt"), tclass = c("POSIXct", "POSIXt"), .indexTZ = "", tzone = "", class = c("xts", 
    "zoo"))
    
    apply.daily(dfx, function(x) eigen(matrix(x, nrow = sqrt(length(x))))$values[1])
    #               [,1]
    #2007-01-02 1.455287
    #2007-01-03 1.984869
    #2007-01-04 1.872842
    #2007-01-05 1.972804