[R] Put a normal curve on plot
David Barron
mothsailor at googlemail.com
Tue Oct 31 18:44:38 CET 2006
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
More information about the R-help
mailing list