[R] Intercept in model formulae.

Thomas Lumley tlumley at u.washington.edu
Wed Sep 4 16:43:14 CEST 2002


On Wed, 4 Sep 2002, David Orme wrote:

> Hi,
>
> I'm trying to create a linear model for a dataset that has a breakpoint e.g.
>
> # dummy dataset
> x <- 1:20
> y <- c(1:10,seq(10.5,15,0.5))
> plot(x,y)
>
> I've modelled this using the following formula:
>
> temp <- lm(y ~ x*(x<=10)+x*(x>10)) # where 10 is the breakpoint
>
> I want to be able to omit the intercept (i.e. force the line through
> zero) from the first of these segments (x<=10) so that I'm only
> estimating one intercept and two slopes. I've tried including -1 in
> the formula, folllowing the lm(y ~ x -1) syntax  to omit the
> intercept from lm(y ~x) but the linear model still returns an
> estimate of the intercept of the first segment. I can't work out what
> the syntax for the formula should be. Is it possible in one step or
> do I have to fit the two lines as separate models on subsets of the
> data?

I'm not completely clear on whether you want a breakpoint or just a change
in slope (your data just have a change in slope but your example model
doesn't enforce the two lines meeting at x=10))

Assuming you do just want a change in slope you can do

x10<-pmax(x,10)
lm(y~x+x10-1)

The coefficient of x is the slope for x<10, the *sum* of the two
coefficients is the slope for x>10.

	-thomas


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list