[R] Specify a correct formula in R for Piecewise Linear Functions?
Thomas Lumley
tlumley at u.washington.edu
Wed Jan 2 16:58:46 CET 2008
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
More information about the R-help
mailing list