[R] Vectorized expression to extrapolate matrix columns with columns of another matrix
Jeffrey J. Hallman
jhallman at frb.gov
Wed May 12 13:58:21 CEST 2010
Abiel X Reinhart <abiel.x.reinhart at jpmchase.com> writes:
> I have two identically sized matrices of data that represent time series (I am storing the data in zoo objects, but the idea should apply to any matrix of data). The time series in the second matrix extend further than in the first matrix, and I would like to use the data in matrix 2 to extrapolate the data in matrix 1. In other words, if mat1[i,j] == NA, then mat1[i,j] <- mat1[i-1, j]*mat2[i,j]/mat2[i-1,j]. Of course, before we can calculate mat1[i,j] we may need to calculate mat1[i-1,j], and that in turn may require the computation of mat1[i-2,j], etc. This could all clearly be done with loops, but I am wondering if anyone can think of a vectorized expression or other efficient method that would work.
If you use tis objects and all of the columns of mat1 become NA on the same row, i.e.,
mat 1 looks like this:
20100101 1 2 3
20100102 4 5 6
20100103 NA NA NA
20100104 NA NA NA
then something like this should work:
mat1 <- naWindow(mat1) ## drops the ending NA rows
rate <- growth.rate(window(mat2, start = end(mat1)))
growth.rate(mat1) <- rate
--
Jeff
More information about the R-help
mailing list