[R] problems with package 'segmented'

Rolf Turner rolf.turner at xtra.co.nz
Sat Jan 26 03:45:43 CET 2013


In view of our private communication on this matter (to the effect that
you had not been able to elicit a response from Vito Muggeo) I did a
little delving into the code of segmented.lm().  I ***think*** I have found
the problem.  On line 243 (of the code as seen from loading the package)
the construction:

     initial <- unlist(mapply(function(x, y) {

should read:

     initial <- as.vector(mapply(function(x, y) {

(The unlist() construction  has no effect.)

Once this change is made, the function runs on the example
without throwing an error (or warning).

I have not investigated as to whether the result returned is sensible.

It is also possible that the "fix" I have suggested will induce other 
problems
in other contexts; I don't really understand what's going on well enough to
be certain.

I hope that this is of some help, but.

     cheers,

             Rolf Turner


On 01/12/2010 12:32 AM, Benedikt Drosse wrote:

(BTW --- shouldn't that date read "01/12/2012"?  I don't *think*
your posting sat in my inbox for two *years*!!!)

> Dear R-users,
>
> I am trying to understand how the 'segmented'-package works to determine
> breakpoints and slopes of regression lines in broken-line regression models.
> However, I am not able to repeat the example on the "plant"-dataset,
> which was reported in the accompanying paper of the package. (V.M.R
> Muggeo, "Segmented: an R package to fit regression models with
> broken-line relationships")
>
> I would be grateful for any ideas, why this function is not working the
> way it was described in the paper. The error message points to a wrong
> length of row- and dimnames. Unfortunately this doesn't help me very
> much in understanding the function, if it does not work on the example data.
>
> Do you think the error will be somewhere in the function, or is it my
> stupidity?
> I am using R version 2.15.1 and the package was built in 2.15.2, but in
> respect to the error message I cannot imagine that this will cause my
> problems.
>
>
> Below you will find the code as it was reported in the paper:
>
> library(segmented)
>
> data("plant")
> attach(plant)
>
> X <- model.matrix(~0 + group)*time
>
> time.KV <- X[,1]
> time.KW <- X[,2]
> time.WC <- X[,3]
>
> olm <- lm(y~0+ group + time.KV + time.KW + time.WC)
>
> os <- segmented(olm, seg.Z= ~ time.KV + time.KW + time.WC,
> psi=list(time.KV=c(300,500), time.KW=c(450,600), time.WC=c(300,450)))
>
>
> The error message is as follows:
>
> Error in `rownames<-`(`*tmp*`, value = c("psi1.time.KV", "psi2.time.KV",  :
>     Length of 'dimnames' [1] not equal to array extent
> Zusätzlich:Warnmeldung:
> In cbind(initial, psi, sqrt(vv)) :
>     number of rows of result is not a multiple of vector length (arg 2)
>
>
>
> However, when I take out one break.point from any of the groups (e.g.
> time.KV=300), the function works fine without reporting any errors:
>
> code as follows:
> os <- segmented(olm, seg.Z= ~ time.KV + time.KW + time.WC,
> psi=list(time.KV=300, time.KW=c(450,600), time.WC=c(300,450)))
>
>
> Thanks alot for your comments.



More information about the R-help mailing list