Jorgy Porgee
2009-Aug-11 09:07 UTC
[R] How do I plot: regression line, regression line + s.d, regression line - s.d on the same chart?
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.
Dimitris Rizopoulos
2009-Aug-11 09:24 UTC
[R] How do I plot: regression line, regression line + s.d, regression line - s.d on the same chart?
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
Seemingly Similar Threads
- Is there a way to convert rgb/hex value to a (nearest) color name?
- How do I ensure that the minimum value is always displayed on a y-axis in a plot?
- How do I plot a line followed by two forecast points?
- by (tapply) and for loop differences
- Can someone please explain why the last tick mark on this chart is not showing?