# TS: Transform Time Series to Rectangular Form In matloff/regtools: Regression and Classification Tools

 TStoX R Documentation

## Transform Time Series to Rectangular Form

### Description

Input a time series and transform it to a form suitable for prediction using `lm` etc.

### Usage

```TStoX(x,lg)
TStoXmv(xmat,lg,y)
```

### Arguments

 `x` A vector. `lg` Lag, a positive integer. `xmat` A matrix, data frame etc., a multivariate time series. Each column is a time series, over a common time period. `y` A time series, again on that common time period. If NULL in `TStoXmv`, then `y` is set to `x` (i.e. for a univariate time series in which older values predict newer ones).

### Details

Similar to `stats::embed`, but in lagged form, with applications such as `lm` in mind.

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

### Value

As noted, the idea is to set up something like `lm(Y ~ X)`. 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.

In the output in the multivariate case, let k denote `ncol(xmat)`. Then the first k columns of the output will be the k series at lag `lg`, the second k columns will be the k series at lag `lg-1`, ..., and the `lg`-th set of k columns will be the k series at lag 1,

Norm Matloff

### Examples

```
x1 <- c(5,12,13,8,88,6)
x2 <- c(5,4,3,18,168,0)
y <- 1:6
xmat <- cbind(x1,x2)

TStoX(x1,2)
#      [,1] [,2] [,3]
# [1,]    5   12   13
# [2,]   12   13    8
# [3,]   13    8   88
# [4,]    8   88    6

xy <- TStoXmv(xmat,2,y)
xy
#      [,1] [,2] [,3] [,4] [,5]
# [1,]    5    5   12    4    3
# [2,]   12    4   13    3    4
# [3,]   13    3    8   18    5
# [4,]    8   18   88  168    6

lm(xy[,5] ~ xy[,-5])
# Coefficients:
# (Intercept)    xy[, -5]1    xy[, -5]2    xy[, -5]3    xy[, -5]4
#       -65.6          3.2         18.2         -3.2           NA
# need n > 7 here for useful lm() call, but this illustrates the idea
```

matloff/regtools documentation built on July 17, 2022, 10:10 a.m.