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.
How do I plot: regression line, regression line + s.d, regression line - s.d on the same chart?
3 messages · Jorgy Porgee, Dimitris Rizopoulos, ONKELINX, Thierry
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
Another option is to use ggplot2 Dataset <- data.frame(x = runif(100, -5, 5)) Dataset$y <- 3 + 2 * Dataset$x + rnorm(100, sd = 2) library(ggplot2) ggplot(Dataset, aes(x = x, y = y)) + geom_smooth(method = "lm") + geom_point() ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx at inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -----Oorspronkelijk bericht----- Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Namens Dimitris Rizopoulos Verzonden: dinsdag 11 augustus 2009 11:24 Aan: Jorgy Porgee CC: r-help at r-project.org Onderwerp: Re: [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 ______________________________________________ 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. Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document. The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.