Looking for pattern of events in a time series I have a time course experiment that follows 8 treatment groups of 12 fish for 24 hours with observations made at 5 second intervals.  Among the measurements made is how far each fish travels (in mm)  between observations. The 24 hours are divided into 1 dark period and 1 light period.
Here is a plot of the movements of the 12 individual fish in treatment group H for the first hour of the dark period:
 
You can see that some fish have long periods of inactivity, some short periods, and some have none during this particular window. I need to combine the data from all 12 fish in the treatment group in such a way as to identify the length and frequency of the rest periods during the entire dark period and the entire light period. I need to do this for each treatment group. Then I need to compare the differences between their rest period lengths and frequencies.
I'm not a stats gal, and I'm completely at sea. The problem resembles sequence alignment to me (my bioinfomatics background), so I'm thinking Hidden Markov models, but this may be way off base.  Could anyone suggest a good approach to this problem and perhaps a small example in R?
Thanks!
 A: I think an HMM-based analysis could be helpful for you. Since you know that you are looking for a distinction between rest and motion, you can just postulate a 2-state model. For HMMs, you need to specify the emission probability for each state. My first try would be to use an exponential (or a gamma?) for the resting phase (since it bounded by zero from below and a normal distribution for the other state (you should set the initial parameters to a some reasonable value). 
You can then calculate the posterior state distribution along with the maximum-likelihood estimates for your parameters. The posterior-state sequence can give you the estimated lengths of the resting and activity periods (just count the number of successive states). You could even put the dark/light period as covariate into the model.
This http://cran.r-project.org/web/packages/depmixS4/index.html is a great package for HMMs.
This http://cran.r-project.org/web/packages/depmixS4/vignettes/depmixS4.pdf vignette has very useful information about its application and the usage of constraints and covariates with HMMs as well.
One problem I'm seeing is that you have multiple fish. You should start by fitting a HMM for each fish separately. Maybe you could combine fish if you could somehow "normalize" the activity such that they could yield the same emission probability parameters. Or you could use the fish-number as a covariate.
Some example code:
require(depmixS4)
set.seed(1)
mod <- depmix( activity~1, data=yourdata, nstates=2,
               family=gaussian() );
fitted <- fit(mod)

but there are many, many possibilities, check out the above links!
Good luck with your project!
