rpiecewise

Changing arguments in seg.control() while maintaining psi = NA


I have a dataset that I'm trying to fit a piecewise linear regression model using segmented(). I wanted to leave the psi = NA because I have several "transects" of data that require a piecewise model to be fitted. The trouble is that I keep getting one of two errors:

Error in segmented.lm(transect.lm, seg.Z = ~distance, psi = NA, control = seg.control(n.boot = 30,  : 
  only 1 datum in an interval: breakpoint(s) at the boundary or too close each other
Error: at least one coef is NA: breakpoint(s) at the boundary? (possibly with many x-values replicated)

I want to leave psi as NA. Can anything within seg.control be changed to prevent the error?

Here is I put in for the segmented model:

transect.lm <- lm(elevation~length, data = data2)
transect.pw <- segmented(transect.lm, 
                          seg.Z = ~length, 
                          psi = NA, 
                          control = seg.control(
                            n.boot = 30, 
                            h = 0.001, 
                            stop.if.error = FALSE))

Solution

  • I found a post in R-Bloggers that talked about how to manage errors in for loops. The part of the for loop that was giving me trouble can be within a try() command. Within the article, R would just skip over it and continue on its analysis, but I found with my for loop, it would cycle back and try again, so it ended up looking like this:

    transect.lm <- lm(elevation~length, data = data2)
    try(transect.pw <- segmented(transect.lm, 
                              seg.Z = ~length, 
                              psi = NA, 
                              control = seg.control(
                                n.boot = 30, 
                                h = 0.001)))
    

    Errors would still be printed to let me know something wrong happened, but it would loop back and try again until a model was fitted, and then move to the next transect.

    Thanks for everyone's help!