[R] Great difference for piecewise linear function between R and SAS

Paul Johnson pauljohn32 at gmail.com
Mon Mar 24 19:09:35 CET 2008


On Mon, Mar 24, 2008 at 8:11 AM, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
> On 3/24/2008 9:03 AM, zhijie zhang wrote:
>  > Dear  Murdoch,
>
> >   Thanks very
>
>  I would use predict() instead.  What you have there doesn't look as
>  though it uses the B-spline basis.
>
>  The reference given in the ?bs help page is a reasonable starting point,
>  but just about any book that covers splines should handle the B-spline
>  basis and the linear case.
>
>  Duncan Murdoch
>

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


-- 
Paul E. Johnson
Professor, Political Science
1541 Lilac Lane, Room 504
University of Kansas



More information about the R-help mailing list