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 [EMAIL PROTECTED] University of Washington, Seattle ______________________________________________ R-help@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.