Here, we discuss the sign test for paired samples in R with interpretations, including, test statistics, p-values, and confidence intervals.
The sign test for paired samples in R can be performed with the
SIGN.test()
function from the "BSDA" package.
The sign test for paired samples can be used to test whether the median of the differences between paired values from the two dependent populations where two dependent random samples come from is equal to a certain value (which is stated in the null hypothesis) or not. It is a non-parametric alternative to the paired two sample t-test.
In the sign test for paired samples, the test statistic is based on the signs of the observed paired differences minus the null hypothesis median. It is the number of observed paired differences greater than the null hypothesis median of paired difference, and inference is based on binomial distribution.
Question | Is the median of paired x and y differences equal to \(m_0\)? | Is the median of paired x and y differences greater than \(m_0\)? | Is the median of paired x and y differences less than \(m_0\)? |
Form of Test | Two-tailed | Right-tailed test | Left-tailed test |
Null Hypothesis, \(H_0\) | \(m_d = m_0\) | \(m_d = m_0\) | \(m_d = m_0\) |
Alternate Hypothesis, \(H_1\) | \(m_d \neq m_0\) | \(m_d > m_0\) | \(m_d < m_0\) |
To use the SIGN.test()
function form the "BSDA" package,
first install the package, then load it as follows:
# Create the data samples for the sign test for paired samples
# Values are paired based on matching position in each sample
data_x = c(5.3, 8.2, 6.8, 6.3, 5.6)
data_y = c(7.8, 6.2, 9.2, 7.1, 5.6)
# Run the sign test for paired samples with specifications
SIGN.test(data_x, data_y,
alternative = "two.sided",
md = 0, conf.level = 0.95)
Dependent-samples Sign-Test
data: data_x and data_y
S = 1, p-value = 0.625
alternative hypothesis: true median difference is not equal to 0
93.75 percent confidence interval:
-2.5 2.0
sample estimates:
median of x-y
-0.8
Argument | Usage |
x, y | x is the sample data values for the first sample, and y is for the second sample |
md | Population median value of paired differences in null hypothesis |
alternative | Set alternate hypothesis as "greater", "less", or the default "two.sided" |
conf.level | Level of confidence for the test and confidence interval (default = 0.95) |
# Create data
data_x = rnorm(100); data_y = rnorm(100)
# Create object
sgnt_object = SIGN.test(data_x, data_y,
alternative = "two.sided",
md = 0, conf.level = 0.95)
# Extract a component
sgnt_object$statistic
S
44
Test Component | Usage |
sgnt_object$statistic | Test statistic; number of paired differences greater than the median |
sgnt_object$p.value | P-value |
sgnt_object$estimate | Point estimate or sample median of paired differences |
sgnt_object$conf.int | Confidence interval |
The sign test for paired samples has test statistics, \(S\), of the form:
\[S = \sum_{i=1}^n I_{([x_i-y_i]-m_0)>0},\]
which is the number of observed paired differences greater than the null hypothesis median.
For \(n_d\) which is the number of non-zero \(((x_i-y_i) - m_0)\), inference on \(S\) is based on the binomial distribution with \(\text{size} = n_d\) and \(\text{prob} = 0.5\).
\(x_i's\) and \(y_i's\) are the sample values,
\(m_0\) is the population median of paired differences to be tested and set in the null hypothesis,
\(I_{([x_i-y_i]-m_0)>0}\) is \(1\) when \(([x_i-y_i]-m_0)>0\) and \(0\) otherwise, and
\(n\) is the sample size of paired differences.
See also the sign test for one sample and the Wilcoxon signed rank test for paired samples.
For unpaired (or independent) samples, see the Wilcoxon rank sum test.
Enter the data by hand.
data_x = c(20.9, 22.3, 18.1, 19.5, 16.9,
20.4, 17.2, 18.5, 19.2, 25.5,
23.0, 20.7, 20.2, 17.1, 19.8)
data_y = c(24.3, 21.4, 20.1, 18.7, 13.8,
23.4, 15.6, 22.2, 25.7, 15.7,
22.1, 19.2, 15.3, 15.5, 15.2)
For the following null hypothesis \(H_0\), and alternative hypothesis \(H_1\), with the level of significance \(\alpha=0.05\).
\(H_0:\) the population median of the paired differences is equal to 0 (\(m_d = 0\)).
\(H_1:\) the population median of the paired differences is not equal to 0 (\(m_d \neq 0\), hence the default two-sided).
Because the level of significance is \(\alpha=0.05\), the level of confidence is \(1 - \alpha = 0.95\).
The SIGN.test()
function has the default
alternative as "two.sided", the default median of the
paired differences as 0, and the default level of
confidence as 0.95, hence, you do not need to specify the
"alternative", "md", and "conf.level" arguments in this
case.
Or:
Dependent-samples Sign-Test
data: data_x and data_y
S = 10, p-value = 0.3018
alternative hypothesis: true median difference is not equal to 0
95 percent confidence interval:
-2.821832 2.832747
sample estimates:
median of x-y
0.9
Achieved and Interpolated Confidence Intervals:
Conf.Level L.E.pt U.E.pt
Lower Achieved CI 0.8815 -2.0000 1.6000
Interpolated CI 0.9500 -2.8218 2.8327
Upper Achieved CI 0.9648 -3.0000 3.1000
The sample median of paired differences, \(\tilde d\), is 0.9,
test statistic, \(S\), is 10,
the p-value, \(p\), is 0.3018,
the interpolated 95% confidence interval is [-2.821832, 2.832747].
Note that for SIGN.test()
in R, the two methods may
disagree for some edge cases, as p-value is based on binomial
distribution, and confidence interval is based on interpolation of
binomial distribution.
P-value: With the p-value (\(p = 0.3018\)) being greater than the level
of significance 0.05, we fail to reject the null hypothesis that the
population median of the paired differences is equal to 0. This is the
same as the "two.sided" test for \(\text{Binomial($n_d$, 0.5)}\), that is,
binom.test(10, 15, 0.5, "two.sided")$p.value
\(=0.3017578\).
Confidence Interval: With the null hypothesis median of paired differences (\(m_d = 0\)) being inside the confidence interval, \([-2.821832, 2.832747]\), we fail to reject the null hypothesis that the population median of the paired differences is equal to 0.
To get the critical value for a sign test for paired samples in R,
you can use the qbinom()
function for binomial distribution
to derive the quantile associated with the given level of significance
value \(\alpha\).
With \(n_d\) as the number of non-zero \(((x_i-y_i) - m_0)\) as above.
For two-tailed test with level of significance \(\alpha\). The critical values are: qbinom(\(\alpha/2\), \(n_d\), 0.5) - 1, and qbinom(\(1-\alpha/2\), \(n_d\), 0.5) + 1.
For one-tailed test with level of significance \(\alpha\). The critical value is: for left-tailed, qbinom(\(\alpha\), \(n_d\), 0.5) - 1; and for right-tailed, qbinom(\(1-\alpha\), \(n_d\), 0.5) + 1.
Example:
For \(\alpha = 0.1\), \(n_d = 60\).
Two-tailed:
[1] 23
[1] 37
One-tailed:
[1] 24
[1] 36
Using a subset of the AirPassengers data from the "datasets" package:
Air_data = data.frame(matrix(AirPassengers, ncol = 12, byrow = TRUE))[,7:12]
colnames(Air_data) = c("Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
rownames(Air_data) = 1949:1960
Air_data
Jul Aug Sep Oct Nov Dec
1949 148 148 136 119 104 118
1950 170 170 158 133 114 140
1951 199 199 184 162 146 166
1952 230 242 209 191 172 194
1953 264 272 237 211 180 201
1954 302 293 259 229 203 229
1955 364 347 312 274 237 278
1956 413 405 355 306 271 306
1957 465 467 404 347 305 336
1958 491 505 404 359 310 337
1959 548 559 463 407 362 405
1960 622 606 508 461 390 432
For "Sep" as the x group versus "Oct" as the y group.
For the following null hypothesis \(H_0\), and alternative hypothesis \(H_1\), with the level of significance \(\alpha=0.1\).
\(H_0:\) the population median of the paired differences is equal to 50 (\(m_d = 50\)).
\(H_1:\) the population median of the paired differences is not equal to 50 (\(m_d \neq 50\), hence the default two-sided).
Because the level of significance is \(\alpha=0.1\), the level of confidence is \(1 - \alpha = 0.9\).
Dependent-samples Sign-Test
data: Air_data$Sep and Air_data$Oct
S = 2, p-value = 0.03857
alternative hypothesis: true median difference is not equal to 50
90 percent confidence interval:
23.71545 47.85636
sample estimates:
median of x-y
34
Achieved and Interpolated Confidence Intervals:
Conf.Level L.E.pt U.E.pt
Lower Achieved CI 0.8540 25.0000 47.0000
Interpolated CI 0.9000 23.7155 47.8564
Upper Achieved CI 0.9614 22.0000 49.0000
P-value: With the p-value (\(p = 0.03857\)) being less than the level of significance 0.1, we reject the null hypothesis that the population median of the paired differences is equal to 50.
\(S\) T-statistic: With \(n_d = 12\), and test statistics value (\(S = 2\)) being less than or equal to \(\text{qbinom(0.05, 12, 0.5)-1}=2\), or being within \(0 \text{ to } 2\), we reject the null hypothesis that the population median of the paired differences is equal to 50.
Confidence Interval: With the null hypothesis median of the paired differences (\(m_d = 50\)) being outside the confidence interval, \([23.71545, 47.85636]\), we reject the null hypothesis that the population median of the paired differences is equal to 50.
Using a subset of the AirPassengers data from the "datasets" package:
Air_data = data.frame(matrix(AirPassengers, ncol = 12, byrow = TRUE))[,1:6]
colnames(Air_data) = c("Jan", "Feb", "Mar", "Apr", "May", "Jun")
rownames(Air_data) = 1949:1960
Air_data
Jan Feb Mar Apr May Jun
1949 112 118 132 129 121 135
1950 115 126 141 135 125 149
1951 145 150 178 163 172 178
1952 171 180 193 181 183 218
1953 196 196 236 235 229 243
1954 204 188 235 227 234 264
1955 242 233 267 269 270 315
1956 284 277 317 313 318 374
1957 315 301 356 348 355 422
1958 340 318 362 348 363 435
1959 360 342 406 396 420 472
1960 417 391 419 461 472 535
For "Mar" as the x group versus "Apr" as the y group.
For the following null hypothesis \(H_0\), and alternative hypothesis \(H_1\), with the level of significance \(\alpha=0.1\).
\(H_0:\) the population median of the paired differences is equal to 4 (\(m_d = 4\)).
\(H_1:\) the population median of the paired differences is greater than 4 (\(m_d > 4\), hence one-sided).
Because the level of significance is \(\alpha=0.1\), the level of confidence is \(1 - \alpha = 0.9\).
Dependent-samples Sign-Test
data: Air_data$Mar and Air_data$Apr
S = 7, p-value = 0.2744
alternative hypothesis: true median difference is greater than 4
90 percent confidence interval:
3.223434 Inf
sample estimates:
median of x-y
7
Achieved and Interpolated Confidence Intervals:
Conf.Level L.E.pt U.E.pt
Lower Achieved CI 0.8062 4.0000 Inf
Interpolated CI 0.9000 3.2234 Inf
Upper Achieved CI 0.9270 3.0000 Inf
P-value: With the p-value (\(p = 0.2744\)) being greater than the level of significance 0.1, we fail to reject the null hypothesis that the population median of the paired differences is equal to 4.
\(S\) T-statistic: \(n_d = 12 - 1 = 11\) because one paired difference is \(4\). With \(n_d = 11\), and test statistics value (\(S = 7\)) being less than \(\text{qbinom(0.9, 11, 0.5)+1}=9\), or being outside \(9 \text{ to } 11\), we fail to reject the null hypothesis that the population median of the paired differences is equal to 4.
Confidence Interval: With the null hypothesis median of the paired differences (\(m_d = 4\)) being inside the confidence interval, \([3.223434, \infty)\), we fail to reject the null hypothesis that the population median of the paired differences is equal to 4.
For "Jun" as the x group versus "May" as the y group.
For the following null hypothesis \(H_0\), and alternative hypothesis \(H_1\), with the level of significance \(\alpha=0.05\).
\(H_0:\) the population median of the paired differences is equal to 65 (\(m_d = 65\)).
\(H_1:\) the population median of the paired differences is less than 65 (\(m_d < 65\), hence one-sided).
Because the level of significance is \(\alpha=0.05\), the level of confidence is \(1 - \alpha = 0.95\).
Dependent-samples Sign-Test
data: Air_data$Jun and Air_data$May
S = 2, p-value = 0.01929
alternative hypothesis: true median difference is less than 65
95 percent confidence interval:
-Inf 58.99727
sample estimates:
median of x-y
40
Achieved and Interpolated Confidence Intervals:
Conf.Level L.E.pt U.E.pt
Lower Achieved CI 0.9270 -Inf 56.0000
Interpolated CI 0.9500 -Inf 58.9973
Upper Achieved CI 0.9807 -Inf 63.0000
P-value: With the p-value (\(p = 0.01929\)) being less than the level of significance 0.05, we reject the null hypothesis that the population median of the paired differences is equal to 65.
\(S\) T-statistic: With \(n_d = 12\), and the test statistics value (\(S = 2\)) being less than or equal to \(\text{qbinom(0.05, 12, 0.5)-1}=2\), or being within \(0 \text{ to } 2\), we reject the null hypothesis that the population median of the paired differences is equal to 65.
Confidence Interval: With the null hypothesis median value (\(m_d = 65\)) being outside the confidence interval, \((-\infty, 58.99727]\), we reject the null hypothesis that the population median of the paired differences is equal to 65.
Here for a sign test for paired samples, we show how to get the test
statistics (or S-value), and p-values from the SIGN.test()
function in R, or by written code.
data_x = Air_data$Jun; data_y = Air_data$May
sgnt_object = SIGN.test(data_x, data_y,
alternative = "two.sided",
md = 60, conf.level = 0.95)
sgnt_object
Dependent-samples Sign-Test
data: data_x and data_y
S = 3, p-value = 0.146
alternative hypothesis: true median difference is not equal to 60
95 percent confidence interval:
15.06364 62.25545
sample estimates:
median of x-y
40
Achieved and Interpolated Confidence Intervals:
Conf.Level L.E.pt U.E.pt
Lower Achieved CI 0.8540 24.0000 56.0000
Interpolated CI 0.9500 15.0636 62.2555
Upper Achieved CI 0.9614 14.0000 63.0000
\[S = \sum_{i=1}^n I_{([x_i-y_i]-m_0)>0},\]
which is the number of observed paired differences greater than the null hypothesis median.
S
3
[1] 3
Same as:
[1] 3
with \(n_d\) as the number of non-zero \(((x_i-y_i) - m_0)\).
For \(X \sim Binomial(n_d, 0.5)\)
Two-tailed:
For \(S = n_d/2\), \(Pvalue = 1\).
For \(S > n_d/2\), \(Pvalue = 2 * P(X \geq S) = 2 * \sum_{x=S}^{n_d} P(X=x)\).
For \(S < n_d/2\), \(Pvalue = 2 * P(X \leq S) = 2 * \sum_{x=0}^{S} P(X=x)\).
One-tailed:
For right-tail, \(Pvalue = P(X \geq S) = \sum_{x=S}^{n_d} P(X=x)\) or for left-tail, \(Pvalue = P(X \leq S) = \sum_{x=0}^{S} P(X=x)\).
[1] 0.1459961
Same as:
[1] 12
Note that the p-value depends on the \(\text{test statistics}\) (\(S = 3 < n_d/2 = 6\)). We also use the
distribution function pbinom()
for the binomial
distribution in R.
[1] 0.1459961
[1] 0.1459961
One-tailed example:
The feedback form is a Google form but it does not collect any personal information.
Please click on the link below to go to the Google form.
Thank You!
Go to Feedback Form
Copyright © 2020 - 2024. All Rights Reserved by Stats Codes