# Comparison between two times series

I am looking at time series data, comparing the velocity with which different groups of animals move in a new environment. In its raw form, the data consists of trajectories from individual fish that swim together - here you see a sample of about 10 seconds (individual lines correspond to individual fish, the red dotted line is the movement of the entire group - this is what I want to look at in detail).

When I plot the distance the animals traverse over time in a cumulative plot, I see the following picture emerge: the thin lines are individual replicates and the thick ones represent the mean of the corresponding group (color-coded).

It seems to me that the curves are quite different and I tried to quantify this by calculating the area between the curves and comparing this with a distribution of values I receive by randomly shuffling the categories of the individual replicates. The result of this would be the following plot: The histogram shows the distribution of areas between the curves I could in principle get from the observed replicates, the horizontal line is the value I observe with the actual categories.

This strikes me as quite different from the null, but how can I quantify this difference so that a classical frequentist would be happy? In the end, I have only one observation (the actual area between the curves), so a statistical test would be difficult. Does my basic idea make sense at all? Any input is greatly appreciated!

Edit I am aware that on the plot there are negative values shown for the area between the curves - I deliberately did not go for absolute values to keep the distribution normal.

Nice study! Some finer details for your cumulative plot seem to be missing:

1. Do you only have 2 groups of fish represented in that plot and are you interested in comparing the "average group trajectory" across these two groups?

2. What do you actually mean by "individual replicate" in this plot? Does "individual replicate" correspond to an individual fish or to a pair of individual fish? It is important to clarify what entity is implied by "individual replicate, as this entity will need to be represented in your modelling.

Assuming you are indeed interested in comparing the "average group trajectory" across 2 groups, one way of going about this is to use a so-called hierarchical generalized additive mixed effects model (HGAM). See https://peerj.com/articles/6876/ for the article Hierarchical generalized additive models in ecology: an introduction with mgcv by Pedersen et al. which will show you how to pull this off.

In this type of model, you will treat the entity of interest (e.g., individual fish if that is what you mean by "individual replicate") as a random grouping factor. Each entity will have repeated response measurements associated with it, corresponding to the cumulative distance travelled over time. When plotted over time for a specific entity, these measurements will form an observed trajectory specific to that entity.

The model will allow you to capture the underlying shape of each entity's trajectory (for all entities represented by the one included in your study) and hence the underlying shape of the average trajectory in each of your 2 groups of entities. (Again, the average trajectory of a group refers to all entities represented by the ones included in your study for that particular group.) Once you are able to recover the average group trajectories, you can compare them across the entire time span and identify whether they differ across the entire span or perhaps over just specific portions of it.

When you specify your HGAM model, you'll need to consider how the entities specific to a group vary about the average trajectory in that group. For example, the simplest scenario would be that the individual entities in each group have individual trajectories that are shifted up or down from the average trajectory of each group by a random amount. More complicated scenarios would allow for the individual trajectories to be "stretched" and "shifted" versions of the average trajectory of the group. Each of these scenarios would come with its specific model specification, as explained in the above paper.

The HGAM models provide great flexibility because they only assume that the individual (and group) trajectories are smooth and possibly nonlinear. In this sense, these models imply a nonparametric shape for these curves.

Another option for your setting would be to consider that the possibly shape of the individual (and group) trajectories can be captured via a small number of parameters (e.g., via polynomial regression). This approach is popular in growth curve analysis.

Other articles that might come in handy if you decide to pursue these types of approaches include:

1. How to analyze linguistic change using mixed models, Growth Curve Analysis and Generalized Additive Modeling by Winter et al. (https://academic.oup.com/jole/article/1/1/7/2281883)

2. GENERALISED ADDITIVE MIXED MODELS FOR DYNAMIC ANALYSIS IN LINGUISTICS: A PRACTICAL INTRODUCTION by Márton Sóskuthy (https://eprints.whiterose.ac.uk/113858/2/1703_05339v1.pdf)

3. Growth Curve Analysis and Visualization Using R by Daniel Mirman (https://www.routledge.com/Growth-Curve-Analysis-and-Visualization-Using-R/Mirman/p/book/9781466584327 and https://www.danmirman.org/gca);

4. Overview GAMM analysis of time series data by Jacolien van Rij (https://jacolienvanrij.com/Tutorials/GAMM.html);

5. Noam Ross' Resources for Learning About and Using GAMs in R (https://github.com/noamross/gam-resources) and free course on GAMs in R (https://noamross.github.io/gams-in-r-course/).

It appears to me you are dealing with cointegrated series. I would start with creating the time series table where the columns are change in X and Y coordinate of each fish, and the rows are time. Then I'd run PCA on this data set. I bet the first principal component PC1 would catch the common movement of fish. The explained variance by PC1 would tell you how much of the movement is common, and the rest would be the individual movements. Maybe you'd be able to capture in next few PCs the groups of fish that comove together within the population. If you have sample data set, you may get more ideas here

• Neat suggestion, @Aksakal! Were you thinking of dynamic PCA, which would be better equipped to accommodate the time series nature of the input data? Also, could you elaborate on whether you would advocate using the proper form of PCA separately for each group and then comparing each of the first few relevant PCs across groups to see how where across the time span they might differ? Note that each of these PCs will in effect be a time series, so GAM models with factor smooth interactions could be used to compare a particular PC across groups to see where there might be group differences. Commented Feb 14, 2021 at 0:08
• I would start with simple pca, just throw everything in, then watch what happens. Maybe separate pca for x and y coordinates. Commented Feb 14, 2021 at 2:01