sampszcalc

 Performs sample size calculations, with optional correction for the design
 effect deviating from unity.

 -- Function File: N = sampszcalc (TESTTYPE, EFFSZ)
 -- Function File: N = sampszcalc (TESTTYPE, EFFSZ, POW)
 -- Function File: N = sampszcalc (TESTTYPE, EFFSZ, POW, ALPHA)
 -- Function File: N = sampszcalc (TESTTYPE, EFFSZ, POW, ALPHA, TAILS)
 -- Function File: N = sampszcalc (TESTTYPE, EFFSZ, POW, ALPHA, TAILS, DEFF)

      'N = sampszcalc (TESTTYPE, EFFSZ)' returns the required sample size to
      reach the significance level (alpha) of 0.05 in a two-tailed version of
      the test specified in TESTTYPE for the specified effect size, EFFSZ,
      with a power of 0.8 (i.e. a type II error rate of 1 - 0.8 = 0.2)

        TESTTYPE can be:

          't2' (default) : two-sample unpaired t-test

          't' : paired t-test or one-sample t-test

          'z2' (default) : two-sample unpaired z-test (Normal approximation)

          'z' : paired z-test or one-sample z-test (Normal approximation)

          'r' : significance test for correlation

        EFFSZ can be numeric value corresponding to the standardized effect
        size: Cohen's d or h (when TESTTYPE is 't2', 't', 'z' or 'z'), or 
        Pearson's correlation coefficient (when TESTTYPE is 'r'). For
        convenience, EFFSZ can also be one of the following strings:

          'small' : which is 0.2 for Cohen's d (or h), or 0.1 for Pearson's r.

          'medium' : which is 0.5 for Cohen's d (or h), or 0.3 for Pearson's r.

          'large' : which is 0.8 for Cohen's d (or h), or 0.5 for Pearson's r.

       'N = sampszcalc (TESTTYPE, EFFSZ, POW)' also sets the desired power of
       the test. The power corresponds to 1 - beta, where beta is the type II
       error rate (i.e. the probability of not rejecting the null hypothesis
       when it is actually false). (Default is 0.8)

       'N = sampszcalc (TESTTYPE, EFFSZ, POW, ALPHA)' also sets the desired
       significance level, ALPHA, of the test. ALPHA corresponds to the type I
       error rate (i.e. the probability of rejecting the null hypothesis when
       it is actually true). (Default is 0.05)

       HINT: If the test is expected to be among a family of tests, divide
       ALPHA by the number of tests so that the sample size calculations will
       maintain the desired power after correction for multiple comparisons.

       'N = sampszcalc (TESTTYPE, EFFSZ, POW, ALPHA, TAILS)' also sets whether
       the test is one-sided or two-sided. (Default is 2)

       'N = sampszcalc (TESTTYPE, EFFSZ, POW, ALPHA, TAILS, DEFF)' also sets
       the design effect to correct the sample size calculation. (Default is 1)
       DEFF can be estimated by dividing the sampling variance of the parameter
       of interest from a complex experimental design by the equivalent
       statistic computed using simple random sampling with replacement.

  Author: Andrew Charles Penn
  https://www.researchgate.net/profile/Andrew_Penn/

  Copyright 2019 Andrew Charles Penn
  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see http://www.gnu.org/licenses/

Demonstration 1

The following code


 % The difference between a sample mean from a zero constant (one sample test)
 % or the difference between two dependent means (matched pair). Sample size
 % determined for Cohen's d = 0.8.
 % d effect size

 n = sampszcalc ('t', 0.8)

Produces the following output

n =    15

Demonstration 2

The following code


 % The difference between two independent means (two groups). Sample size
 % determined for Cohen's d = 0.8.

 n = sampszcalc ('t2', 0.8)

Produces the following output

n =    26

Demonstration 3

The following code


 % The difference between two independent means (two groups). Sample size
 % determined for Cohen's d = 0.8 and a design effect of 1.5

 n = sampszcalc ('t2', 0.8, [], [], [], 1.5)

Produces the following output

n =    39

Demonstration 4

The following code


 % The difference between two independent proportions (two sample test). 
 % Sample size determined for Cohen's h = 0.8 using Normal approximation.

 n = sampszcalc ('z2', 0.8)

Produces the following output

n =    25

Demonstration 5

The following code


 % The test for Pearson's correlation coefficient (r) equal to 0 (constant),
 % Sample size determined for r effect size = 0.5.

 n = sampszcalc ('r', 0.5)

Produces the following output

n =    30

Demonstration 6

The following code


 % Sample size calculation for nested two-sample test using the design effect
 % from a pilot experiment. N below corresponds to the number of independent
 % sampling units (i.e. clusters).
 % See also the help documentation for functions bootlm and deffcalc.
 score = [21, 26, 33, 22, 18, 25, 26, 24, 21, 25, 35, 28, 32, 36, 38, ...
          26, 34, 27, 38, 44, 34, 45, 38, 31, 41, 34, 35, 38, 46]';
 method = {'A','A','A','A','A','A','A','A','A','A','A','A','A','A','A', ...
           'B','B','B','B','B','B','B','B','B','B','B','B','B','B'}';
 room = [1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, ...
         1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3]';

 [STATS_STD] = bootlm (score, {method}, 'clustid', room, ...
                             'seed', 1, 'display', 'off', 'dim', 1, ...
                             'posthoc', 'trt_vs_ctrl', ...
                             'method', 'bayesian', 'prior', 'auto', ...
                             'standardize', true);

 [STATS, BOOTSTAT] = bootlm (score, {method}, 'clustid', room, ...
                             'seed', 1, 'display', 'off', 'dim', 1, ...
                             'posthoc', 'trt_vs_ctrl', ...
                             'method', 'bayesian', 'prior', 'auto');

 [STATS_SRS, BOOTSTAT_SRS] = bootlm (score, {method}, 'clustid', [], ...
                             'seed', 1, 'display', 'off', 'dim', 1, ...
                             'posthoc', 'trt_vs_ctrl', ...
                             'method', 'bayesian', 'prior', 'auto');

 fprintf('Cohen''s d = %.2f\n', STATS_STD.estimate)

 N = sampszcalc ('t2', STATS_STD.estimate, 0.80, 0.05, 2)

 DEFF = deffcalc (BOOTSTAT, BOOTSTAT_SRS)

 N_corrected = sampszcalc ('t2', STATS_STD.estimate, 0.80, 0.05, 2, DEFF)

Produces the following output

Cohen's d = 1.72
N =     7
DEFF = 3.5527
N_corrected =    23

Package: statistics-resampling