O'Reilly logo

R Statistical Application Development by Example Beginner's Guide by Prabhanjan Narayanachar Tattar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action – fitting piecewise linear regression models

A piecewise linear regression model can be easily fitted in R by using the same lm function and a bit of caution. A loop is used to find the points at which the model is supposed to have changed its trajectory.

  1. Read the dataset into R with data(PW_Illus).
  2. For convenience, attach the variables in the PW_Illus object by using attach(PW_Illus).
  3. To be on the safe side, we will select a range of the x values, which may be either of the breakpoints:
    break1 <- X[which(X>=12 & X<=18)]
    break2 <- X[which(X>=27 & X<=33)]
  4. Get the number of points that are candidates for being the breakpoints with n1 <- length(break1) and n2 <- length(break2).

    We do not have a clear defining criterion to select one of ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required