Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Reversing the data in a time series object

I figured out a way to backcast (ie. predicting the past) with a time series. Now I'm just struggling with the programming in R.

I would like to reverse the time series data so that I can forecast the past. How do I do this?

Say the original time series object looks like this:

     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2008 116  99 115 101 112 120 120 110 143 136 147 142
2009 117 114 133 134 139 147 147 131 125 143 136 129

I want it to look like this for the 'backcasting':

     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2008 129 136 143 125 131 147 147 139 134 133 114 117
2009 142 147 136 143 110 120 120 112 101 115  99 116

Note, I didn't forget to change the years - I am basically mirroring/reversing the data and keeping the years, then going to forecast.

I hope this can be done in R? Or should I export and do it in Excel somehow?

like image 757
OSlOlSO Avatar asked Aug 07 '11 18:08

OSlOlSO


2 Answers

Try this:

tt <- ts(1:24, start = 2008, freq = 12)
tt[] <- rev(tt)

ADDED. This also works and does not modify tt :

replace(tt, TRUE, rev(tt))
like image 117
G. Grothendieck Avatar answered Nov 15 '22 23:11

G. Grothendieck


You can just coerce the matrix to a vector, reverse it, and make it a matrix again. Here's an example:

mat <- matrix(seq(24),nrow=2,byrow=TRUE)
> mat

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,]    1    2    3    4    5    6    7    8    9    10    11    12
[2,]   13   14   15   16   17   18   19   20   21    22    23    24
> matrix( rev(mat), nrow=nrow(mat) )

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,]   24   23   22   21   20   19   18   17   16    15    14    13
[2,]   12   11   10    9    8    7    6    5    4     3     2     1
like image 30
Ari B. Friedman Avatar answered Nov 16 '22 00:11

Ari B. Friedman