[R] How do I plot: regression line, regression line + s.d, regression line - s.d on the same chart?

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Tue Aug 11 11:24:21 CEST 2009


have a look at predict.lm() and specifically at the 'interval' argument 
-- check also the following

x <- runif(100, -5, 5)
y <- 3 + 2 * x + rnorm(100, sd = 2)

fit <- lm(y ~ x)
xx <- seq(min(x), max(x), length.out = 30)
pred <- predict(fit, newdata = data.frame(x = xx), interval = "p")

plot(x, y)
matlines(xx, pred, lty = c(1, 2, 2), col = "red")


I hope it helps.

Best,
Dimitris


Jorgy Porgee wrote:
> Hello all,
> I've got a scatter plot, for which I create a regression line using:
> reg<- lm(yvars~xvars) and can plot the regression through the scatter
> just fine.
> I'd like to add two additional lines on the scatter plot: one being
> regressionline+standard deviation, the other being
> regressionline-standard deviation, thus creating some kind of 'band'
> for the scatter.
> 
> Any help will be much appreciated. Thanking you in advance,
> 
> George.
> 
> ______________________________________________
> 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.
> 

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014




More information about the R-help mailing list