FMH
2009-Oct-21 17:09 UTC
[R] How to find the interception point of two linear fitted model in R?
Dear All, Let have 10 pair of observations, as shown below. ###################### x <- 1:10 y <- c(1,3,2,4,5,10,13,15,19,22) plot(x,y) ###################### Two fitted? models, with ranges of [1,5] and [5,10],?can be easily fitted separately by lm function as shown below: ####################### mod1 <- lm(y[1:5] ~ x[1:5]) mod2 <- lm(y[5:10] ~ x[5:10]) ####################### I'm looking for the interception points between these two straight lines from these fitted models, mod1 and mod2. Could someone advice me the way to do it? Thank you Fir?
Paul Johnson
2009-Oct-21 17:18 UTC
[R] How to find the interception point of two linear fitted model in R?
On Wed, Oct 21, 2009 at 12:09 PM, FMH <kagba2006 at yahoo.com> wrote:> Dear All, > > Let have 10 pair of observations, as shown below. > > ###################### > x <- 1:10 > y <- c(1,3,2,4,5,10,13,15,19,22) > plot(x,y) > ###################### > > > Two fitted? models, with ranges of [1,5] and [5,10],?can be easily fitted separately by lm function as shown below: > > ####################### > mod1 <- lm(y[1:5] ~ x[1:5]) > mod2 <- lm(y[5:10] ~ x[5:10]) > ####################### > > I'm looking for the interception points between these two straight lines from these fitted models, mod1 and mod2. > > Could someone advice me the way to do it? > > Thank you > Firextract the coefficients, rearrange in a linear system, use solve.> > m1 <- as.matrix(rbind(coef(mod1), coef(mod2))) > a <- cbind(c(1,1), -m1[, 2]) > a[,1] [,2] [1,] 1 -0.900000 [2,] 1 -3.257143> b <- m1[,1] > solve(a=a,b=b)[1] 4.396364 4.551515 This seems to "visually verify" the result:> plot(1:10,1:10, type="n") > abline(mod1) > abline(mod2)-- Paul E. Johnson Professor, Political Science 1541 Lilac Lane, Room 504 University of Kansas