I would like to be able to place a normal distribution surrounding the predicted values at various places on a plot. Below is some toy code that creates a scatterplot and plots a regression line through the data. library(MASS) mu <- c(0,1) Sigma <- matrix(c(1,.8,.8,1), ncol=2) set.seed(123) x <- mvrnorm(50,mu,Sigma) plot(x) abline(lm(x[,2] ~ x[,1])) Say I want to add a normal distribution surrounding the predicted values at the x-values of -1, 0, and 1. That is, at the points \hat{y} = \mu + \beta_1(-1) \hat{y} = \mu + \beta_1(0) \hat{y} = \mu + \beta_1(1) How might I go about doing this? Harold> version_ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 4.0 year 2006 month 10 day 03 svn rev 39566 language R version.string R version 2.4.0 (2006-10-03) [[alternative HTML version deleted]]
Something like this? library(MASS) mu <- c(0,1) Sigma <- matrix(c(1,.8,.8,1), ncol=2) set.seed(123) x <- mvrnorm(50,mu,Sigma) dta <- data.frame(x=x[,1],y=x[,2]) plot(x) fit <- lm(y~x, data=dta) sfit <- summary(fit) se <- sfit$sigma abline(fit) yhat <- predict(fit,data.frame(x=c(-1,0,1)),se.fit=TRUE) x1 <- seq(-2,0,length=50) x2 <- x1 + 1 x3 <- x2 + 1 lines(x1,yhat$fit[1]+dnorm(x1,-1,se)) lines(x2,yhat$fit[2]+dnorm(x2,0,se)) lines(x3,yhat$fit[3]+dnorm(x3,1,se)) On 31/10/06, Doran, Harold <HDoran at air.org> wrote:> I would like to be able to place a normal distribution surrounding the > predicted values at various places on a plot. Below is some toy code > that creates a scatterplot and plots a regression line through the data. > > library(MASS) > mu <- c(0,1) > Sigma <- matrix(c(1,.8,.8,1), ncol=2) > set.seed(123) > x <- mvrnorm(50,mu,Sigma) > plot(x) > abline(lm(x[,2] ~ x[,1])) > > Say I want to add a normal distribution surrounding the predicted values > at the x-values of -1, 0, and 1. That is, at the points > > \hat{y} = \mu + \beta_1(-1) > \hat{y} = \mu + \beta_1(0) > \hat{y} = \mu + \beta_1(1) > > How might I go about doing this? > > Harold > > > version > _ > platform i386-pc-mingw32 > arch i386 > os mingw32 > system i386, mingw32 > status > major 2 > minor 4.0 > year 2006 > month 10 > day 03 > svn rev 39566 > language R > version.string R version 2.4.0 (2006-10-03) > > [[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. >-- ================================David Barron Said Business School University of Oxford Park End Street Oxford OX1 1HP
Here is some sample code that shows 2 different approaches, which you want to use depends on what exactly you want to do and which will be easier for you, the one draws the normal around the 6,7 point, the other around the 3,4 point, change those values to do your plot: plot(1:10, 2:11) xx <- seq(-3,3,.1) lines( 6+dnorm(xx,0,.75), 7+xx ) library(TeachingDemos) subplot( plot(dnorm(xx),xx, type='l', axes=F,xlab='',ylab=''), 3,4, hadj=0 ) Hope this helps, -- 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 Doran, Harold Sent: Tuesday, October 31, 2006 6:26 AM To: R-help at stat.math.ethz.ch Subject: [R] Put a normal curve on plot I would like to be able to place a normal distribution surrounding the predicted values at various places on a plot. Below is some toy code that creates a scatterplot and plots a regression line through the data. library(MASS) mu <- c(0,1) Sigma <- matrix(c(1,.8,.8,1), ncol=2) set.seed(123) x <- mvrnorm(50,mu,Sigma) plot(x) abline(lm(x[,2] ~ x[,1])) Say I want to add a normal distribution surrounding the predicted values at the x-values of -1, 0, and 1. That is, at the points \hat{y} = \mu + \beta_1(-1) \hat{y} = \mu + \beta_1(0) \hat{y} = \mu + \beta_1(1) How might I go about doing this? Harold> version_ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 4.0 year 2006 month 10 day 03 svn rev 39566 language R version.string R version 2.4.0 (2006-10-03) [[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.