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"))

Arguments

x

Numeric vector. The sample data.

mu0

Numeric. The hypothesized population mean under \(H_0\). Default is 0.

sigma

Numeric. The known population standard deviation.

alternative

Character. Type of alternative hypothesis: "two.sided" (default), "less", or "greater".

Value

A hypothesis_test object of subclass z_test containing:

stat

The z-statistic

p.value

The p-value for the specified alternative

dof

Degrees of freedom (Inf for normal distribution)

alternative

The alternative hypothesis used

null_value

The hypothesized mean \(\mu_0\)

estimate

The sample mean \(\bar{x}\)

sigma

The known population standard deviation

n

The sample size

Details

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)\)

When to Use

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.

Relationship to Wald Test

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.

See also

wald_test() for the general case with estimated standard errors

Examples

# 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