Re: [R] Specify a correct formula in R for Piecewise Linear Functions?

From: Thomas Lumley <tlumley_at_u.washington.edu>
Date: Wed, 2 Jan 2008 07:58:46 -0800 (PST)

On Tue, 1 Jan 2008, Charles C. Berry wrote:
> On Tue, 1 Jan 2008, zhijie zhang wrote:
>
>> Dear all,
>> I have two variables, y and x. It seems that the relationship between them
>> is Piecewise Linear Functions. The cutpoint is 20. That is, when x<20, there
>> is a linear relationship between y and x; while x>=20, there is another
>> different linear relationship between them.
>> How can i specify their relationships in R correctly?
>> # glm(y~I(x<20)+I(x>=20),family = binomial, data = point) something like
>> this?
>
> Try this:
>
>> library(splines)
>> fit <- glm( y ~ bs( x, deg=1, knots=20 ), family=binomial)
>

In the linear case I would actually argue that there is a benefit from constructing the spline basis by hand, so that you know what the coefficients mean. (For quadratic and higher order splines I agree that pre-existing code for the B-spline basis makes a lot more sense).

For example, in

   fit <- glm( y ~ pmax(x,20)+pmin(x,20), family=binomial) the coefficients are the slope when is < 20 and the slope when x>20.

         -thomas

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley_at_u.washington.edu	University of Washington, Seattle

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 02 Jan 2008 - 16:00:34 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 02 Jan 2008 - 17:30:05 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive