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

From: Greg Snow <Greg.Snow_at_imail.org>
Date: Wed, 2 Jan 2008 10:18:12 -0700

Try:

glm( y ~ x + I( (x-20)*(x>20) ), ...

This will give you a piecewise linear function with a change point at x=20 and the 2 lines meeting at x=20. The intercept and slope on x will represent the line for x <= 20 and the slope on the I(...) term will represent the difference in slope between the previous line and the new slope for x>=20 (so the test on this term tests the null that a single line fits as well as the piecewise linear).

Hope this helps,

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow_at_imail.org
(801) 408-8111
 
 


> -----Original Message-----
> From: r-help-bounces_at_r-project.org
> [mailto:r-help-bounces_at_r-project.org] On Behalf Of zhijie zhang
> Sent: Tuesday, January 01, 2008 8:25 AM
> To: R-help_at_stat.math.ethz.ch
> Subject: [R] Specify a correct formula in R for Piecewise
> Linear Functions?
>
> 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?
> Thanks a lot.
>
> --
> With Kind Regards,
>
> oooO:::::::::
> (..):::::::::
> :\.(:::Oooo::
> ::\_)::(..)::
> :::::::)./:::
> ::::::(_/::::
> :::::::::::::
> [*************************************************************
> **********]
> Zhi Jie,Zhang ,PHD
> Tel:+86-21-54237149
> Dept. of Epidemiology,School of Public Health,Fudan
> University Address:No. 138 Yi Xue Yuan Road,Shanghai,China
> Postcode:200032
> Email:epistat_at_gmail.com
> Website: www.statABC.com
> [*************************************************************
> **********]
> oooO:::::::::
> (..):::::::::
> :\.(:::Oooo::
> ::\_)::(..)::
> :::::::)./:::
> ::::::(_/::::
> :::::::::::::
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
______________________________________________ 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 - 17:22:51 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 - 18:30:04 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