R User Group of Milano (Italy)

Power and Sample Size Analysis: Z test


This article provide a brief background about power and sample size analysis. Then, power and sample size analysis is computed for the Z test.
Next articles will describe power and sample size analysis for:

  • one sample and two samples t test;,
  • p test, chi-square test, correlation;
  • one-way ANOVA;
  • DOE 2^k.

Finally, a PDF article showing both the underlying methodology and the R code here provided, will be published.


Power and sample size analysis are important tools for assessing the ability of a statistical test to detect when a null hypothesis is false, and for deciding what sample size is required for having a reasonable chance to reject a false null hypothesis.

The following four quantities have an intimate relationship:

  1. sample size
  2. effect size
  3. significance level = P(Type I error) = probability of finding an effect that is not there
  4. power = 1 - P(Type II error) = probability of finding an effect that is there

Given any three, we can determine the fourth.

Z test

The formula for the power computation can be implemented in R, using a function like the following:

In the same way, the function to compute the sample size can be built.

The above code is provided for didactic purpose. In fact, the pwr package provide a function to perform power and sample size analysis.

The function pwr.norm.test() computes parameters for the Z test. It accepts the four parameters see above, one of them passed as NULL. The parameter passed as NULL is determined from the others.

Some examples

Power at \mu = 105 for H0: \mu = 100 against H1: \mu>100.
\sigma = 15, n = 20, $$\alpha = 0.05$

This is the result with the self-made function:

And here the same with the pwr.norm.test() function:

The sample size of the test for power equal to 0.80 can be computed using the self-made function

or with the pwr.norm.test() function:

The power function can be drawn:

View (and download) the full code:

One comment

  1. Stefan /

    Thank you for the article! Very informative in a short text!

    I am looking foreward to the other parts, as well as the pdf of all, that will for sure serve me as a good reference.

    It is always a good idea to write a few sentences of introduction and a short explanation of each of the variables you will calculate, especially by commenting the code - if the article is intended for beginners... :)

Leave a Reply