gr <- c(1.66, 1.61, 1.62, 1.69, 1.58, 1.43, 1.66,
1.69, 1.58, 1.20, 1.52, 1.60, 1.55, 1.67,
1.77, 1.50, 1.64, 1.54, 1.40, 1.36, 1.50,
1.40, 1.35, 1.48, 1.64, 1.91, 1.70)STAT 516 Lec 01 (accessible)
Inference on the mean and variance of a Normal population
Setup
Throughout let
In this lecture we review how to:
- Estimate
and . - Build confidence intervals for
and . - Test hypotheses concerning
and . - Choose the sample size.
We call
Golden ratio example:
A class of
What is the true mean of
Check if
qqnorm(scale(gr))
abline(0,1)Estimation
Based on
Then
Golden ratio example (cont):
We have mean(gr) = 1.565 and var(gr) = 0.0219.
hist(gr)Important sampling distribution results
Provided
Discuss: Anatomy of chi-square and t random variables
. .
Relate these to the results on the previous slide.
Simulation illustrating sampling distribution results:
sims <- 1000
mu <- 1
sigma <- 1/2
n <- 8
Tn <- numeric(sims)
Wn <- numeric(sims)
for(s in 1:sims){
X <- rnorm(n,mu,sigma)
sn <- sd(X)
xbar <- mean(X)
Tn[s] <- sqrt(n)*(xbar - mu) / sn
Wn[s] <- (n-1)*sn^2 / sigma^2
}hist(Tn,freq = FALSE,breaks = 50)
x <- seq(-4,4,length = 500)
lines(dt(x,n-1)~x)hist(Wn,freq = FALSE,breaks = 50)
x <- seq(0,max(Wn),length = 500)
lines(dchisq(x,n-1)~x)Confidence intervals for the mean and variance
The sampling distribution results give
for . for .
Exercise: Derive the above.
Golden ratio example (cont):
Build 95% CIs for population mean and variance of
alpha <- 0.05
n <- length(gr)
lomu <- mean(gr) - qt(1-alpha/2,n-1) * sd(gr)/sqrt(n)
upmu <- mean(gr) + qt(1-alpha/2,n-1) * sd(gr)/sqrt(n)
losgs <- (n-1) * var(gr) / qchisq(1-alpha/2,n-1)
upsgs <- (n-1) * var(gr) / qchisq(alpha/2,n-1)The 95% CI for
Testing hypotheses about the mean
Consider testing hypotheses about
The corresponding p-values are, with
Golden ratio example (cont):
Test
alpha <- 0.05
Tstat <- (mean(gr) - 1.618) / (sd(gr) / sqrt(n))
abs(Tstat) > qt(1-alpha/2,n-1)[1] FALSE
Fail to reject
pval <- 2*(1 - pt(abs(Tstat),n-1))Equivalently, the p-value, which is 0.073, is greater than
The t.test() function in R
The function t.test() tests
t.test(gr)
One Sample t-test
data: gr
t = 54.902, df = 26, p-value < 2.2e-16
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
1.506228 1.623401
sample estimates:
mean of x
1.564815
The t.test() function in R
Now test
t.test(gr, mu = 1.618, conf.level = 0.99)
One Sample t-test
data: gr
t = -1.866, df = 26, p-value = 0.07336
alternative hypothesis: true mean is not equal to 1.618
99 percent confidence interval:
1.485616 1.644013
sample estimates:
mean of x
1.564815
The t.test() function in R
Now test
t.test(gr, mu = 1.618, alternative = "greater")
One Sample t-test
data: gr
t = -1.866, df = 26, p-value = 0.9633
alternative hypothesis: true mean is greater than 1.618
95 percent confidence interval:
1.516202 Inf
sample estimates:
mean of x
1.564815
Testing hypotheses about the variance
Consider testing hypotheses about
The corresponding p-values are, with
Golden ratio example (cont):
Test
alpha <- 0.05
Wstat <- (n-1)*var(gr) / 0.03
Wstat < qchisq(alpha,n-1)[1] FALSE
FTR
pval <- pchisq(Wstat,n-1)Equivalently, the p-value, which is 0.164, is greater than
Pallet weights example
The weights in lbs of several wooden pallets were recorded.
wt <- c(41,34,40,44,33,42,52,38,32,31,31,35,
39,44,42,42,35,33,40,48,51,32,41,35,
38,48,37,35,42,41,40,47,40,46,33,38,
51,39,40)Pallet weights example continued
- Check if we can assume normally distributed pallet weights.
- Build a 95% CI for the mean pallet weight assuming
. - Build a 95% CI for the mean pallet weight assuming
is unknown. - Test at
whether the mean pallet weight is equal to lbs. - Test at
whether the mean pallet weight is less than lbs.
Sample size calculations
We can choose a sample size based on the desired:
- Width of a confidence interval.
- Power of a test to reject
when it is false.
Sample size required to achieve desired CI width
A CI for
if is known if is unknown
For ease, use the “
If one wants
So take
Must put in a guess for
Golden ratio example (cont):
Find
alpha <- 0.05
M <- 0.08/2
sigma_guess <- sd(gr)
nr <- ceiling((qnorm(1-alpha/2) * sigma_guess / M)^2)
nr[1] 53
Sample size required to achieve desired power
The power of a test is the probability with which it rejects
For tests of
So the power depends on the true value of
Exercise: Derive the power functions for the tests of
Plot of power curves for right-, left-, and two-sided tests
Set
alpha <- 0.05
mu0 <- 1.618
sigma <- sd(gr)
n <- length(gr)
mu <- seq(mu0-4*sigma/sqrt(n),mu0+4*sigma/sqrt(n),length=500)
za <- qnorm(1-alpha)
za2 <- qnorm(1-alpha/2)
d <- sqrt(n) * (mu - mu0) / sigma
rp <- 1 - pnorm(za - d)
lp <- pnorm(-za - d)
rp2 <- 1 - pnorm(za2 - d)
lp2 <- pnorm(-za2 - d)
tsp <- lp2 + rp2plot(rp ~ mu, type = "l", ylab = "power", xlab = "mu")
lines(lp ~ mu, lty =2)
lines(tsp ~ mu, lty = 3)Power curve for right-sided test at various sample sizes
Test
alpha <- 0.05
mu0 <- 1.618
sigma <- sd(gr)
n <- length(gr)
mu <- seq(mu0-1*sigma/sqrt(n),mu0+4*sigma/sqrt(n),length=500)
za <- qnorm(1-alpha)
# various sample sizes
nn <- c(10,20,30,40,50,60)
rp <- matrix(NA,500,length(nn))
for(j in 1:length(nn)){
d <- sqrt(nn[j]) * (mu - mu0) / sigma
rp[,j] <- 1 - pnorm(za - d)
}plot(NA,xlim = range(mu), ylim = c(0,1), ylab = "power", xlab = "mu")
for(j in 1:length(nn)) lines(rp[,j] ~ mu, lty = j)
legend(x = min(mu), y = 1,legend = paste("n =",nn),lty = 1:length(nn),bty = "n")Sample size based on desired power
To find the smallest sample size guaranteeing a desired power:
- Fix an alternative value
and a desired power . - Set up the equation
and solve for (then round up).
For our tests concerning
- In the one-sided case
. - In the two-sided case
.
In the above
Exercise: Derive the sample size formula for the test of
Golden ratio example (cont):
Suppose the true mean of
Give the sample size
alpha <- 0.05
gm <- 0.80
sigma <- sd(gr)
mu <- 1.65
mu0 <- 1.618
za <- qnorm(1 - alpha)
zb <- qnorm(gm)
nr <- ceiling(sigma^2 * (za + zb)^2 / (mu - mu0)^2)
nr[1] 133