3

I have a function that tries to apply a piece-wise regression model to my data. In some cases, the data has a generous amount of missing values and I don't have a good estimator of where the knots will be. I decided to bypass the piece-wise and go for a simple linear regression:

try(piecewise) if error go to lm with just one slope

Here's the code that does it. Note that lin.reg is a helper function that outputs predict() for the lm object in the x range. It does not create any problem.

piece <- function(x,y){

  # just in case this comes with column names or something
  y <- as.numeric(y)


    # create the lm object
      lm.obj <- lm(y~x)
    # Try to fit piecewise
      seg <- try(segmented(lm.obj,seg.Z=~x))

    #  print(seg)

      if("try-error" %in% class(seg)) {

        # Print that you are using a linear regression and not the piece-wise
          print("Using linear Regression")

        # Call helper function
          result <- lin.reg(x,y)

        # Get out of the error/function
        return(result)
        }

      # Use the piece-wise
      result <- predict(segmented::segmented(lm.obj,seg.Z=~x),
                       newdata = data.frame(x,y))
      print("Using piece-wise regression")

  return(result)

}

Problem(s)

I get this error when piece-wise goes wrong

Error: at least one coef is NA: breakpoint(s) at the boundary? (possibly with many x-values replicated)

But it is unreliable/unpredictable, sometimes it gets ignored and sometimes it breaks the function. I am looping over the rows of a data frame with the y values and the same call gets to different rows before braking.

I believe it has to do with the if("try-error" %in% class(seg)) that might not be the best way to catch the error.

I added some printing to make sure. Here's when it works properly, note iteration 284 gave error and went to simple linear.

[1] "Using piece-wise regression"
[1] 283
[1] "segmented" "lm"       
[1] "Using piece-wise regression"
[1] 284
Error : at least one coef is NA: breakpoint(s) at the boundary? (possibly with many x-values replicated)
[1] "try-error"
[1] "Using linear Regression"

And here's when it doesn't, seems like the try() call is not returning error as it should

[1] "Using piece-wise regression"
[1] 312
[1] "segmented" "lm"       
[1] "Using piece-wise regression"
[1] 313
[1] "segmented" "lm"       
  Error: at least one coef is NA: breakpoint(s) at the boundary? (possibly with many x-values replicated) 
Matias Andina
  • 4,029
  • 4
  • 26
  • 58
  • This could be interesting for you: https://stackoverflow.com/questions/12135400/errors-in-segmented-package-breakpoints-confusion – jay.sf Nov 01 '17 at 22:11

1 Answers1

0

Adding the argument silent=T in the try block worked out for me.

  • Welcome to Stack Overflow. This currently seems more like a comment than an answer. Could flesh this out so that the answer includes reproducible code which solves the problem from the original question? – Peter Apr 01 '21 at 06:56