If you have a sample $X_1, X_2, \dots, X_n$ from a normal population in vector x
, the procedure t.test
in R will give you a 95% confidence
interval $(49.19, 50.33)$ for the population mean $\mu,$ for a sample of size $n = 20$ from $\mathsf{Norm}(\mu = 50, \sigma=7),$ as shown below.
(No hand computation is needed.)
set.seed(2021)
x = rnorm(20, 50, 7)
t.test(x)$conf.int
[1] 49.19194 56.32578
attr(,"conf.level")
[1] 0.95
This procedure also does a t test, but you can use $
notation to show just the confidence interval.
This confidence interval is of the form $\bar X\pm t^* s/\sqrt{n},$ where $\bar X$ is the sample mean, $s$ is the
sample standard deviation, and $t^*$ cuts probability $0.025$
from the upper tail of Student's t distribution with $n-1 = 19$ degrees of freedom.
a = mean(x) # sample mean
s = sd(x) # sample standard deviation
t.q = qt(.975, 19) # quantiles .025 and .975 of T(19)
a; s; qt(.975,19)
[1] 52.75886
[1] 7.621391
[1] 2.093024
a + qt(c(.025,.975), 19)*s/sqrt(20)
[1] 49.19194 56.32578
Notice that the appropriate quantiles of the (symmetrical)
t distribution are $\pm 2.09,$ whereas the same quantiles
of the standard normal distribution are $\pm 1.96.$ When $n\ge 30,$ the quantiles of $\mathsf{T}(n-1)$ for a 95% confidence interval are not far from $\pm 1.96;$ both quantiles round to $2.0.$ [But this 'rule of 30' does not work quite so well for confidence levels other than 95%. For a 90% CI the normal quantiles are $\pm 1.645\approx \pm 1.6;$ for $n=30$ the t quantiles are $\pm 1.699 \approx \pm 1.7.]$
qnorm(c(.025,.975))
[1] -1.959964 1.959964
If, for some reason, you wanted a 90% or a 99% confidence
interval for $\mu$ in the above example, you could also get
those by using t.test
. Notice that the 99% confidence
interval is the longest of the three CIs given here (90%, 95%, 99%).
t.test(x, conf.level = .90)$conf.int
[1] 49.81208 55.70564
attr(,"conf.level")
[1] 0.9
t.test(x, conf.level = .99)$conf.int
[1] 47.88327 57.63445
attr(,"conf.level")
[1] 0.99
Finally, supposing we did not know that the data x
were
sampled from a normal distribution with $\mu = 50,$ we wanted
to test $H_0: \mu = 50$ against $H_a: \mu \ne 50.$
Here are complete results from t.test
.
t.test(x, mu = 50)
One Sample t-test
data: x
t = 1.6189, df = 19, p-value = 0.122
alternative hypothesis:
true mean is not equal to 50
95 percent confidence interval:
49.19194 56.32578
sample estimates:
mean of x
52.75886
The null hypothesis is not rejected at the 5% level of significance because the P-value $0.122 > 0.05 = 5\%.$
Accordingly, $\mu_0 = 50$ is contained in the 95% CI.