Two-Sample Z Test in R: Short Guide to Proportions and Means

In this post, you will learn how to perform a two-sample z test in R. A two-sample z test is a statistical test we can use to examine whether two population means or proportions differ significantly, depending on the type of test we use.

In a previous post, we looked at the basics of the z test in R, including one-sample and two-sample tests. Here, we will focus on how to carry out a two-sample z test in R.

Two-Sample Z Test for Proportions in R

We can use a two-sample z test for proportions in R to examine whether the proportion of a particular outcome differs between two independent groups. For example, we might want to compare the proportion of people who support a policy in two different groups. For this test, we need sufficiently large samples for the normal approximation to be appropriate.

Let’s say that we conducted two surveys and obtained the following results:

  • Group A: 60 out of 100 participants supported a new policy.
  • Group B: 72 out of 120 participants supported the same policy.

Now we want to test whether support differs significantly between the two groups. We can do this using a two-sample z test for proportions in R.

two sample z test for proportions in r

Carrying out the Two-Sample Z test

Here is how to carry out a two-sample Z test in R:

# Run two-sample z-test for proportions
prop.test(x, n, correct = FALSE)Code language: PHP (php)

In the code chunk above, we have combined the number of successes and the sample sizes into vectors x and n. Then we used prop.test() with correct = FALSE to run the test for proportions.

Two-Sample Z Test for Means in R

We can also use a two-sample z test in R to compare the means of two independent groups. For this test, we assume that the population standard deviations are known. In practice, however, we often do not know these values, and a two-sample t-test is usually more appropriate.

For this example, let’s assume that we have two groups with the following values:

  • Group A: Mean = 100, population standard deviation = 15, sample size = 50.
  • Group B: Mean = 105, population standard deviation = 15, sample size = 60.

Now, we want to test whether the two population means are equal. That is, we want to know whether the difference between the sample means is statistically significant.

Here is how we can perform a two-sample z test in R:

# Compute z-score
z <- (mean1 - mean2) / sqrt((sd1^2 / n1) + (sd2^2 / n2))

# Compute p-value (two-tailed)
p_value <- 2 * pnorm(-abs(z))Code language: R (r)

In the code above, we calculated the z statistic using the sample means, the known population standard deviations, and the sample sizes. We then used the z statistic to calculate a two-tailed p-value. That is, we can use the p-value to test whether the difference between the two population means is statistically significant.

Conclusion

In this post, you learned how to perform a two-sample z test in R. We looked at two examples: comparing proportions and comparing means between two independent groups.

First, we used prop.test() to compare the proportions in two groups. Next, we calculated the z statistic and p-value manually to test whether two population means differed. As we saw, the two-sample z test can compare proportions or means, provided the assumptions for the respective test are met.

I hope you found this tutorial helpful. If you have any questions or suggestions, please leave a comment below. Also, if you found this post useful, feel free to share it with others who might be interested in statistical analysis using R.

Resources

Here are some more data analysis-related tutorials on this page that you might find helpful:

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top