# signrank

Wilcoxon signed rank test

## Syntax

``p = signrank(x)``
``p = signrank(x,y)``
``p = signrank(x,y,Name,Value)``
``````[p,h] = signrank(___)``````
``````[p,h,stats] = signrank(___)``````
``[___] = signrank(x,m)``
``[___] = signrank(x,m,Name,Value)``

## Description

example

````p = signrank(x)` returns the p-value of a two-sided Wilcoxon signed rank test.`signrank` tests the null hypothesis that data in the vector `x` come from a distribution whose median is zero at the 5% significance level. The test assumes that the data in `x` come from a continuous distribution symmetric about its median.```

example

````p = signrank(x,y)` returns the p-value of a paired, two-sided test for the null hypothesis that `x` – `y` comes from a distribution with zero median.```
````p = signrank(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] = signrank(___)``` also returns a logical value indicating the test decision. `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] = signrank(___)``` also returns the structure `stats` with information about the test statistic.```

example

````[___] = signrank(x,m)` returns any of the output arguments in the previous syntaxes for the null hypothesis that the data in `x` are observations from a distribution with median `m`.```

example

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

## Examples

collapse all

Test the hypothesis of zero median.

Generate the sample data.

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

Test the hypothesis that the data in `x` has zero median.

`[p,h] = signrank(x)`
```p = 3.2229e-05 ```
```h = logical 1 ```

At the default 5% significance level, the value `h` = 1 indicates that the test rejects the null hypothesis of zero median.

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

Generate the sample data.

```rng('default') % for reproducibility x = lognrnd(2,.25,10,1); y = x + trnd(2,10,1);```

Test the hypothesis that `x``y` has zero median.

`[p,h] = signrank(x,y)`
```p = 0.3223 ```
```h = logical 0 ```

The results indicate that the test fails to reject the null hypothesis of zero median in the difference at the default 5% significance level.

Conduct a -sided test on a large sample using approximation.

`load('gradespaired.mat');`

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

```[p,h,stats] = signrank(gradespaired(:,1),... gradespaired(:,2),'tail','left')```
```p = 0.0047 ```
```h = logical 1 ```
```stats = struct with fields: zval: -2.5982 signedrank: 2.0175e+03 ```

Because the sample size is greater than 15, `signrank` uses an approximate method to calculate the $p$-value and also returns the value of the $z$-statistic. The value `h` = 1 indicates that the test rejects the null hypothesis that there is no difference between the grade medians at the 5% significance level. There is enough statistical evidence to conclude that the median grade before the tutoring program is less than the median grade after the tutoring program.

Repeat the test using the exact method.

```[p,h,stats] = signrank(gradespaired(:,1),gradespaired(:,2),... 'tail','left','method','exact')```
```p = 0.0045 ```
```h = logical 1 ```
```stats = struct with fields: signedrank: 2.0175e+03 ```

The results obtained using the approximate method are consistent with the exact method.

`load mileage`

The data contains the mileages per gallon for three different types of cars in columns 1 to 3.

Test the hypothesis that the median mileage for the type of cars in the second column differs from 33.

`[p,h,stats] = signrank(mileage(:,2),33)`
```p = 0.0313 ```
```h = logical 1 ```
```stats = struct with fields: signedrank: 21 ```

At the 5% significance level, the results indicate that the median mileage for the second type of cars differs from 33. Note that `signrank` uses an exact method to calculate the $p$-value for small samples and does not return the $z$-statistic.

Use the name-value pair arguments in `signrank`.

`load mileage`

The data contains the mileage per gallon for three different types of cars in columns 1 to 3.

Test the hypothesis that the median mileage for the type of cars in the second row are larger than 33.

`[p,h,stats] = signrank(mileage(:,2),33,'tail','right')`
```p = 0.0156 ```
```h = logical 1 ```
```stats = struct with fields: signedrank: 21 ```

Repeat the same test at the 1% significance level using the approximate method.

```[p,h,stats] = signrank(mileage(:,2),33,'tail','right',... 'alpha',0.01,'method','approximate')```
```p = 0.0180 ```
```h = logical 0 ```
```stats = struct with fields: zval: 2.0966 signedrank: 21 ```

This result, `h` = 0, indicates that the null hypothesis cannot be rejected at the 1% significance level.

## Input Arguments

collapse all

Sample data, specified as a vector.

Data Types: `single` | `double`

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

Data Types: `single` | `double`

Hypothesized value of the median, specified as a scalar.

Example: `signrank(x,10)`

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 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 signed rank test with 1% significance level, which returns the approximate p-value.

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

Example: `'alpha'`, `0.01`

Data Types: `double` | `single`

Computation method of `p`, specified as the comma-separated pair consisting of `'method'` and one of the following.

 `'exact'` Exact computation of the p-value, `p`. Default value for 15 or fewer observations in `x`, `x` – `m`, or `x` – `y` when `method` is unspecified. `'approximate'` Normal approximation while computing the p-value, `p`. Default value for more than 15 observations in `x`, `x` – `m`, or `x` – `y` when `'method'` is unspecified because the exact method can be slow on large samples.

Example: `'method'`,`'exact'`

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 0 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 0. `'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 0 or `m`.For a two-sample test, the alternate hypothesis states the data in `x` – `y` come from a distribution with median greater than 0. `'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 0 or `m`.For a two-sample test, the alternate hypothesis states the data in `x` – `y` come from a distribution with median less than 0.

Example: `'tail'`,`'left'`

## Output Arguments

collapse all

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. `signrank` computes the two-sided p-value by doubling the most significant one-sided value.

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

• If `h` = 1, this indicates the 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.

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

• `signrank`: Value of the sign rank test statistic.

• `zval`: Value of the z- statistic (computed when `'method'` is `'approximate'`).

collapse all

### Wilcoxon Signed Rank Test

The Wilcoxon signed rank test is a nonparametric test for two populations when the observations are paired. In this case, the test statistic, W, is the sum of the ranks of positive differences between the observations in the two samples (that is, `x``y`). When you use the test for one sample, then W is the sum of the ranks of positive differences between the observations and the hypothesized median value M0 (which is 0 when you use `signrank(x)` and `m` when you use `signrank(x,m)`).

### z-Statistic

For large samples, or when `method` is `approximate`, the `signrank` function calculates the p-value using the z-statistic, given by

`$z=\frac{\left(W-n\left(n+1\right)/4\right)}{\sqrt{\frac{n\left(n+1\right)\left(2n+1\right)-tieadj}{24}}},$`

where n is the sample size of the difference `x – y` or `x``m`. For the two-sample case, `signrank` uses ```[tie_rank,tieadj] = tiedrank(abs(diffxy),0,0,epsdiff)``` to obtain the tie adjustment value `tieadj`.

## Algorithms

`signrank` treats `NaN`s in `x` and `y` as missing values and ignores them.

For the two-sample case, `signrank` uses a tolerance based on the values `epsdiff = eps(x) + eps(y)`. `signrank` computes the absolute values of the differences (`abs(d(i))` where ```d(i) = x(i) – y(i)```) and compares them to `epsdiff`. Values with an absolute value less than `epsdiff` (`abs(d(i)) < epsdiff(i)`) are treated as ties.

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

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