Site icon R-bloggers

Wilcoxon Signed Rank Test in R

[This article was first published on Methods – finnstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Wilcoxon Signed Rank Test is based on the direction of differences and magnitudes. This test is more sensitive and powerful than an ordinary sign test.

The ordinary sign test was based only on the direction of differences ignoring their magnitudes.

How can one apply Wilcoxon signed-rank test for matched paired samples?

Under matched paired samples, the differences d within n paired values (xi,yi) for i= 1,2,3….n are assumed to have come from continuous and symmetric population differences.

If Md is the median of the population of differences and expected to possess a known value Md0, we test

H0: Md= Md0vs H1: Md≠Md0

LSTM Network in R » Recurrent Neural network »

This test comes under the nonparametric test used to compare related samples, matched samples, or repeated measurements on a single sample to assess whether their population means ranks differ.

Majorly used when your data are non-normally distributed.

The paired samples Wilcoxon test (also known as Wilcoxon signed-rank test) is a non-parametric alternative to paired t-test used to compare paired data.

The R function wilcox.test() can be used as follow:

How to clean the datasets in R? » janitor Data Cleansing »

wilcox.exact(x, y, paired = TRUE, alternative = "two.sided")

We want to example the efficacy of the product after treatment applied.

Pre <-c(19,20,13,14,15,16,17,18,19,20)
Post <-c(20,22,15,16,15,19,22,23,19,18)

Now create a data frame

data <- data.frame(Pre,Post)

The ultimate aim is to identify the any significant difference in the median values of Pre and Post treatment?

Market Basket Analysis in R » What Goes With What »

Visualization

Before testing the significance, let’s visualize the data.

library("ggpubr")
library(reshape)
data1<-melt(data)
ggboxplot(data1, x = "variable", y = "value",
          color = "variable", palette = c("#00AFBB", "#E7B800"), fill = "variable",
          order = c("Pre", "Post"),
          ylab = "Value", xlab = "Groups")

Summary

Let’s calculate the summary statistics.

library(psych)
describeBy(data1,data1$variable)
Descriptive statistics by group 
 group: Pre
           vars  n mean   sd median trimmed  mad min max range  skew kurtosis  se
 variable*    1 10  1.0 0.00    1.0    1.00 0.00   1   1     0   NaN      NaN 0.0
 value        2 10 17.1 2.51   17.5   17.25 2.97  13  20     7 -0.29    -1.59 0.8
 group: Post
           vars  n mean   sd median trimmed  mad min max range  skew kurtosis   se
 variable*    1 10  2.0 0.00      2    2.00 0.00   2   2     0   NaN      NaN 0.00
 value        2 10 18.9 2.92     19   18.88 4.45  15  23     8 -0.06    -1.63 0.92

Post median value is higher than the Pre value.

Let’s calculate the p-value based on exactRankTests package.

library(exactRankTests)
res <- wilcox.exact(Pre,Post, paired = TRUE)
res

Exact Wilcoxon signed rank test

Timeseries analysis in R » Decomposition, & Forecasting »

data:  Pre and Post
V = 3.5, p-value = 0.04688
alternative hypothesis: true mu is not equal to 0

print only the p-value

res$p.value
0.04688

If you want to test whether the median value before treatment is less than the median value post-treatment, type this:

wilcox.exact(Pre, Post, data = data, paired = TRUE, alternative = "less")

Result

data:  Pre and Post
V = 3.5, p-value = 0.02344
alternative hypothesis: true mu is less than 0

If you want to test whether the median value before treatment is greater than the median value post-treatment, type this:

Principal component analysis (PCA) in R »

wilcox.exact(Pre, Post, data = data, paired = TRUE, alternative = "greater")

The exact Wilcoxon signed-rank test

data:  Pre and Post
V = 3.5, p-value = 0.9922
alternative hypothesis: true mu is greater than 0

Conclusion

Based on the test, significant improvement was observed in the tested sample performance over the pre-value.

Animated Graph GIF with gganimate & ggplot »

The post Wilcoxon Signed Rank Test in R appeared first on finnstats.

To leave a comment for the author, please follow the link and comment on their blog: Methods – finnstats.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.