# creating a data frame with "before" and "after" values
# each row is an individual with a measurement taken "before" and "after"
df <- tibble(before = c(1.2, 2.0, 0.8, 0.6, 1.3, 3.2, 0.9),
after = c(1.6, 2.8, 0.7, 0.6, 1.5, 2.7, 1.1)) 1. Description
In this practice problem, you’ll do a Wilcoxon signed rank test comparing paired samples to each other by hand, then double check your work with R. This is the non-parametric version of the paired t-test.
2. Steps
a. Values
Start with these values:
| Before | After |
|---|---|
| 1.2 | 1.6 |
| 2.0 | 2.8 |
| 0.8 | 0.7 |
| 0.6 | 0.6 |
| 1.3 | 1.5 |
| 3.2 | 2.7 |
| 0.9 | 1.1 |
b. Calculate the differences between samples
Then, calculate the differences between those values. You should get something that looks like this:
| Differences |
|---|
| -0.4 |
| -0.8 |
| 0.1 |
| 0.0 |
| -0.2 |
| 0.5 |
| -0.2 |
c. Take out 0, arrange by magnitude
Then, omit all observations of 0 and arrange your numbers by magnitude. You should get something that looks like this:
| Ordered magnitude |
|---|
| 0.1 |
| -0.2 |
| -0.2 |
| -0.4 |
| 0.5 |
| -0.8 |
d. Give each value a “sign” and a “rank”
This is the “signed rank” part of the test: assign each value a sign (+ or -), then rank (1, 2, 3, 4, etc.) You should get something that looks like this:
| Ordered magnitude | Signed rank |
|---|---|
| 0.1 | + 1 |
| -0.2 | - 2 |
| -0.2 | - 2 |
| -0.4 | - 4 |
| 0.5 | + 5 |
| -0.8 | - 6 |
e. Sum the magnitudes of the + and - values
Sum the magnitudes of the + and - values. This will be the -W and +W statistic. You then choose the lower statistic.
In this case, you should get:
+W = 1 + 5 = 6
-W = 2 + 2 + 4 + 6 = 14
Pick the lowest one: +W = 6.
e. Run the test in R to check your work.
Start with this data frame:
Then run a Wilcoxon signed rank test (note the paired = TRUE argument).
wilcox.test(x = df$before,
y = df$after,
paired = TRUE) # argument for a paired testWarning in wilcox.test.default(x = df$before, y = df$after, paired = TRUE):
cannot compute exact p-value with zeroes
Wilcoxon signed rank test with continuity correction
data: df$before and df$after
V = 6, p-value = 0.4017
alternative hypothesis: true location shift is not equal to 0
f. Try a one-sample test.
Compare the following values to \(\mu = 7\):
| Sample |
|---|
| 2.8 |
| 8.3 |
| 4.5 |
| 6.7 |
| 8.1 |
| 7.2 |
| 7.8 |
First, calculate the difference between each observation and the \(\mu\), which is 7:
| Sample | Difference |
|---|---|
| 2.8 | -4.2 |
| 8.3 | 1.3 |
| 4.5 | -2.5 |
| 6.7 | -0.3 |
| 8.1 | 1.1 |
| 7.2 | 0.2 |
| 7.8 | 0.8 |
Then order each difference and assign a sign and rank:
| Ordered magnitude | Signed rank |
|---|---|
| 0.2 | + 1 |
| -0.3 | - 2 |
| 0.8 | + 3 |
| 1.1 | + 4 |
| 1.3 | + 5 |
| -2.5 | - 6 |
| -4.2 | - 7 |
Then sum the magnitudes:
+W = 1 + 3 + 4 + 5 = 13
-W = 2 + 6 + 7 = 15
For this example, W = 13.
Then, double check your work in R:
wilcox.test(c(2.8, 8.3, 4.5, 6.7, 8.1, 7.2, 7.8),
mu = 7)
Wilcoxon signed rank exact test
data: c(2.8, 8.3, 4.5, 6.7, 8.1, 7.2, 7.8)
V = 13, p-value = 0.9375
alternative hypothesis: true location is not equal to 7