Skip to content
Prev 140205 / 398506 Next

Great difference for piecewise linear function between R and SAS

On Mon, Mar 24, 2008 at 8:11 AM, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
Dear Duncan and others:

Can you please refer us to an understandable book that explains about
b-splines?  I've tried a few and the math pretty quickly becomes
unintelligible to a non-math major.

And if this book explains the "orthogonal polynomials" that are used
to represent ordinal factors in R, it would be even better.


Pointing attention back to this poster's original question, I would
say that using bs() here is like hitting a mosquito with a 1000 pound
bomb.  There are easier ways to get the estimates for the slope and
intercept shifts than with bs.

I'd be tempted to take the simplest approach and create a factor to
represent the levels of distance, as in

myDistance <- ifelse( distance > 24, "hi", ifelse(distance > 16.13,
"med", "low"))

### Coerce that to a factor

myDistance <- factor(myDistance)

That creates a factor with three levels, and in the regression

glm (mark~x+poly(elevation,2)+ distance*myDistance ...)

will give the change values for the  intercepts and slopes for the segments.

If you want the coefficients of the 3 separate lines, run it like this instead:


glm (mark~-1 + x+poly(elevation,2)+ myDistance / distance ...)


Also, I'd mention that the package "segmented" is available, but it
has the added feature that it will try to estimate the breakpoints
from the data.


PJ