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)) 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 two models? Any suggestions? Thanks in advance, David Orme> R.version_ platform ppc-apple-macos arch ppc os MacOS system ppc, MacOS status major 1 minor 5.0 year 2002 month 04 day 29 language R -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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? Any suggestions? Thanks in advance, David Orme> R.version_ platform ppc-apple-macos arch ppc os MacOS system ppc, MacOS status major 1 minor 5.0 year 2002 month 04 day 29 language R -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
try this: ind1 <- as.numeric(x > 10) # to calculate the intercept for this part of the line temp <- lm(y ~ ind1 + x:(x <= 10) - 1)> tempCall: lm(formula = y ~ ind1 + x:(x <= 10) - 1) Coefficients: ind1 x:x <= 10FALSE x:x <= 10TRUE 5.0 0.5 1.0 In model formulae, "*" indicates that the model should include both the main effects terms and the interactions. The logical value (x <= 10) is coerced to a factor (at least effectively). R. Woodrow Setzer, Jr. Phone: (919) 541-0128 Experimental Toxicology Division Fax: (919) 541-5394 Pharmacokinetics Branch NHEERL MD-74; US EPA; RTP, NC 27711 |---------+------------------------------> | | David Orme | | | <d.orme at ic.ac.uk> | | | Sent by: | | | owner-r-help at stat.m| | | ath.ethz.ch | | | | | | | | | 09/04/02 05:54 AM | | | | |---------+------------------------------> >---------------------------------------------------------------------------------------------------------------| | | | To: r-help at stat.math.ethz.ch | | cc: | | Subject: [R] Intercept in model formulae. | >---------------------------------------------------------------------------------------------------------------| 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? Any suggestions? Thanks in advance, David Orme> R.version_ platform ppc-apple-macos arch ppc os MacOS system ppc, MacOS status major 1 minor 5.0 year 2002 month 04 day 29 language R -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. -.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._._._ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
For two-parameter model (only two slopes) use the following equation> coef(lm(y ~ 0+x+pmax(x-10,0)))x pmax(x - 10, 0) 1.0 -0.5 or lm(y ~ x+pmax(x-10,0)-1)). coef(obj)["x"] is the left slope and (coef(obj)["x"]+coef(obj)["pmax(x-10,0)"]) is the right slope. Therefore you might test immediately for the existence of the breakpoint through the coefficient pmax(x-10,0), the differece-in-slope parameter. best, vito ----- Original Message ----- From: "David Orme" <d.orme at ic.ac.uk> To: <r-help at stat.math.ethz.ch> Sent: Friday, August 30, 2002 10:13 AM Subject: [R] Intercept in model formulae.> Hi, > > I'm trying to create a linear model for a dataset that has a breakpointe.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)) > > 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 two models? > > Any suggestions? > > Thanks in advance, > > David Orme > > > R.version > _ > platform ppc-apple-macos > arch ppc > os MacOS > system ppc, MacOS > status > major 1 > minor 5.0 > year 2002 > month 04 > day 29 > language R > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-> r-help mailing list -- Readhttp://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 >_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
dealing more generally with deterministic structural shiftsmore in the context of Classical Linear Regressions, you can define suitable dummy variables and multiply them with the regressors (all or a subset). 'test1' contains two separate slope coefficients, whereas in 'test2' the additional increment in the second subperiod is estimated, hence you have to sum the two estimates to get the overall effect for the second subperiod. x <- 1:20 y <- c(1:10,seq(10.5,15,0.5)) dum1 <- c(rep(1,10),rep(0,10)) dum2 <- c(rep(0,10),rep(1,10)) reg1 <- x*dum1 reg2 <- x*dum2 test1 <- lm(y~-1+reg1+reg2) test2 <- lm(y~-1+x+reg2) summary(test) Bernhard -----Original Message----- From: vito muggeo [mailto:vito.muggeo at giustizia.it] Sent: 11 September 2002 11:24 To: r-help at stat.math.ethz.ch; David Orme Subject: Re: [R] Intercept in model formulae. For two-parameter model (only two slopes) use the following equation> coef(lm(y ~ 0+x+pmax(x-10,0)))x pmax(x - 10, 0) 1.0 -0.5 or lm(y ~ x+pmax(x-10,0)-1)). coef(obj)["x"] is the left slope and (coef(obj)["x"]+coef(obj)["pmax(x-10,0)"]) is the right slope. Therefore you might test immediately for the existence of the breakpoint through the coefficient pmax(x-10,0), the differece-in-slope parameter. best, vito ----- Original Message ----- From: "David Orme" <d.orme at ic.ac.uk> To: <r-help at stat.math.ethz.ch> Sent: Friday, August 30, 2002 10:13 AM Subject: [R] Intercept in model formulae.> Hi, > > I'm trying to create a linear model for a dataset that has a breakpointe.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)) > > 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 two models? > > Any suggestions? > > Thanks in advance, > > David Orme > > > R.version > _ > platform ppc-apple-macos > arch ppc > os MacOS > system ppc, MacOS > status > major 1 > minor 5.0 > year 2002 > month 04 > day 29 > language R > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-> r-help mailing list -- Readhttp://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 >_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. -.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._ If you have received this e-mail in error or wish to read our e-mail disclaimer statement and monitoring policy, please refer to http://www.drkw.com/disc/email/ or contact the sender -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._