Hi.. I'm not sure why polygon returns an area above the standard normal curve. z <- pretty(c(-3,3), 100) ht <- dnorm(z) data <- data.frame(z=z, ht=ht) zc <- 1.645 plot(data, type="l") lines(data) t <- subset(data, z>zc) polygon(t, col="red") Thanks, Lance [[alternative HTML version deleted]]
polygon expects to be given the vertices, so if you want the area under the curve you'll want to include vertices at density=0 z <- pretty(c(-3,3), 100) ht <- dnorm(z) plot(z,ht, type="l") zc <- 1.645 ht<-ht[z>zc] z<-z[z>zc] ht<-c(0,ht,0) z<-c(z[1],z,z[length(z)]) polygon(z,ht,col='red') On 26-May-07, at 7:34 AM, LL wrote:> Hi.. I'm not sure why polygon returns an area above the standard > normal curve. > > z <- pretty(c(-3,3), 100) > ht <- dnorm(z) > data <- data.frame(z=z, ht=ht) > zc <- 1.645 > plot(data, type="l") > lines(data) > t <- subset(data, z>zc) > polygon(t, col="red") > > Thanks, > Lance > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.-- Mike Lawrence Graduate Student, Department of Psychology, Dalhousie University Website: myweb.dal.ca/mc973993 Public calendar: icalx.com/public/informavore/Public "The road to wisdom? Well, it's plain and simple to express: Err and err and err again, but less and less and less." - Piet Hein
Ok.. I see that I did not completely define the polygon. The code below works fine z <- pretty(c(-3,3), 100) ht <- dnorm(z) data <- data.frame(z=z, ht=ht) zc <- 1.645 plot(data, type="n") lines(data) #segments(160,0,160,dnorm(160, m=160, sd=17), lty=2) t <- subset(data, data$z>zc) xvals <- t$z dvals <- t$ht polygon(c(xvals,rev(xvals)),c(rep(0,length(xvals)),rev(dvals)),col="red") ----- Original Message ----- From: LL To: r-help@stat.math.ethz.ch Sent: Saturday, May 26, 2007 12:34 PM Subject: polygon error? Hi.. I'm not sure why polygon returns an area above the standard normal curve. z <- pretty(c(-3,3), 100) ht <- dnorm(z) data <- data.frame(z=z, ht=ht) zc <- 1.645 plot(data, type="l") lines(data) t <- subset(data, z>zc) polygon(t, col="red") Thanks, Lance [[alternative HTML version deleted]]