Hans-Henning Gabriel
2009-Apr-22 11:27 UTC
[R] Discover significant change in sorted vector
Hi, suppose I have a simple sorted vector like this: a <- c(2,3,3,5,6,8,8,9,15, 25, 34,36,36,38,41,43,44,44,46); Is there a function in R, I can use to discover that from index 8 to index 11 the values are changing significantly? The function should return a value pointing to one of the indices 8, 9, 10 or 11. Any of them would be fine. The difficulty is that there may be no big gap. I mean, indices 8 and 11 are somehow "connected" by indices 9 and 10. So, it's not an option to just search for biggest difference between the values. Perfect would be a function that is able to discover multiple changes if it is present in the data. Thanks!! Henning
Henrique Dallazuanna
2009-Apr-22 11:40 UTC
[R] Discover significant change in sorted vector
Perhaps something about like this: which(diff(a) > mean(diff(a))) On Wed, Apr 22, 2009 at 8:27 AM, Hans-Henning Gabriel < hanshenning.gabriel@gmail.com> wrote:> Hi, > > suppose I have a simple sorted vector like this: > > a <- c(2,3,3,5,6,8,8,9,15, 25, 34,36,36,38,41,43,44,44,46); > > Is there a function in R, I can use to discover that from index 8 to index > 11 the values are changing significantly? > The function should return a value pointing to one of the indices 8, 9, 10 > or 11. Any of them would be fine. > The difficulty is that there may be no big gap. I mean, indices 8 and 11 > are somehow "connected" by indices 9 and 10. So, it's not an option to just > search for biggest difference between the values. > > Perfect would be a function that is able to discover multiple changes if it > is present in the data. > > Thanks!! > Henning > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Hans-Henning Gabriel wrote:> Hi, > > suppose I have a simple sorted vector like this: > > a <- c(2,3,3,5,6,8,8,9,15, 25, 34,36,36,38,41,43,44,44,46); > > Is there a function in R, I can use to discover that from index 8 to > index 11 the values are changing significantly? > The function should return a value pointing to one of the indices 8, > 9, 10 or 11. Any of them would be fine. > The difficulty is that there may be no big gap. I mean, indices 8 and > 11 are somehow "connected" by indices 9 and 10. So, it's not an option > to just search for biggest difference between the values. > > Perfect would be a function that is able to discover multiple changes > if it is present in the data. >Hi Henning, Is max(diff(a)) any good to you? Jim
Try this:> a <- c(2,3,3,5,6,8,8,9,15, 25, 34,36,36,38,41,43,44,44,46); > ix <- seq_along(a) > library(strucchange) > bp <- breakpoints(a ~ ix, h = 4) > bpOptimal 3-segment partition: Call: breakpoints.formula(formula = a ~ ix, h = 4) Breakpoints at observation number: 7 11 Corresponding to breakdates: 0.3684211 0.5789474> plot(a ~ ix) > lines(ix, fitted(bp))On Wed, Apr 22, 2009 at 7:27 AM, Hans-Henning Gabriel <hanshenning.gabriel at gmail.com> wrote:> Hi, > > suppose I have a simple sorted vector like this: > > a <- c(2,3,3,5,6,8,8,9,15, 25, 34,36,36,38,41,43,44,44,46); > > Is there a function in R, I can use to discover that from index 8 to index > 11 the values are changing significantly? > The function should return a value pointing to one of the indices 8, 9, 10 > or 11. Any of them would be fine. > The difficulty is that there may be no big gap. I mean, indices 8 and 11 are > somehow "connected" by indices 9 and 10. So, it's not an option to just > search for biggest difference between the values. > > Perfect would be a function that is able to discover multiple changes if it > is present in the data. > > Thanks!! > Henning > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >