[R] Put a normal curve on plot

2006-10-31 Thread Doran, Harold
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  
day03  
svn rev39566   
language   R   
version.string R version 2.4.0 (2006-10-03)

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Put a normal curve on plot

2006-10-31 Thread David Barron
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 [EMAIL PROTECTED] 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
 day03
 svn rev39566
 language   R
 version.string R version 2.4.0 (2006-10-03)

 [[alternative HTML version deleted]]

 __
 R-help@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

__
R-help@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.


Re: [R] Put a normal curve on plot

2006-10-31 Thread Greg Snow
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
[EMAIL PROTECTED]
(801) 408-8111
 

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Doran, Harold
Sent: Tuesday, October 31, 2006 6:26 AM
To: R-help@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  
day03  
svn rev39566   
language   R   
version.string R version 2.4.0 (2006-10-03)

[[alternative HTML version deleted]]

__
R-help@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.

__
R-help@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.