How to perform One Sample & Two Sample Z-Tests in R

One sample Z-tests in R can be performed using the qnorm() function to calculate the z-score for a given sample mean and population mean. Two sample Z-tests in R can be performed using the pnorm() function to calculate the z-score for two sample means and a given population mean. Both tests can also be performed using the t.test() function with the corresponding arguments.


You can use the z.test() function from the BSDA package to perform one sample and two sample z-tests in R.

This function uses the following basic syntax:

z.test(x, y, alternative='two.sided', mu=0, sigma.x=NULL, sigma.y=NULL,conf.level=.95)

where:

  • x: values for the first sample
  • y: values for the second sample (if performing a two sample z-test)
  • alternative: the alternative hypothesis (‘greater’, ‘less’, ‘two.sided’)
  • mu: mean under the null or mean difference (in two sample case)
  • sigma.x: population standard deviation of first sample
  • sigma.y: population standard deviation of second sample
  • conf.level: confidence level to use

The following examples shows how to use this function in practice.

Example 1: One Sample Z-Test in R

Suppose the IQ in a certain population is normally distributed with a mean of μ = 100 and standard deviation of σ = 15.

A scientist wants to know if a new medication affects IQ levels, so she recruits 20 patients to use it for one month and records their IQ levels at the end of the month.

The following code shows how to perform a one sample z-test in R to determine if the new medication causes a significant difference in IQ levels:

library(BSDA)

#enter IQ levels for 20 patients
data = c(88, 92, 94, 94, 96, 97, 97, 97, 99, 99,
         105, 109, 109, 109, 110, 112, 112, 113, 114, 115)

#perform one sample z-test
z.test(data, mu=100, sigma.x=15)

	One-sample z-Test

data:  data
z = 0.90933, p-value = 0.3632
alternative hypothesis: true mean is not equal to 100
95 percent confidence interval:
  96.47608 109.62392
sample estimates:
mean of x 
   103.05 

The test statistic for the one sample z-test is 0.90933 and the corresponding p-value is 0.3632.

Since this p-value is not less than .05, we do not have sufficient evidence to reject the null hypothesis.

Thus, we conclude that the new medication does not significantly affect IQ level.

Example 2: Two Sample Z-Test in R

Suppose the IQ levels among individuals in two different cities are known to be normally distributed each with population standard deviations of 15.

A scientist wants to know if the mean IQ level between individuals in city A and city B are different, so she selects a of  20 individuals from each city and records their IQ levels.

library(BSDA)

#enter IQ levels for 20 individuals from each city
cityA = c(82, 84, 85, 89, 91, 91, 92, 94, 99, 99,
         105, 109, 109, 109, 110, 112, 112, 113, 114, 114)

cityB = c(90, 91, 91, 91, 95, 95, 99, 99, 108, 109,
         109, 114, 115, 116, 117, 117, 128, 129, 130, 133)

#perform two sample z-test
z.test(x=cityA, y=cityB, mu=0, sigma.x=15, sigma.y=15)

	Two-sample z-Test

data:  cityA and cityB
z = -1.7182, p-value = 0.08577
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -17.446925   1.146925
sample estimates:
mean of x mean of y 
   100.65    108.80

The test statistic for the two sample z-test is -1.7182 and the corresponding p-value is 0.08577

Since this p-value is not less than .05, we do not have sufficient evidence to reject the null hypothesis.

Thus, we conclude that the mean IQ level is not significantly different between the two cities.

The following tutorials explain how to perform other common statistical tests in R:

x