vartest2

Two-sample F-test for equal variances

Syntax

``h = vartest2(x,y)``
``h = vartest2(x,y,Name,Value)``
``````[h,p] = vartest2(___)``````
``````[h,p,ci,stats] = vartest2(___)``````

Description

````h = vartest2(x,y)` returns a test decision for the null hypothesis that the data in vectors `x` and `y` comes from normal distributions with the same variance, using the two-sample F-test. The alternative hypothesis is that they come from normal distributions with different variances. The result `h` is `1` if the test rejects the null hypothesis at the 5% significance level, and `0` otherwise.```

example

````h = vartest2(x,y,Name,Value)` returns a test decision for the two-sample F-test with additional options specified by one or more name-value pair arguments. For example, you can change the significance level or conduct a one-sided test.```

example

``````[h,p] = vartest2(___)``` also returns the p-value of the test, `p`, using any of the input arguments in the previous syntaxes.```

``````[h,p,ci,stats] = vartest2(___)``` also returns the confidence interval for the true variance ratio, `ci`, and the structure `stats` containing information about the test statistic.```

Examples

collapse all

Load the sample data. Create vectors containing the first and second columns of the data matrix to represent students&' grades on two exams.

```load examgrades; x = grades(:,1); y = grades(:,2);```

Test the null hypothesis that the data in `x` and `y` comes from distributions with the same variance.

`[h,p,ci,stats] = vartest2(x,y)`
```h = 1 ```
```p = 0.0019 ```
```ci = 2×1 1.2383 2.5494 ```
```stats = struct with fields: fstat: 1.7768 df1: 119 df2: 119 ```

The returned result `h = 1` indicates that `vartest2` rejects the null hypothesis at the default 5% significance level. `ci` contains the lower and upper boundaries of the 95% confidence interval for the true variance ratio. `stats` contains the value of the test statistic for the $F$-test and the numerator and denominator degrees of freedom.

Load the sample data. Create vectors containing the first and second columns of the data matrix to represent students' grades on two exams.

```load examgrades; x = grades(:,1); y = grades(:,2);```

Test the null hypothesis that the data in `x` and `y` comes from distributions with the same variance, against the alternative that the population variance of `x` is greater than that of `y`.

`vartest2(x,y,'Tail','right')`
```ans = 1 ```

The returned result `h = 1` indicates that `vartest2` rejects the null hypothesis at the default 5% significance level, in favor of the alternative hypothesis that the population variance of `x` is greater than that of `y`.

Input Arguments

collapse all

Sample data, specified as a vector, matrix, or multidimensional array.

• If `x` and `y` are vectors, they do not need to be the same length.

• If `x` and `y` are matrices, they must have the same number of columns, but do not need to have the same number of rows. `vartest2` performs separate tests along each column and returns a vector of the results.

• If `x` and `y` are multidimensional arrays, they must have the same number of dimensions, and the same size along all but the first nonsingleton dimension.

Data Types: `single` | `double`

Sample data, specified as a vector, matrix, or multidimensional array.

• If `x` and `y` are vectors, they do not need to be the same length.

• If `x` and `y` are matrices, they must have the same number of columns, but do not need to have the same number of rows. `vartest2` performs separate tests along each column and returns a vector of the results.

• If `x` and `y` are multidimensional arrays, they must have the same number of dimensions, and the same size along all but the first nonsingleton dimension.

Data Types: `single` | `double`

Name-Value Arguments

Specify optional pairs of arguments as `Name1=Value1,...,NameN=ValueN`, where `Name` is the argument name and `Value` is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose `Name` in quotes.

Example: `'Tail','right','Alpha',0.01` specifies a right-tailed hypothesis test at the 1% significance level.

Significance level of the hypothesis test, specified as the comma-separated pair consisting of `'Alpha'` and a scalar value in the range (0,1).

Example: `'Alpha',0.01`

Data Types: `single` | `double`

Dimension of the input matrix to test along, specified as the comma-separated pair consisting of `'Dim'` and a positive integer value. For example, specifying `'Dim',1` tests the data in each column for variance equality, while `'Dim',2` tests the data in each row.

Example: `'Dim',2`

Data Types: `single` | `double`

Type of alternative hypothesis to evaluate using the F-test, specified as the comma-separated pair consisting of `'Tail'` and one of the following.

 `'both'` Test the alternative hypothesis that the population variances are not equal. `'right'` Test the alternative hypothesis that the population variance of `x` is greater than that of `y`. `'left'` Test the alternative hypothesis that the population variance of `x` is less than that of `y`.

Example: `'Tail','right'`

Output Arguments

collapse all

Hypothesis test result, returned as `1` or `0`.

• If `h` `= 1`, this indicates the rejection of the null hypothesis at the `Alpha` significance level.

• If `h` `= 0`, this indicates a failure to reject the null hypothesis at the `Alpha` significance level.

p-value of the test, returned as a scalar value in the range [0,1]. `p` is the probability of observing a test statistic as extreme as, or more extreme than, the observed value under the null hypothesis. Small values of `p` cast doubt on the validity of the null hypothesis.

Confidence interval for the true ratio of the population variances, returned as a two-element vector containing the lower and upper boundaries of the 100 × (1 – `Alpha`)% confidence interval.

Test statistics for the hypothesis test, returned as a structure containing:

• `fstat` — Value of the test statistic.

• `df1` — Numerator degrees of freedom of the test.

• `df2` — Denominator degrees of freedom of the test.

collapse all

Two-Sample F-Test

The two-sample F-test is used to test if the variances of two populations are equal.

The test statistic is

`$F=\frac{{s}_{1}{}^{2}}{{s}_{2}{}^{2}},$`

where s1 and s2 are the sample standard deviations. The test statistic is a ratio of the two sample variances. The further this ratio deviates from 1, the more likely you are to reject the null hypothesis. Under the null hypothesis, the test statistic F has a F-distribution with numerator degrees of freedom equal to N1 – 1 and denominator degrees of freedom equal to N2 – 1, where N1 and N2 are the sample sizes of the two data sets.

Multidimensional Array

A multidimensional array has more than two dimensions. For example, if `x` is a 1-by-3-by-4 array, then `x` is a three-dimensional array.

First Nonsingleton Dimension

The first nonsingleton dimension is the first dimension of an array whose size is not equal to 1. For example, if `x` is a 1-by-2-by-3-by-4 array, then the second dimension is the first nonsingleton dimension of `x`.

Version History

Introduced before R2006a