[R-sig-teaching] Graph critical region

hadley wickham h.wickham at gmail.com
Wed Jun 24 04:58:20 CEST 2009


On Tue, Jun 23, 2009 at 9:44 PM, Ista Zahn<istazahn at gmail.com> wrote:
> I've seen quicker ways to do it, but I've been using ggplot2 for this.
> See examples below.
>
> On Tue, Jun 23, 2009 at 7:57 PM, AbouEl-Makarim
> Aboueissa<aaboueissa at usm.maine.edu> wrote:
>> Dear All:
>>
>> I need some help with graphing the critical regions of hypothesis testing.
>>
>> Consider the Z-test, assume Z=3.2, and alpha=0.05 (that is Z_(a.pha/2)=1.96.
>>
>> Then how to graph the rejection region in the following three cases:
>>
>> Left-tailed test
>
> library(ggplot2)
> z <- seq(-4,4, by = .01)
> Density <- dnorm(z)
> criteria <- factor(rep("retain", length(z)), levels=c("retain", "reject"))
> criteria[which(z < qnorm(.05))] <- "reject"
> qplot(z,Density, geom=c("path","area"), fill=criteria) +
> scale_fill_manual(values=c("grey40", "red"))

Here's how I'd do it.  I don't know if it's much of an improvement,
but at least the final example shows you how to do the two-tailed
case:

library(ggplot2)
df <- data.frame(z = seq(-4, 4, length = 100))

base <- qplot(z, dnorm(z), data = df, geom = "area")

base + geom_area(data = subset(df, z < qnorm(0.05)), fill = "red")
base + geom_area(data = subset(df, z > qnorm(0.95)), fill = "red")
base + geom_area(aes(group = sign(z)),
  data = subset(df, abs(z) > qnorm(1 - 0.05 / 2)), fill = "red")

-- 
http://had.co.nz/




More information about the R-sig-teaching mailing list