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

Greg Snow Greg.Snow at imail.org
Wed Jan 2 18:18:12 CET 2008


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.
> 




More information about the R-help mailing list