1

I wish to be able to visualize the rejection area in R for a one- and two-tailed p-value of 0.05 in the following chi-squared density curve:

curve(dchisq(x,24), xlim=c(5,50), ylim=c(0,.06))

It will also be great if anybody has a generic way to do it for other test statistics as well.

Thanks in advance

Tonio

JARO
  • 249
  • 2
  • 12

2 Answers2

6

An approach using ggplot:

df   <- 24
p    <- 0.05
gg   <- data.frame(x=seq(5,50,0.1))
gg$y <- dchisq(gg$x,df)

library(ggplot2)
ggplot(gg) + 
  geom_path(aes(x,y)) +
  geom_linerange(data=gg[gg$x>qchisq(p,df,lower.tail=F),],
                 aes(x, ymin=0, ymax=y),
                 colour="red")

jlhoward
  • 58,004
  • 7
  • 97
  • 140
  • 2
    Nice solution in ggplot2! May I suggest an edit? If you swap `geom_linerange` with `geom_ribbon` and `color=` with `fill=` you have a more ascetically pleasing result. You can also include the `alpha=` argument to change the transparency of the rejection region if you plan on overlapping density plots. – rmbaughman May 02 '14 at 17:27
2
library(HH)
old.omd <- par(omd=c(.05,.88, .05,1))
chisq.setup(df=24)
chisq.curve(df=24, col='blue',alpha=c(0.025,0.025))
chisq.observed(38, df=24)
par(old.omd)
gd047
  • 29,749
  • 18
  • 107
  • 146
  • Nice package. I just was wondering how to place the `ylab` to the left of the plot. – JARO Jan 30 '14 at 08:34