# Time series with multiple subjects and multiple variables

I am a web developer and novice statistician.

My data looks something like this

Subject  Week   x1  x2  x3  x4  x5  y1
A        1      .5  .6  .7  .8  .7  10
B        1      .3  .6  .2  .1  .3  8
C        1      .3  .1  .2  .3  .2  6
A        2      .1  .9  1.5 .8  .7  5
B        2      .3  .6  .3  .1  .3  2
D        2      .3  .1  .4  .3  .5  10


I am trying to predict y1 as a product of the x variables. However, I have reason to believe that there may be a lag in the effect of the multiple x variables on y1, i.e the x variables from week 1 for subject A influence y1 for subject A in week 2.

Note that not all subjects will have data points for every week (in fact most won't). Subjects will tend to have data points for say week 1, 2, 3, 4 then drop off and not show up again until week 7,8,9. I am willing to restrict my analysis to data points where we have data for the previous N weeks given my hypothesis about lag.

Like I said, I am a novice and am unsure of the best way to deal with a dataset of this form. I am hoping to carry out this analysis either in R, Python, or some combination of the two. I don't think that the current week's x variables will have no effect. I think they will have some effect, perhaps greater than previous weeks. I just believe that previous weeks will have some effect.

I am expecting there to be two to three weeks of lag. To give a little context, the analysis that I am attempting here relates to judging the quality of online traffic. Every week I get a score rating the quality of a certain stream of users I send to a given website. I am trying to find secondary metrics, such as browser distribution, percent duplicate clickouts, etc. that will allow me to predict what that score will be ahead of time.

• I anticipate that this won't be a complete answer, but should give you place to begin. I would start with the material in Chapter 7/Section 6/7/8 of the book "Bayesian Statistical Modeling" by Peter Congdon. My problem involved space/time prediction of IED events and was a bit more complex then yours but the pieces are all there; I'm trying to find my old WinBUGS code (which you could re-work into an R/JAGS implementation). However, there is WinBUGS code available for Congdon's examples and they should provide you a good starting point. – Aengus Apr 16 '12 at 19:21
• Since I am a novice, do you think you could break down for me at a high level what I need to do and what this represents in terms of analysis? Thank you very much and any code that you could provide would be more than appreciated! I am familiar with python and R. – Spencer Apr 16 '12 at 21:25
• How much lag are you talking about? Just one week, or multiple weeks? Are you expecting that $y_{week\ n}$ is a function of the $x$s from just one previous week, or many previous weeks? Although your example is well laid out, providing a set of real-world variables will likely get you better answers, as answerers will be able to see what you're trying to do. – naught101 Apr 17 '12 at 6:52
• I am expecting there to be 2-3 weeks of lag. I've edited the question to give a real world example. – Spencer Apr 17 '12 at 13:31
• I would think of it as a simple regression. The covariates are x1(t), x2(t),...,x5(t) plus your lagged variables x1(t-1), x1(t-2), etc. Depending on how sophisticated you want to get, you can simply take guesses at the lags and plot x1(t-lag) versus y1 and look for relationships or you could run an autocorrelation.You should be able to do the former very quickly in R using data frames and you don't have many lag increments to worry about. I've left out discussion about subjects, but I'm boarding. The easiest way to start with those is to code them as discrete variables. – Aengus Apr 17 '12 at 14:01

As I mentioned in my note above, I would treat this as a regression problem. Here is a link to constructing, in R, the lag (and lead) variables from your data (R Head).

Included in the post is a brief introduction to using the resulting data in a regression model. You might also want to do a bit of background digging on the R package dynlm (dynamic linear regression).

• I am reading up on dynamic linear regression and it seems to fit what I am looking for. Do you have any suggested reading materials? – Spencer Apr 17 '12 at 18:35
• Sorry, not off the top of my head. You are probably already aware of the 'dlm' package in R; here's a link with some R code link – Aengus Apr 17 '12 at 21:31

You could create tables where the y1 is shifted by 0,1,2,3,4 weeks.

Then you run an analysis on them. For instance, you could make a neural network that tries to predict y1 from x. For some ideas, you can give Weka a spin.

Then, you have some measure of predicting y1 from x for each lag. Using this, you can find the lag that fits best.

Alternatively, you can create one table that includes x from the current week, x from the previous week, ... and y1. Then do a analysis of influence (e.g. PCA) to see which week and which variable has the most influence.

• I don't think that the current week's x variables will have no effect. I think they will have some effect, perhaps greater than previous weeks. I just believe that previous weeks will have some effect. Additionally, since I am already familiar with R and python, I'd prefer to use those tools. – Spencer Apr 17 '12 at 13:47
• @Spencer You'll find python/R packages for all the tools in Weka. See the updated answer. – j13r Apr 17 '12 at 14:16