G07CAF (PDF version)
G07 Chapter Contents
G07 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

G07CAF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

G07CAF computes a t-test statistic to test for a difference in means between two Normal populations, together with a confidence interval for the difference between the means.

2  Specification

SUBROUTINE G07CAF ( TAIL, EQUAL, NX, NY, XMEAN, YMEAN, XSTD, YSTD, CLEVEL, T, DF, PROB, DL, DU, IFAIL)
INTEGER  NX, NY, IFAIL
REAL (KIND=nag_wp)  XMEAN, YMEAN, XSTD, YSTD, CLEVEL, T, DF, PROB, DL, DU
CHARACTER(1)  TAIL, EQUAL

3  Description

Consider two independent samples, denoted by X and Y, of size nx and ny drawn from two Normal populations with means μx and μy, and variances σx2 and σy2 respectively. Denote the sample means by x- and y- and the sample variances by sx2 and sy2 respectively.
G07CAF calculates a test statistic and its significance level to test the null hypothesis H0:μx=μy, together with upper and lower confidence limits for μx-μy. The test used depends on whether or not the two population variances are assumed to be equal.
  1. It is assumed that the two variances are equal, that is σx2=σy2.
    The test used is the two sample t-test. The test statistic t is defined by;
    tobs=x--y- s1/nx+1/ny
    where
    s2 = nx-1 sx2 + ny-1 sy2 nx + ny - 2
    is the pooled variance of the two samples.
    Under the null hypothesis H0 this test statistic has a t-distribution with nx+ny-2 degrees of freedom.
    The test of H0 is carried out against one of three possible alternatives;
    • H1:μxμy; the significance level, p=Pttobs, i.e., a two tailed probability.
    • H1:μx>μy; the significance level, p=Pttobs, i.e., an upper tail probability.
    • H1:μx<μy; the significance level, p=Pttobs, i.e., a lower tail probability.
    Upper and lower 1001-α% confidence limits for μx-μy are calculated as:
    x--y-±t1-α/2s1/nx+1/ny.
    where t1-α/2 is the 1001-α/2 percentage point of the t-distribution with (nx+ny-2) degrees of freedom.
  2. It is not assumed that the two variances are equal.
    If the population variances are not equal the usual two sample t-statistic no longer has a t-distribution and an approximate test is used.
    This problem is often referred to as the Behrens–Fisher problem, see Kendall and Stuart (1969). The test used here is based on Satterthwaites procedure. To test the null hypothesis the test statistic t is used where
    tobs=x--y- sex--y-
    where sex--y-= sx2nx+ sy2ny.
    A t-distribution with f degrees of freedom is used to approximate the distribution of t where
    f = se x- - y- 4 sx2 / nx 2 nx-1 + sy2 / ny 2 ny-1 .
    The test of H0 is carried out against one of the three alternative hypotheses described above, replacing t by t and tobs by tobs.
    Upper and lower 1001-α% confidence limits for μx-μy are calculated as:
    x--y-±t1-α/2sex-y-.
    where t1-α/2 is the 1001-α/2 percentage point of the t-distribution with f degrees of freedom.

4  References

Johnson M G and Kotz A (1969) The Encyclopedia of Statistics 2 Griffin
Kendall M G and Stuart A (1969) The Advanced Theory of Statistics (Volume 1) (3rd Edition) Griffin
Snedecor G W and Cochran W G (1967) Statistical Methods Iowa State University Press

5  Parameters

1:     TAIL – CHARACTER(1)Input
On entry: indicates which tail probability is to be calculated, and thus which alternative hypothesis is to be used.
TAIL='T'
The two tail probability, i.e., H1:μxμy.
TAIL='U'
The upper tail probability, i.e., H1:μx>μy.
TAIL='L'
The lower tail probability, i.e., H1:μx<μy.
Constraint: TAIL='T', 'U' or 'L'.
2:     EQUAL – CHARACTER(1)Input
On entry: indicates whether the population variances are assumed to be equal or not.
EQUAL='E'
The population variances are assumed to be equal, that is σx2=σy2.
EQUAL='U'
The population variances are not assumed to be equal.
Constraint: EQUAL='E' or 'U'.
3:     NX – INTEGERInput
On entry: nx, the size of the X sample.
Constraint: NX2.
4:     NY – INTEGERInput
On entry: ny, the size of the Y sample.
Constraint: NY2.
5:     XMEAN – REAL (KIND=nag_wp)Input
On entry: x-, the mean of the X sample.
6:     YMEAN – REAL (KIND=nag_wp)Input
On entry: y-, the mean of the Y sample.
7:     XSTD – REAL (KIND=nag_wp)Input
On entry: sx, the standard deviation of the X sample.
Constraint: XSTD>0.0.
8:     YSTD – REAL (KIND=nag_wp)Input
On entry: sy, the standard deviation of the Y sample.
Constraint: YSTD>0.0.
9:     CLEVEL – REAL (KIND=nag_wp)Input
On entry: the confidence level, 1-α, for the specified tail. For example CLEVEL=0.95 will give a 95% confidence interval.
Constraint: 0.0<CLEVEL<1.0.
10:   T – REAL (KIND=nag_wp)Output
On exit: contains the test statistic, tobs or tobs.
11:   DF – REAL (KIND=nag_wp)Output
On exit: contains the degrees of freedom for the test statistic.
12:   PROB – REAL (KIND=nag_wp)Output
On exit: contains the significance level, that is the tail probability, p, as defined by TAIL.
13:   DL – REAL (KIND=nag_wp)Output
On exit: contains the lower confidence limit for μx-μy.
14:   DU – REAL (KIND=nag_wp)Output
On exit: contains the upper confidence limit for μx-μy.
15:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ or ​1. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1​ or ​1 is recommended. If the output of error messages is undesirable, then the value 1 is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is 0. When the value -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6  Error Indicators and Warnings

If on entry IFAIL=0 or -1, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
IFAIL=1
On entry,TAIL'T', 'U' or 'L',
orEQUAL'E' or 'U',
orNX<2,
orNY<2,
orXSTD0.0,
orYSTD0.0,
orCLEVEL0.0,
orCLEVEL1.0.

7  Accuracy

The computed probability and the confidence limits should be accurate to approximately five significant figures.

8  Further Comments

The sample means and standard deviations can be computed using G01ATF.

9  Example

This example reads the two sample sizes and the sample means and standard deviations for two independent samples. The data is taken from page 116 of Snedecor and Cochran (1967) from a test to compare two methods of estimating the concentration of a chemical in a vat. A test of the equality of the means is carried out first assuming that the two population variances are equal and then making no assumption about the equality of the population variances.

9.1  Program Text

Program Text (g07cafe.f90)

9.2  Program Data

Program Data (g07cafe.d)

9.3  Program Results

Program Results (g07cafe.r)


G07CAF (PDF version)
G07 Chapter Contents
G07 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2012