NAG FL Interface
g07caf (ttest_​2normal)

Settings help

FL Name Style:


FL Specification Language:


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

Fortran Interface
Subroutine g07caf ( tail, equal, nx, ny, xmean, ymean, xstd, ystd, clevel, t, df, prob, dl, du, ifail)
Integer, Intent (In) :: nx, ny
Integer, Intent (Inout) :: ifail
Real (Kind=nag_wp), Intent (In) :: xmean, ymean, xstd, ystd, clevel
Real (Kind=nag_wp), Intent (Out) :: t, df, prob, dl, du
Character (1), Intent (In) :: tail, equal
C Header Interface
#include <nag.h>
void  g07caf_ (const char *tail, const char *equal, const Integer *nx, const Integer *ny, const double *xmean, const double *ymean, const double *xstd, const double *ystd, const double *clevel, double *t, double *df, double *prob, double *dl, double *du, Integer *ifail, const Charlen length_tail, const Charlen length_equal)
The routine may be called by the names g07caf or nagf_univar_ttest_2normal.

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. 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¯ s(1/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=P(t|tobs|), i.e., a two tailed probability.
    • H1:μx>μy; the significance level, p=P(ttobs), i.e., an upper tail probability.
    • H1:μx<μy; the significance level, p=P(ttobs), i.e., a lower tail probability.
    Upper and lower 100(1-α)% confidence limits for μx-μy are calculated as:
    (x¯-y¯)±t1-α/2s(1/nx)+(1/ny).  
    where t1-α/2 is the 100(1-α/2) percentage point of the t-distribution with (nx+ny-2) degrees of freedom.
  2. 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¯ se(x¯-y¯)  
    where se(x¯-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 100(1-α)% confidence limits for μx-μy are calculated as:
    (x¯-y¯)±t1-α/2se(x-y¯).  
    where t1-α/2 is the 100(1-α/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 Arguments

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 Integer Input
On entry: nx, the size of the X sample.
Constraint: nx2.
4: ny Integer Input
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 Integer Input/Output
On entry: ifail must be set to 0, −1 or 1 to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of 0 causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of −1 means that an error message is printed while a value of 1 means that it is not.
If halting is not appropriate, the value −1 or 1 is recommended. If message printing is undesirable, then the value 1 is recommended. Otherwise, the value 0 is recommended. 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, clevel=value.
Constraint: 0.0<clevel<1.0.
On entry, equal=value.
Constraint: equal='E' or 'U'.
On entry, nx=value.
Constraint: nx2.
On entry, ny=value.
Constraint: ny2.
On entry, tail=value.
Constraint: tail='T', 'U' or 'L'.
On entry, xstd=value.
Constraint: xstd>0.0.
On entry, ystd=value.
Constraint: ystd>0.0.
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
ifail=-399
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
ifail=-999
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

7 Accuracy

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

8 Parallelism and Performance

g07caf is not threaded in any implementation.

9 Further Comments

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

10 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.

10.1 Program Text

Program Text (g07cafe.f90)

10.2 Program Data

Program Data (g07cafe.d)

10.3 Program Results

Program Results (g07cafe.r)