Chi-square Functions and Examples

R has functions for probabilities and quantiles for chi-square (similar to those for the normal). Q for quantiles, p for probabilities, r for random deviates.

qchisq(.95, df=7)        # quantile for 95th percentile with 7 degrees of freedom 
## [1] 14.06714
qchisq(.99, df=7)
## [1] 18.47531
pchisq(7, df=7)          # note the mean of chi-square with 7 df is 7, but because of skew, mean != median
## [1] 0.5711201
pchisq(100, df=100)      # closer to 50 percent because less skewed
## [1] 0.5188083
options(digits=3)        # digits sets the number of significant digits to display; =2 shortens what is shown
rchisq(4,df=3)
## [1] 1.632 1.427 4.218 0.748

Review Question

You have sampled 25 children from an elementary school 5th grade class and measured the height of each. You wonder whether these children are more variable in height than typical children. Their variance in height is 4. Compute a 95 percent confidence interval for this variance. If the variance of height in children in 5th grade nationally is 2, do you consider this sample ordinary?

We want to estimate the population variance and place a confidence interval around it. Then we want to test whether the null hypothesis that the population variance is 2 (it does not matter whether we place the confidence interval about the hypothesized population value or the estimated population value).

V.pop = 2
V.samp = 4
N.samp = 25
df.samp = (N.samp-1)
q025 <-qchisq(.025, df=df.samp)
q975 <- qchisq(.975, df=df.samp)
q025
## [1] 12.4
q975
## [1] 39.4
lb <-(df.samp*V.samp)/q975
ub <- (df.samp*V.samp)/q025
lb
## [1] 2.44
ub
## [1] 7.74

The lower bound is 2.4, so the hypothesized population value of 2 is below the boundary. The sample is not ordinary; i.e., the test is significant.

Slideshow example of confidence intervals for the variance

Suppose N=15 and the estimated variance is 10.

V.samp = 10
N.samp = 15
df.samp = (N.samp-1)
q025 <-qchisq(.025, df=df.samp)
q975 <- qchisq(.975, df=df.samp)
q025
## [1] 5.63
q975
## [1] 26.1
lb <-(df.samp*V.samp)/q975
ub <- (df.samp*V.samp)/q025
lb
## [1] 5.36
ub
## [1] 24.9