Suppose x and y are numeric vectors of the same length. plot(x,y) #scatterplot lmObj1 <- lm(y~x) # best fit line abline(lmObj1) # good lmObj2 <- lm(x~y) #get best fit but with axes interchanged abline(lmObj2) # not what I want. I want the correct line, drawn on the same graph, but with # response and predictor variables interchanged One way to proceed would be to extract the intercept and slope from lmObj2 and then do the arithmetic to draw the correct line. I'm hoping for a more streamlined method. Is there one? Thanks David -- View this message in context: nabble.com/reflecting-a-line-tp19422091p19422091.html Sent from the R help mailing list archive at Nabble.com.
Hi David, I don“t know if I got what you are looking for. But see the code below. x<-1:100 y<-x+(runif(100)*x) plot(y~x) mymod<-glm(y~x) my.coefs<-coef(mymod) my.coefs curve(my.coefs[1]+my.coefs[2]*x, lwd=2, col="red", add=T) Cheers, mitinho astronauta brazil On Wed, Sep 10, 2008 at 6:06 PM, David Epstein <David.Epstein@warwick.ac.uk>wrote:> > Suppose x and y are numeric vectors of the same length. > > plot(x,y) #scatterplot > lmObj1 <- lm(y~x) # best fit line > abline(lmObj1) # good > lmObj2 <- lm(x~y) #get best fit but with axes interchanged > abline(lmObj2) # not what I want. I want the correct line, drawn on the > same > graph, but with > # response and predictor variables interchanged > > One way to proceed would be to extract the intercept and slope from lmObj2 > and then do the arithmetic to draw the correct line. I'm hoping for a more > streamlined method. Is there one? > > Thanks > David > -- > View this message in context: > nabble.com/reflecting-a-line-tp19422091p19422091.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of David Epstein > Sent: Wednesday, September 10, 2008 1:06 PM > To: r-help at r-project.org > Subject: [R] re flecting a line > > > Suppose x and y are numeric vectors of the same length. > > plot(x,y) #scatterplot > lmObj1 <- lm(y~x) # best fit line > abline(lmObj1) # good > lmObj2 <- lm(x~y) #get best fit but with axes interchanged > abline(lmObj2) # not what I want. I want the correct line, > drawn on the same > graph, but with > # response and predictor variables interchanged > > One way to proceed would be to extract the intercept and > slope from lmObj2 > and then do the arithmetic to draw the correct line. I'm > hoping for a more > streamlined method. Is there one? > > Thanks > DavidIs something like this what you are looking for? x <- 1:100 y <- 10 + x + 10*rnorm(100) plot(y~x) fit1.lm <- lm(y~x) abline(fit1.lm, col='blue') fit2.lm <- lm(x~y) abline(-coef(fit2.lm)[1]/coef(fit2.lm)[2], 1/coef(fit2.lm)[2], col='red') Hope this is helpful, Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204
Try this: lines(fitted(lm(x ~ y)), y, lty = 2) On Wed, Sep 10, 2008 at 4:06 PM, David Epstein <David.Epstein at warwick.ac.uk> wrote:> > Suppose x and y are numeric vectors of the same length. > > plot(x,y) #scatterplot > lmObj1 <- lm(y~x) # best fit line > abline(lmObj1) # good > lmObj2 <- lm(x~y) #get best fit but with axes interchanged > abline(lmObj2) # not what I want. I want the correct line, drawn on the same > graph, but with > # response and predictor variables interchanged > > One way to proceed would be to extract the intercept and slope from lmObj2 > and then do the arithmetic to draw the correct line. I'm hoping for a more > streamlined method. Is there one? > > Thanks > David > -- > View this message in context: nabble.com/reflecting-a-line-tp19422091p19422091.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Gabor Grothendieck > Sent: Wednesday, September 10, 2008 3:45 PM > To: David Epstein > Cc: r-help at r-project.org > Subject: Re: [R] re flecting a line > > Try this: > > lines(fitted(lm(x ~ y)), y, lty = 2) > >That is certainly more clear than what I posted. Nice solution Gabor. Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204