Hi I just realized that when I use linear regression to draw a line through my data points with something like the following: abline(lm(y ~ x)) the length of the line is infinite, i.e., the line goes beyond the smallest and the largest data values. This seems not very right to me (not to mention it looks unaesthetic). I do not mean to imply that the straight-line behavior of my system is maintained throughout. I would like to limit the length of this line to the range of my data. However, I have not been able to figure out how to. Very disconcertingly, I found out that all the books that teach statistics using R seem to be drawing such infinite length-lines. I would appreciate any advice or suggestions. Regards, Tariq [[alternative HTML version deleted]]
Gabor Grothendieck
2008-Jun-19 05:01 UTC
[R] Controlling the length of line with abline(lm())
That's normally how its done so you might want to reconsider whether you really want only the line segment; however, if you insist see ?segments plot(y ~ x) y.lm <- lm(y ~ x) n <- length(x) # x assumed to be in ascending or descending order segments(x[1], fitted(y.lm)[1], x[n], fitted(y.lm)[n]) On Wed, Jun 18, 2008 at 10:09 PM, Tariq Perwez <tariq.perwez at gmail.com> wrote:> Hi > I just realized that when I use linear regression to draw a line through my > data points with something like the following: > > abline(lm(y ~ x)) > > > the length of the line is infinite, i.e., the line goes beyond the smallest > and the largest data values. This seems not very right to me (not to mention > it looks unaesthetic). I do not mean to imply that the straight-line > behavior of my system is maintained throughout. I would like to limit the > length of this line to the range of my data. However, I have not been able > to figure out how to. Very disconcertingly, I found out that all the books > that teach statistics using R seem to be drawing such infinite length-lines. > I would appreciate any advice or suggestions. Regards, > > > Tariq > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
On 19/06/2008, at 2:09 PM, Tariq Perwez wrote:> Hi > I just realized that when I use linear regression to draw a line > through my > data points with something like the following: > > abline(lm(y ~ x)) > > > the length of the line is infinite, i.e., the line goes beyond the > smallest > and the largest data values. This seems not very right to me (not > to mention > it looks unaesthetic). I do not mean to imply that the straight-line > behavior of my system is maintained throughout. I would like to > limit the > length of this line to the range of my data. However, I have not > been able > to figure out how to. Very disconcertingly, I found out that all > the books > that teach statistics using R seem to be drawing such infinite > length-lines. > I would appreciate any advice or suggestions.As things stand, I don't think you can do it in one hit. You have to go through something like fit <- lm(y~x) z <- predict(fit,data.frame(x=range(x))) lines(range(x),z) I guess one could ``automate'' this via something like: fline <- function(object) { # ``fline'' <--> fitted line. r <- range(object$model[,2]) d <- data.frame(r) names(d) <- attr(object$terms,"term.labels") y <- predict(object,d) lines(r,y) } (There may well be a cleverer way to do this .....) And then execute fline(lm(y~x)) Howzat? cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
On Wed, Jun 18, 2008 at 9:09 PM, Tariq Perwez <tariq.perwez at gmail.com> wrote:> Hi > I just realized that when I use linear regression to draw a line through my > data points with something like the following: > > abline(lm(y ~ x)) > > > the length of the line is infinite, i.e., the line goes beyond the smallest > and the largest data values. This seems not very right to me (not to mention > it looks unaesthetic). I do not mean to imply that the straight-line > behavior of my system is maintained throughout. I would like to limit the > length of this line to the range of my data. However, I have not been able > to figure out how to. Very disconcertingly, I found out that all the books > that teach statistics using R seem to be drawing such infinite length-lines. > I would appreciate any advice or suggestions. Regards,ggplot2 only extends the line as far as your data: qplot(x, y) + geom_smooth(method=lm) You can find out more about ggplot2 at http://had.co.nz/ggplot2. Hadley -- http://had.co.nz/
You can use the clipplot function in the TeachingDemos package to limit the range of the line (the 2nd example in the help page shows an example of 3 lines for 3 subgroups of data with each line limited to the x-range of the data). There is also the clip function in the graphics package (no extra packages need to be installed), but it uses a different interface and getting it to work can be tricky sometimes. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Tariq Perwez > Sent: Wednesday, June 18, 2008 8:09 PM > To: r-help at r-project.org > Subject: [R] Controlling the length of line with abline(lm()) > > Hi > I just realized that when I use linear regression to draw a > line through my data points with something like the following: > > abline(lm(y ~ x)) > > > the length of the line is infinite, i.e., the line goes > beyond the smallest and the largest data values. This seems > not very right to me (not to mention it looks unaesthetic). I > do not mean to imply that the straight-line behavior of my > system is maintained throughout. I would like to limit the > length of this line to the range of my data. However, I have > not been able to figure out how to. Very disconcertingly, I > found out that all the books that teach statistics using R > seem to be drawing such infinite length-lines. > I would appreciate any advice or suggestions. Regards, > > > Tariq > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >