[R] Put a normal curve on plot
Greg Snow
Greg.Snow at intermountainmail.org
Tue Oct 31 23:06:53 CET 2006
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.
More information about the R-help
mailing list