[R] Discover significant change in sorted vector

vito muggeo vmuggeo at dssm.unipa.it
Thu Apr 23 10:42:23 CEST 2009


dear Hans,
As pointed out by Petr Pikal, segmented allows to estimate breakpoints 
of (continuous) piecewise relationships. That is, the mean lines are 
assumed to be connected and segmented tries to join them at the 
estimated breakpoints. The estimated breakpoints may be any value in the 
range of the segmented covariate (not necessarly an integer). 
Computation times are substantially indipendent of the number of 
breakpoints to be estimated.

strucchange "thinks different"; it allows disconnected segments and the 
estimated breakpoints have to be the observed time indices.

Hope this helps you,
vito

Petr PIKAL ha scritto:
> Hi
> 
> One possibility is to use segmented
> 
> e.g
> 
>> 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)
>> plot(ix,a)
>> library(segmented)
>> fit<-lm(a~ix)
>> fit.s<-segmented(fit, ~ix, list(ix=c(5,10)))
>> fit.s
> Call: segmented.lm(obj = fit, seg.Z = ~ix, psi = list(ix = c(5, 10)))
> 
> Meaningful coefficients of the linear terms:
> (Intercept)          ix       U1.ix       U2.ix 
>   0.6785714   1.0714286   8.9285714  -8.4500000 
> 
> Estimated Break-Point(s) psi1.ix psi2.ix :  8.476 10.880
> 
> Regards
> Petr
> 
> 
> r-help-bounces at r-project.org napsal dne 22.04.2009 15:45:55:
> 
>> Gabor, initially this looked like the perfect solution, exactly what I 
>> need.
>>
>> Unfortunately it is too expensive/costly. I have vectors of length 800 
>> and more, my machine needs > 5 minutes (I aborted) to compute the 
>> breakpoints. Required is computation time < 1 sec. :)
>>
>> Any other suggestions? Maybe there is another approach not that 
>> perfect as from the strucchange package, but still sufficient?
>>
>> Best
>> Henning
>>
>>
>> Am 22.04.2009 um 14:55 schrieb Gabor Grothendieck:
>>
>>> 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)
>>>> bp
>>>         Optimal 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.
>>>>
>> ______________________________________________
>> 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.
> 
> ______________________________________________
> 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.
> 
> 

-- 
====================================
Vito M.R. Muggeo
Dip.to Sc Statist e Matem `Vianelli'
Università di Palermo
viale delle Scienze, edificio 13
90128 Palermo - ITALY
tel: 091 6626240
fax: 091 485726/485612
http://dssm.unipa.it/vmuggeo




More information about the R-help mailing list