Dear R helpers, I would like to have abline, for a lm model for example, lying within data range. Do you know how to get it? Thank in advance Nguyen D Nguyen #CODE x<- rnorm(200, 35,5) y<- rnorm(200, 0.87,0.12) plot(y~x, xlim=c(0,50), pch=17, bty="l") abline(lm(y~x)) # I would like abline is between min(x) and max(x) [[alternative HTML version deleted]]
try this:
x <- rnorm(200, 35, 5)
y <- rnorm(200, 0.87, 0.12)
###########
lmObj <- lm(y ~ x)
xs <- range(x)
ys <- predict(lmObj, newdata = data.frame(x = xs))
plot(x, y, pch = 17, bty = "l")
lines(xs, ys, col = "red", lty = 2, lwd = 2)
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Nguyen Dinh Nguyen" <n.nguyen at garvan.org.au>
To: <r-help at stat.math.ethz.ch>
Sent: Tuesday, March 20, 2007 12:32 PM
Subject: [R] abline within data range
> Dear R helpers,
>
> I would like to have abline, for a lm model for
> example, lying within data range. Do you know how to
> get it?
>
> Thank in advance
>
> Nguyen D Nguyen
>
> #CODE
> x<- rnorm(200, 35,5)
> y<- rnorm(200, 0.87,0.12)
> plot(y~x, xlim=c(0,50), pch=17, bty="l")
> abline(lm(y~x))
>
> # I would like abline is between min(x) and max(x)
>
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Nguyen Dinh Nguyen wrote:> Dear R helpers, > > I would like to have abline, for a lm model for > example, lying within data range. Do you know how to > get it? > > Thank in advance > > Nguyen D Nguyen > > #CODE > x<- rnorm(200, 35,5) > y<- rnorm(200, 0.87,0.12) > plot(y~x, xlim=c(0,50), pch=17, bty="l") > abline(lm(y~x)) > > # I would like abline is between min(x) and max(x)Just make it yourself as in: newdat <- data.frame(x = range(x)) pred <- predict(lm(y ~ x), newdata = newdat) lines(newdat$x, pred) Uwe Ligges> > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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.
Dear Dimitris,
It works beautifully!
Many thanks
Rhelpers are great!
All the best,
Nguyen
try this:
x <- rnorm(200, 35, 5)
y <- rnorm(200, 0.87, 0.12)
###########
lmObj <- lm(y ~ x)
xs <- range(x)
ys <- predict(lmObj, newdata = data.frame(x = xs))
plot(x, y, pch = 17, bty = "l")
lines(xs, ys, col = "red", lty = 2, lwd = 2)
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Nguyen Dinh Nguyen" <n.nguyen@garvan.org.au>
To: <r-help@stat.math.ethz.ch>
Sent: Tuesday, March 20, 2007 12:32 PM
Subject: [R] abline within data range
> Dear R helpers,
>
> I would like to have abline, for a lm model for
> example, lying within data range. Do you know how
to> get it?
>
> Thank in advance
>
> Nguyen D Nguyen
>
> #CODE
> x<- rnorm(200, 35,5)
> y<- rnorm(200, 0.87,0.12)
> plot(y~x, xlim=c(0,50), pch=17, bty="l")
> abline(lm(y~x))
>
> # I would like abline is between min(x) and max(x)
[[alternative HTML version deleted]]
One approach is to use the clipplot function from the TeachingDemos package. This is probably overkill to load the package for doing this just once, but if you are creating a more complicated graph with several lines limited by their range then this may be of interest. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Nguyen > Dinh Nguyen > Sent: Tuesday, March 20, 2007 5:32 AM > To: r-help at stat.math.ethz.ch > Subject: [R] abline within data range > > Dear R helpers, > > I would like to have abline, for a lm model for example, > lying within data range. Do you know how to get it? > > Thank in advance > > Nguyen D Nguyen > > #CODE > x<- rnorm(200, 35,5) > y<- rnorm(200, 0.87,0.12) > plot(y~x, xlim=c(0,50), pch=17, bty="l") > abline(lm(y~x)) > > # I would like abline is between min(x) and max(x) > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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 3/20/07, Nguyen Dinh Nguyen <n.nguyen at garvan.org.au> wrote:> Dear R helpers, > > I would like to have abline, for a lm model for > example, lying within data range. Do you know how to > get it? > > Thank in advance > > Nguyen D Nguyen > > #CODE > x<- rnorm(200, 35,5) > y<- rnorm(200, 0.87,0.12) > plot(y~x, xlim=c(0,50), pch=17, bty="l") > abline(lm(y~x))This is really easy with ggplot: install.packages("ggplot") library(ggplot) qplot(x,y, type=c("point","smooth"), method=lm) or, if you don't want the standard errors qplot(x,y, type=c("point","smooth"), method=lm, se=F) Hadley