# [R] Analyzing "Stacked" Time Series

From: Misha <iamisha1_at_comcast.net>
Date: Mon, 07 May 2007 16:43:05 -0700

I have a question about pooling or "stacking" several time series “samples” (sorry in advance for the long, possibly confusing, message).

I'm sure I'm revealing far more ignorance than I'm aware of, but that's why I'm sending this...

[Example at bottom]

I have regional migration flows (“samples”) from, say, regions A to B, A to C, B to A, …., C to B (Noted as xIJ in my example). Each of these is a time series, but I want to use R’s time series tools to fit an ARIMA (or ARMA) model along with some independent variables (Noted as YIJ, ZIJ, where Y and Z are characteristics (e.g., per capita GDP), I indicates the region, and J in {O, D} indicates whether it is the origin or destination region).

I can do this easily enough for a single flow, say from A to B. But what if I want to find one set of coefficients for all of the flows?

While I can construct vectors that look like what I think they should, I can’t figure out how to coerce them into being “stacked” time series, with the time indices repeating through each vector (t=1,...t=10,t=1,...,t=10,...,t=1,...t=10). And since I can’t even figure out how to do this (or whether it is possible/sensible), I certainly can’t use R’s time series packages on this “stacked” time series.

For my example (below), it’s easy enough to do something like,

fit1 <- lm(X ~ YO + YD + ZO + ZD)

But anything involving lags is not correct, since it seems—at best—to be treating my “stacked” time series (i.e., 6 series of length 10) as one series of length 60. For example, these give questionable, if any, output.

arima1 <- arima(X, order = c(1, 0, 0))
fit.gls001 <- gls(X ~ YO + YD + ZO + ZD,

correlation = corARMA(p = 2), method = "ML")

fit.gls002 <- gls(X ~ YO + YD + ZO + ZD +

```		lag(YO) + lag(YD) + lag(ZO) + lag(ZD),
correlation = corARMA(p = 1), method = "ML")
```
ar001 <- ar.ols(cbin(X, YO, YD, ZO, ZD))

Here is my example:

```xAB <- as.ts(round(rnorm(10, 0, 1), 2), start = c(1990, 1))
xAC <- as.ts(round(rnorm(10, 0, 1), 2), start = c(1990, 1))
xBA <- as.ts(round(rnorm(10, .75, 1.5), 2), start = c(1990, 1))
xBC <- as.ts(round(rnorm(10, .25, 1.9), 2), start = c(1990, 1))
xCA <- as.ts(round(rnorm(10, 1.5, 2.2), 2), start = c(1990, 1))
xCB <- as.ts(round(rnorm(10, .5, 0.8), 2), start = c(1990, 1))
yA <- as.ts(round(rnorm(10, -0.2, 0.3), 2), start = c(1990, 1))
yB <- as.ts(round(runif(10, -1, 1), 2), start = c(1990, 1))
yC <- as.ts(round(runif(10, -1, 1), 2), start = c(1990, 1))
zA <- as.ts(round(rnorm(10, -1.5, 2), 2), start = c(1990, 1))
```
zB <- as.ts(round(rnorm(10, 0, 0.5), 2), start = c(1990, 1)) zC <- as.ts(round(runif(10, 0, 0.5), 2), start = c(1990, 1))
```Orig <- c(1, 1, 2, 2, 3, 3)
Dest <- c(2, 3, 1, 3, 1, 2)
Xt <- cbind(xAB, xAC, xBA, xBC, xCA, xCB)
```
Yt <- cbind(yA, yB, yC)
Zt <- cbind(zA, zB, zC)

X <- vector()
for(i in 1:ncol(Xt)){

```	X <- append(X, Xt[,i])

}
```

YO <- vector()
for(i in 1:length(Orig)){
```	YO <- append(YO, Yt[,Orig[i]])

}
```

ZO <- vector()
for(i in 1:length(Orig)){
```	ZO <- append(ZO, Zt[,Orig[i]])

}
```

YD <- vector()
for(i in 1:length(Dest)){
```	YD <- append(YD, Yt[,Dest[i]])

}
```

ZD <- vector()
for(i in 1:length(Dest)){
```	ZD <- append(ZD, Zt[,Dest[i]])

}
```