Accelerating the pace of engineering and science

# signtest

## Syntax

• p = signtest(x) example
• p = signtest(x,y) example
• p = signtest(x,y,Name,Value) example
• [p,h] = signtest(___)
• [p,h,stats] = signtest(___) example

## Description

example

p = signtest(x) returns the p-value for a two-sided sign test.

signtest tests the hypothesis that data in x has a continuous distribution with zero median against the alternative that the distribution does not have zero median at the 5% significance level.

example

p = signtest(x,y) returns the p-value of a two-sided sign test. Here,signtest tests for the hypothesis that the data in xy has a distribution with zero median against the alternative that the distribution does not have zero median. Note that a hypothesis of zero median for xy is not equivalent to a hypothesis of equal median for x and y.

example

p = signtest(x,y,Name,Value) returns the p-value for the sign test with additional options specified by one or more Name,Value pair arguments.

[p,h] = signtest(___) also returns a logical value indicating the test decision. The value h = 1 indicates a rejection of the null hypothesis, and h = 0 indicates a failure to reject the null hypothesis at the 5% significance level. You can use any of the input arguments in the previous syntaxes.

example

[p,h,stats] = signtest(___) also returns the structure stats containing information about the test statistic.

example

[___] = signtest(x,m) returns any of the output arguments in the previous syntaxes for the test whether the data in x are observations from a distribution with median m against the alternative that the median is different from m.

example

[___] = signtest(x,m,Name,Value) returns any of the output arguments in the previous syntaxes for the sign test with additional options specified by one or more Name,Value pair arguments.

## Examples

expand all

### Test for Zero Median of a Single Population

Test the hypothesis of zero median.

Generate the sample data.

```rng('default') % for reproducibility
x = randn(1,25);```

The sampling distribution of x is symmetric with zero median.

Test the null hypothesis that x comes from a distribution with a median different from zero median.

`[p,h,stats] = signtest(x,0)`
```p =
0.1078

h =
0

stats =
zval: NaN
sign: 17```

At the default 5% significance level, the result h = 0 indicates that signtest fails to reject to the null hypothesis of zero median. signtest calculates the p-value using the exact method, hence it does not calculate zval and returns it as a NaN.

### Test for Zero Median for the Difference of Paired Samples

Test the hypothesis of zero median for the difference between paired samples.

Generate the sample data.

```rng('default') % for reproducibility
before = lognrnd(2,.25,10,1);
after = before + (lognrnd(0,.5,10,1) - 1);
```

The sampling distribution of the difference between before and after is symmetric with zero median.

Test the null hypothesis that the difference of before and after has zero median.

```[p,h] = signtest(before,after)
```
```p =

0.7539

h =

0```

At the default 5% significance level, the value h = 0 indicates that signtest fails to reject to the null hypothesis of zero median in the difference.

### Medians of Paired Samples

Test the hypothesis of zero median for the difference between two paired samples using the exact and approximate methods.

Generate the sample data.

```rng('default') % for reproducibility
x = lognrnd(2,.25,15,1);
y = x + trnd(2,15,1);
display([x y])
```
```ans =

8.4521    7.8047
11.6869   11.4094
4.2009    5.1133
9.1664   12.1655
8.0020   10.0300
5.3285    6.0153
6.6300    5.1235
8.0499    8.6737
18.0763   19.2164
14.7665   15.3380
5.2726    8.4187
15.7798   16.2093
8.8583    8.5575
7.2735    7.4783
8.8347    7.8894```

Test the hypothesis that xy has zero median.

`[p,h,stats] = signtest(x,y)`
```p =

0.3018

h =

0

stats =

zval: NaN
sign: 5```

At the default 5% significance level, the value h = 0 indicates that the test fails to reject the null hypothesis of zero median in the difference.

Repeat the test using the approximate method.

`[p,h,stats] = signtest(x,y,'method','approximate')`
```p =

0.3017

h =

0

stats =

zval: -1.0328
sign: 5```

The approximate p-value, which signtest obtains using the z-statistic, is really close to the exact p-value.

### Test for Large Samples

Perform a left-sided sign test for large samples.

Navigate to a folder containing sample data.

```cd(matlabroot)
cd('help/toolbox/stats/examples')```

`load gradespaired`

Test the null hypothesis that the median of the grade differences before and after the tutoring program is 0 against the alternate that it is less than 0.

```[p,h,stats] = signtest(gradespaired(:,1),gradespaired(:,2),...
'tail','left')```
```p =

0.0013

h =

1

stats =

zval: -3.0110
sign: 37```

Because the sample size is large (greater than 100), signtest uses an approximate method to calculate the p-value and also returns the value of the z-statistic. The test rejects the null hypothesis that there is no difference between the grade medians at the 5% significance level.

### Test for Median of a Single Population

Test the hypothesis that the population median is different from a specified value.

`load lawdata`

The data set has 15 observations for variables gpa and lsat.

