Wilcoxon Signed-Rank Test in Statistics Using R

 

Experiment: Wilcoxon Signed-Rank Test in Statistics Using R

1. Experiment Title

Hypothesis Testing Using the Wilcoxon Signed-Rank Test in R


2. Aim

To study and perform the Wilcoxon Signed-Rank Test using R to determine whether there is a significant difference between two related samples when the assumptions of the paired t-test are not satisfied.


3. Objectives

After completing this experiment, students should be able to:

  1. Understand the purpose of the Wilcoxon Signed-Rank Test.
  2. Identify situations where the test is appropriate.
  3. Formulate null and alternative hypotheses.
  4. Calculate signed ranks manually.
  5. Perform the Wilcoxon Signed-Rank Test using R.
  6. Interpret the test statistic and p-value.
  7. Make a statistical decision based on the test result.

4. Theory

4.1 What is the Wilcoxon Signed-Rank Test?

The Wilcoxon Signed-Rank Test is a non-parametric statistical test used to compare two related or paired samples.

It is commonly used as an alternative to the paired t-test when:

  • The data are not normally distributed.
  • The sample size is small.
  • The differences between paired observations cannot reasonably be assumed to be normally distributed.
  • The data can be ranked.

Examples

The test can be used to compare:

  • Student marks before and after a training program.
  • Patient measurements before and after a treatment.
  • Employee productivity before and after training.
  • Machine performance before and after maintenance.

The relationship between the two tests can be summarized as:

SituationAppropriate Test
Two related samples, normality assumption satisfied    Paired t-test
Two related samples, normality assumption not satisfied    Wilcoxon Signed-Rank Test

4.2 Hypotheses

For a two-sided Wilcoxon Signed-Rank Test:

Null Hypothesis

H0:H_0:

The median difference between the paired observations is zero.

There is no significant change between the two measurements.

Alternative Hypothesis

H1:H_1:

The median difference between the paired observations is not zero.

There is a significant difference between the two measurements.


5. Problem Statement

A teacher wants to determine whether a special training program improves student test scores.

The marks of 10 students are recorded before and after attending the training program.

Student   Before Training     After Training
14550
25055
34852
46065
55558
65257
75863
84751
96267
105460

Using a significance level of:

α=0.05\alpha=0.05

determine whether there is a significant difference between the student marks before and after the training program.


6. Manual Calculation

Step 1: Calculate the Differences

Calculate:

d=After−Befored = \text{After} - \text{Before}
StudentBeforeAfterDifference dd
14550+5
25055+5
34852+4
46065+5
55558+3
65257+5
75863+5
84751+4
96267+5
105460+6

Step 2: Remove Zero Differences

Any pair with:

d=0d=0

is removed from the calculation.

In this example, there are no zero differences.


Step 3: Calculate Absolute Differences

Calculate:

∣d∣|d|

and rank them from smallest to largest.

Because some differences are equal, average ranks are assigned to tied values.

| Student | Difference dd | ∣d∣|d| | Rank | Sign |

1             +5                     5      6.5      + 
2             +5                     5      6.5      + 
3             +4                     4     2.5       + 
4             +5                     5     7          + 
5             +3                     3     1          + 
6             +5                     5     6.5       + 
7             +5                     5    6.5        + 
8             +4                     4     2.5       + 
9             +5                     5     7          + 
10           +6                     6    10         + 


Step 4: Calculate the Sum of Positive and Negative Ranks

Let:

W+=sum of positive ranksW^+=\text{sum of positive ranks} W−=sum of negative ranksW^-=\text{sum of negative ranks}

All differences are positive.

Therefore:

W+=1+2.5+2.5+(6×6.5)+10W^+=1+2.5+2.5+(6\times7)+10 W+=55W^+=58

and:

W−=0W^-=0



7. R Program

# Wilcoxon Signed-Rank Test

# Marks before training
before <- c(45, 50, 48, 60, 55,
            52, 58, 47, 62, 54)

# Marks after training
after <- c(50, 55, 52, 65, 58,
           57, 63, 51, 67, 60)


# Display the data

data <- data.frame(
  Student = 1:10,
  Before = before,
  After = after
)

print(data)


# Perform Wilcoxon Signed-Rank Test

result <- wilcox.test(after,
                      before,
                      paired = TRUE,
                      alternative = "two.sided",
                      conf.level = 0.95)


# Display test result

print(result)


# Significance level

alpha <- 0.05


# Decision based on p-value

if (result$p.value < alpha) {
  
  cat("\nDecision: Reject the Null Hypothesis\n")
  cat("Conclusion: There is a significant difference between the scores before and after training.\n")
  
} else {
  
  cat("\nDecision: Fail to Reject the Null Hypothesis\n")
  cat("Conclusion: There is not enough evidence to conclude that the scores differ significantly.\n")
}

8. Explanation of the R Function

The main R function used is:

wilcox.test()

For paired data:

wilcox.test(after, before, paired = TRUE)

Important Arguments

ArgumentMeaning
after        Measurements after the treatment
before        Measurements before the treatment
paired = TRUE        Specifies that observations are paired
alternative = "two.sided"        Tests for any significant difference
conf.level = 0.95        Uses a 95% confidence level

9. Expected Output

Student Before After
1        1     45    50
2        2     50    55
3        3     48    52
4        4     60    65
5        5     55    58
6        6     52    57
7        7     58    63
8        8     47    51
9        9     62    67
10      10     54    60

	Wilcoxon signed rank exact test

data:  after and before
V = 55, p-value = 0.001953
alternative hypothesis: true location shift is not equal to 0


Decision: Reject the Null Hypothesis
Conclusion: There is a significant difference between the scores before and after training.



10. Interpretation

The Wilcoxon Signed-Rank Test examines whether the median difference between the paired observations is significantly different from zero.

The decision is based on the p-value:

ConditionDecision
p-value < 0.05        Reject H0H_0
p-value ≥ 0.05        Fail to reject H0H_0

In this example, all students show an increase in their marks after training. The Wilcoxon test helps determine whether this improvement is statistically significant.


11. Result

Thus, the Wilcoxon Signed-Rank Test was performed using R to compare two related samples. The differences between paired observations were ranked according to their absolute values, and the signed ranks were used to test whether the median difference was significantly different from zero. The result was interpreted using the p-value at a 5% significance level.




Comments

Popular posts from this blog

Statistical Methods Lab ( R Language) PCCBL308 Semester 3 KTU BTech CB and CU 2024 Scheme - Dr Binu V P

Programs in R - using control statements - Assignment 2

Programs to try using Functions in R - Assignment 3