zhijie zhang
2008-Jan-01 15:24 UTC
[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@gmail.com Website: www.statABC.com [***********************************************************************] oooO::::::::: (..)::::::::: :\.(:::Oooo:: ::\_)::(..):: :::::::)./::: ::::::(_/:::: ::::::::::::: [[alternative HTML version deleted]]
Charles C. Berry
2008-Jan-01 18:43 UTC
[R] Specify a correct formula in R for Piecewise Linear Functions?
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)HTH, Chuck> 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
Thomas Lumley
2008-Jan-02 15:58 UTC
[R] Specify a correct formula in R for Piecewise Linear Functions?
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
Greg Snow
2008-Jan-02 17:18 UTC
[R] Specify a correct formula in R for Piecewise Linear Functions?
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. >
Seemingly Similar Threads
- Generating series of distributions with the same skewness and different kurtosis or with same kurtosis and different skewness?
- handle dates in R?
- Anybody has ever met the problem to add a legend to a figure generated by image()?
- How to add legend for image()?
- help on ROC analysis