Test the hypothesis that the median lsat score is higher than 570.

`[p,h,stats] = signtest(lsat,570,'tail','right')`
```p =

0.0176

h =

1

stats =

zval: NaN
sign: 12```

Both the p-value, 0.0176, and h = 1 indicate that at the 5% significance level the test concludes in favor of the alternate hypothesis.

## Input Arguments

expand all

### x — Sample datavector

Sample data, specified as a vector.

Data Types: single | double

### y — Sample datavector

Sample data, specified as a vector. y must be the same length as x.

Data Types: single | double

### m — Hypothesized value of the median scalar

Hypothesized value of the median, specified as a scalar.

Example: signtest(x,35)

Data Types: single | double

### Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'alpha',0.01,'method','approximate','tail','right' specifies a right-tailed sign test with 1% significance level , which returns the approximate p-value.

### 'alpha' — Significance level0.05 (default) | scalar value in the range 0 to 1

Significance level of the hypothesis test, specified as the comma-separated pair consisting of 'alpha' and a scalar value in the range 0 to 1. The default value of alpha is 0.05. Significance level of h is 100 * alpha%.

Example: 'alpha', 0.01

Data Types: double | single

### 'method' — p-value computation method'exact' | 'approximate'

p-value computation method, specified as the comma-separated pair consisting of 'method' and one of the following:

 'exact' Exact computation of the p-value, p. 'approximate' Normal approximation for computing the p-value, p.

The default computation method is 'exact', if there are fewer than 100 observations and 'approximate' if there are 100 observations or more.

Example: 'method','exact'

Data Types: char

### 'tail' — Type of test'both' (default) | 'right' | 'left'

Type of test, specified as the comma-separated pair consisting of 'tail' and one of the following:

 'both' Two-sided hypothesis test, which is the default test type.For a one-sample test, the alternate hypothesis states that the data in x come from a continuous distribution with median different than zero (or m).For a two-sample test, the alternate hypothesis states that the data in x-y come from a distribution with median different than zero. 'right' Right-tailed hypothesis test.For a one-sample test, the alternate hypothesis states that the data in x come from a continuous distribution with median greater than zero (or m).For a two-sample test, the alternate hypothesis states the data in x-y come from a distribution with median greater than zero. 'left' Left-tailed hypothesis test. For a one-sample test, the alternate hypothesis states that the data in x come from a continuous distribution with median less than zero (or m).For a two-sample test, the alternative hypothesis states the data in x-y come from a distribution with median less than zero.

Example: 'tail','left'

## Output Arguments

expand all

### p — p-value of the testnonnegative scalar

p-value of the test, returned as a nonnegative scalar from 0 to 1. p is the probability of observing a test statistic as or more extreme than the observed value under the null hypothesis. signtest computes the two-sided p-value by doubling the most significant one-sided value.

### h — Result of the hypothesis test1 | 0

Result of the hypothesis test, returned as a logical value.

• If h = 1, this indicates rejection of the null hypothesis at the 100 * alpha% significance level.

• If h = 0, this indicates a failure to reject the null hypothesis at the 100 * alpha% significance level.

### stats — Test statisticsstructure

Test statistics, returned as a structure. The test statistics stored in stats are:

• sign: Value of the sign test statistic.

• zval: Value of the z-statistic (computed only for large samples).

expand all

### Sign Test

The sign test is a nonparametric test for the median of a population or median of the difference of two populations.

For example, for tests on a single population median:

• If the test is two-sided, then the test statistic, S, is the minimum of the number of observations that are smaller or larger than the hypothesized median value, M0.

• If the test is right-sided, then S is the number of observations that are larger than the hypothesized median value M0.

• If the test is left-sided, then S is the number of observations that are smaller than the hypothesized median value M0.

### z-Statistic

For a large sample, signtest uses the z-statistic to approximate the p-value.

The signtest test statistic is the number of elements that are greater than 0 (for signtest(x) or signtest(x-y)), or m (for signtest(x,m)). Hence, the z-statistic of the sign test, with the continuity correction, is:

$z=\frac{\left(S-E\left(S\right)\right)}{\sqrt{V\left(S\right)}}=\frac{\left(S-\left(0.5\right)n-0.5sign\left(npos-nneg\right)\right)}{\sqrt{\left(0.5\right)\left(0.5\right)n}},$

where npos and nneg are the number of positive and negative differences from the hypothesized median value, respectively.

### Algorithms

For a one-sample test, signtest omits values in x that are zero or NaN.

For a two-sample test, signtest omits values in xy that are zero or NaN.

## References

[1] Gibbons, J. D., and S. Chakraborti. Nonparametric Statistical Inference, 5th Ed. Boca Raton, FL: Chapman & Hall/CRC Press, Taylor & Francis Group, 2011.

[2] Hollander, M., and D. A. Wolfe. Nonparametric Statistical Methods. Hoboken, NJ: John Wiley & Sons, Inc., 1999.