Learn R Programming

regtools (version 1.1.0)

TStoX: Transform Time Series to Rectangular Form

Description

Inputs a time series and transforms it to a form suitable for prediction using lm etc.

Usage

TStoX(x,lg,y=NULL)
TStoMat(xmat,lg,y)

Arguments

x

A vector.

lg

Lag, a positive integer.

xmat

A matrix, data frame etc., with each column a time series, over a common time period.

y

A time series, again on that common time period If NULL, it is set to x

Value

Let m denote length of x, and in the matrix input case, the number of rows in xmat. Let p be 1 in the vector case, ncol(xmat) in the matrix case. The return value is a matrix with m-lg rows. There will be p*lg+1 columns, with "Y," the numbers to be predicted in the last column. y[lg+1], y[lg+2],...,y[m].

In the matrix case, in a given row, there will be all lg recent observations for the first time series, then all lg recent observations for the second one, and so on, and finally the y value.

Details

TStoX is for transforming vectors, while TStoMat handles the multivariate time series case. Intended for use with lm or other regression model, predicting y[i] from observations i-lg, i-lg+1,...,i-1.

Examples

Run this code
# NOT RUN {
set.seed(9999)
z <- sample(1:100,12)
z
# [1] 87 66 79 21 67 81 97 77 92 68 74  3
TStoX(z,3)
#      [,1] [,2] [,3] [,4]
# [1,]   87   66   79   21
# [2,]   66   79   21   67
# [3,]   79   21   67   81
# [4,]   21   67   81   97
# [5,]   67   81   97   77
# [6,]   81   97   77   92
# [7,]   97   77   92   68
# [8,]   77   92   68   74
# [9,]   92   68   74    3
set.seed(9999)
zm <- matrix(sample(1:100,24),nrow=2,byrow=TRUE)
y <- sample(1:5,12,replace=TRUE)
zm
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,]   87   66   79   21   67   81   97   77   92    68
# [2,]   15   96   37   80   78    7   69   12   27    84
#      [,11] [,12]
# [1,]    74     3
# [2,]   100    43
y
# [1] 2 2 2 1 3 2 2 4 3 1 5 1
xy <- TStoMat(zm,5,y)
lm(xy[,11] ~ xy[,1:10])  # toy example, need larger m
# }

Run the code above in your browser using DataLab