Dear R-users, We are monitoring the activity of animals during a few days period. The data from each animal (crossing of infra-red beam) are collected as a time series (in 30 min bins). An example is attached below. y <- c(0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,3,28,27,46,76,77,60,19,35,55,59,48 ,87,20,38,82,62,60,85,105,69,109,102,100,101,116,126,119,63,27,25,15,8,0 ,0,3,0,0,3,0,0,5,3,0,0,6,1,29,73,56,56,57,92,34,51,30,76,30,38,47,87,22, 0,68,76,94,101,119,114,115,111,116,134,125,76,23,19,30,2,8,0,3,0,0,0,7,0 ,0,0,0,4,0,7,0,21,4,49,51,56,43,55,55,34,48,16,0,61,22,94,63,102,47,100, 96,113,93,109,123,120,124,115,94,96,76,36,3,0,0,0,0,0,0,2,5,0,0,0,0,2,10 ,33,34,15,0,47,22,20,33,52,4,41,45,0,21,18,38,32,21,78,82,72,102,103,118 ,116,118,114,82,18,5,21,4,0,14,0,5,2,0,0,2,2,0,0,3,0,2,7,16,13,17,50,0,4 8,16,19,34,39,33,3,67,0,68,34,65,84,61,100,85,108,124,141,139,134,96,54, 91,54,12,0,0,0,0,0,0,0,0,0,0,0,4,11,0,19,27,15,12,20) We would like to have an automatic way, using R, to identify the time point of offset of each bout of activity (i.e. when activity goes down to a minimum value, for a defined duration). In the example above the offset times (the element number) should be approximately: 53, 99, 146, 191, 239, 283, 330 (the last bout of activity can be ignores). Any help or advice will be greatly appreciated, Many thanks, Eran Eran Tauber (PhD) Lecturer in Molecular Evolution Dept. of Genetics University of Leicester University Rd, Leicester LE1 7RH England ____________________________________________________________ Phone: 44 (0)116 252-3455, 252-3421 (lab) Fax: 44 (0)116 252-3378 le.ac.uk/genetics/et22
Gabor Grothendieck
2006-Jul-24 15:34 UTC
[R] Identifying peaks (or offsets) in a time series
Try: RSiteSearch("finding peaks") On 7/24/06, Tauber, Dr E. <et22 at leicester.ac.uk> wrote:> Dear R-users, > > We are monitoring the activity of animals during a few days period. The > data from each animal (crossing of infra-red beam) are collected as a > time series (in 30 min bins). An example is attached below. > > y <- > c(0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,3,28,27,46,76,77,60,19,35,55,59,48 > ,87,20,38,82,62,60,85,105,69,109,102,100,101,116,126,119,63,27,25,15,8,0 > ,0,3,0,0,3,0,0,5,3,0,0,6,1,29,73,56,56,57,92,34,51,30,76,30,38,47,87,22, > 0,68,76,94,101,119,114,115,111,116,134,125,76,23,19,30,2,8,0,3,0,0,0,7,0 > ,0,0,0,4,0,7,0,21,4,49,51,56,43,55,55,34,48,16,0,61,22,94,63,102,47,100, > 96,113,93,109,123,120,124,115,94,96,76,36,3,0,0,0,0,0,0,2,5,0,0,0,0,2,10 > ,33,34,15,0,47,22,20,33,52,4,41,45,0,21,18,38,32,21,78,82,72,102,103,118 > ,116,118,114,82,18,5,21,4,0,14,0,5,2,0,0,2,2,0,0,3,0,2,7,16,13,17,50,0,4 > 8,16,19,34,39,33,3,67,0,68,34,65,84,61,100,85,108,124,141,139,134,96,54, > 91,54,12,0,0,0,0,0,0,0,0,0,0,0,4,11,0,19,27,15,12,20) > > We would like to have an automatic way, using R, to identify the time > point of offset of each bout of activity (i.e. when activity goes down > to a minimum value, for a defined duration). In the example above the > offset times (the element number) should be approximately: 53, 99, 146, > 191, 239, 283, 330 (the last bout of activity can be ignores). > > Any help or advice will be greatly appreciated, > > Many thanks, Eran > > > Eran Tauber (PhD) > Lecturer in Molecular Evolution > Dept. of Genetics > University of Leicester > University Rd, Leicester LE1 7RH > England > ____________________________________________________________ > Phone: 44 (0)116 252-3455, 252-3421 (lab) Fax: 44 (0)116 252-3378 > le.ac.uk/genetics/et22 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Here is something that will mark the beginning/end of your sequence. You will have to adjust the low limits and the lengths to fit your requirements. y <- c(0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,3,28,27,46,76,77,60,19,35,55,59,48 ,87,20,38,82,62,60,85,105,69,109,102,100,101,116,126,119,63,27,25,15,8,0 ,0,3,0,0,3,0,0,5,3,0,0,6,1,29,73,56,56,57,92,34,51,30,76,30,38,47,87,22, 0,68,76,94,101,119,114,115,111,116,134,125,76,23,19,30,2,8,0,3,0,0,0,7,0 ,0,0,0,4,0,7,0,21,4,49,51,56,43,55,55,34,48,16,0,61,22,94,63,102,47,100, 96,113,93,109,123,120,124,115,94,96,76,36,3,0,0,0,0,0,0,2,5,0,0,0,0,2,10 ,33,34,15,0,47,22,20,33,52,4,41,45,0,21,18,38,32,21,78,82,72,102,103,118 ,116,118,114,82,18,5,21,4,0,14,0,5,2,0,0,2,2,0,0,3,0,2,7,16,13,17,50,0,48, 16,19,34,39,33,3,67,0,68,34,65,84,61,100,85,108,124,141,139,134,96,54, 91,54,12,0,0,0,0,0,0,0,0,0,0,0,4,11,0,19,27,15,12,20) plot(y, type='l') # your points abline(v=c(53, 99, 146, 191, 239, 283, 330 ), col='black') # assume that 8 is the minimum for at least 5 samples z <- rle(y < 8) z.index <- cumsum(z$length) # mark start abline(v=z.index[!z$value & (z$length > 5)], col='green', lwd=2) # mark end abline(v=z.index[z$value & (z$length > 5)], col='red', lwd=2) On 7/24/06, Tauber, Dr E. <et22@leicester.ac.uk> wrote:> > Dear R-users, > > We are monitoring the activity of animals during a few days period. The > data from each animal (crossing of infra-red beam) are collected as a > time series (in 30 min bins). An example is attached below. > > y <- > c(0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,3,28,27,46,76,77,60,19,35,55,59,48 > ,87,20,38,82,62,60,85,105,69,109,102,100,101,116,126,119,63,27,25,15,8,0 > ,0,3,0,0,3,0,0,5,3,0,0,6,1,29,73,56,56,57,92,34,51,30,76,30,38,47,87,22, > 0,68,76,94,101,119,114,115,111,116,134,125,76,23,19,30,2,8,0,3,0,0,0,7,0 > ,0,0,0,4,0,7,0,21,4,49,51,56,43,55,55,34,48,16,0,61,22,94,63,102,47,100, > 96,113,93,109,123,120,124,115,94,96,76,36,3,0,0,0,0,0,0,2,5,0,0,0,0,2,10 > ,33,34,15,0,47,22,20,33,52,4,41,45,0,21,18,38,32,21,78,82,72,102,103,118 > ,116,118,114,82,18,5,21,4,0,14,0,5,2,0,0,2,2,0,0,3,0,2,7,16,13,17,50,0,4 > 8,16,19,34,39,33,3,67,0,68,34,65,84,61,100,85,108,124,141,139,134,96,54, > 91,54,12,0,0,0,0,0,0,0,0,0,0,0,4,11,0,19,27,15,12,20) > > We would like to have an automatic way, using R, to identify the time > point of offset of each bout of activity (i.e. when activity goes down > to a minimum value, for a defined duration). In the example above the > offset times (the element number) should be approximately: 53, 99, 146, > 191, 239, 283, 330 (the last bout of activity can be ignores). > > Any help or advice will be greatly appreciated, > > Many thanks, Eran > > > Eran Tauber (PhD) > Lecturer in Molecular Evolution > Dept. of Genetics > University of Leicester > University Rd, Leicester LE1 7RH > England > ____________________________________________________________ > Phone: 44 (0)116 252-3455, 252-3421 (lab) Fax: 44 (0)116 252-3378 > le.ac.uk/genetics/et22 > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]
Could Petr Pikal's peaks function (finzi.psych.upenn.edu/R/Rhelp02a/archive/33097.html) be of any use? Ulrik On 7/24/06, Tauber, Dr E. <et22 at leicester.ac.uk> wrote:> Dear R-users, > > We are monitoring the activity of animals during a few days period. The > data from each animal (crossing of infra-red beam) are collected as a > time series (in 30 min bins). An example is attached below. > > y <- > c(0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,3,28,27,46,76,77,60,19,35,55,59,48 > ,87,20,38,82,62,60,85,105,69,109,102,100,101,116,126,119,63,27,25,15,8,0 > ,0,3,0,0,3,0,0,5,3,0,0,6,1,29,73,56,56,57,92,34,51,30,76,30,38,47,87,22, > 0,68,76,94,101,119,114,115,111,116,134,125,76,23,19,30,2,8,0,3,0,0,0,7,0 > ,0,0,0,4,0,7,0,21,4,49,51,56,43,55,55,34,48,16,0,61,22,94,63,102,47,100, > 96,113,93,109,123,120,124,115,94,96,76,36,3,0,0,0,0,0,0,2,5,0,0,0,0,2,10 > ,33,34,15,0,47,22,20,33,52,4,41,45,0,21,18,38,32,21,78,82,72,102,103,118 > ,116,118,114,82,18,5,21,4,0,14,0,5,2,0,0,2,2,0,0,3,0,2,7,16,13,17,50,0,4 > 8,16,19,34,39,33,3,67,0,68,34,65,84,61,100,85,108,124,141,139,134,96,54, > 91,54,12,0,0,0,0,0,0,0,0,0,0,0,4,11,0,19,27,15,12,20) > > We would like to have an automatic way, using R, to identify the time > point of offset of each bout of activity (i.e. when activity goes down > to a minimum value, for a defined duration). In the example above the > offset times (the element number) should be approximately: 53, 99, 146, > 191, 239, 283, 330 (the last bout of activity can be ignores). > > Any help or advice will be greatly appreciated, > > Many thanks, Eran > > > Eran Tauber (PhD) > Lecturer in Molecular Evolution > Dept. of Genetics > University of Leicester > University Rd, Leicester LE1 7RH > England > ____________________________________________________________ > Phone: 44 (0)116 252-3455, 252-3421 (lab) Fax: 44 (0)116 252-3378 > le.ac.uk/genetics/et22 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Blog: ulrikstervbo.blogspot.com Mailing-list: coollist.com/group.cgi?l=ulrik_i_berlin