Tests whether a population mean equals a hypothesized value when the population standard deviation is known.
z_test(x, mu0 = 0, sigma, alternative = c("two.sided", "less", "greater"))A hypothesis_test object of subclass z_test containing:
The z-statistic
The p-value for the specified alternative
Degrees of freedom (Inf for normal distribution)
The alternative hypothesis used
The hypothesized mean \(\mu_0\)
The sample mean \(\bar{x}\)
The known population standard deviation
The sample size
The z-test is one of the simplest and most fundamental hypothesis tests. It tests \(H_0: \mu = \mu_0\) against various alternatives when the population standard deviation \(\sigma\) is known.
Given a sample \(x_1, \ldots, x_n\), the test statistic is:
$$z = \frac{\bar{x} - \mu_0}{\sigma / \sqrt{n}}$$
Under \(H_0\), this follows a standard normal distribution. The p-value depends on the alternative hypothesis:
Two-sided (\(H_1: \mu \neq \mu_0\)): \(2 \cdot P(Z > |z|)\)
Less (\(H_1: \mu < \mu_0\)): \(P(Z < z)\)
Greater (\(H_1: \mu > \mu_0\)): \(P(Z > z)\)
The z-test requires knowing the population standard deviation, which is rare in practice. When \(\sigma\) is unknown and estimated from data, use a t-test instead. The z-test is primarily pedagogical, illustrating the logic of hypothesis testing in its simplest form.
The z-test is a special case of the Wald test (wald_test()) where the
parameter is a mean and the standard error is \(\sigma/\sqrt{n}\).
The Wald test generalizes this to any asymptotically normal estimator.
wald_test() for the general case with estimated standard errors
# A light bulb manufacturer claims bulbs last 1000 hours on average.
# We test 50 bulbs and know from historical data that sigma = 100 hours.
lifetimes <- c(980, 1020, 950, 1010, 990, 1005, 970, 1030, 985, 995,
1000, 1015, 960, 1025, 975, 1008, 992, 1012, 988, 1002,
978, 1018, 965, 1022, 982, 1005, 995, 1010, 972, 1028,
990, 1000, 985, 1015, 968, 1020, 980, 1008, 992, 1012,
975, 1018, 962, 1025, 985, 1002, 988, 1010, 978, 1020)
# Two-sided test: H0: mu = 1000 vs H1: mu != 1000
z_test(lifetimes, mu0 = 1000, sigma = 100)
#> Hypothesis test ( z_test )
#> -----------------------------
#> Test statistic: -0.2545584
#> P-value: 0.7990642
#> Degrees of freedom: Inf
#> Significant at 5% level: FALSE
# One-sided test: are bulbs lasting less than claimed?
z_test(lifetimes, mu0 = 1000, sigma = 100, alternative = "less")
#> Hypothesis test ( z_test )
#> -----------------------------
#> Test statistic: -0.2545584
#> P-value: 0.3995321
#> Degrees of freedom: Inf
#> Significant at 5% level: FALSE