{"id":10866,"date":"2019-11-28T23:54:00","date_gmt":"2019-11-28T21:54:00","guid":{"rendered":"https:\/\/www.datanovia.com\/en\/?post_type=dt_lessons&#038;p=10866"},"modified":"2020-05-06T23:43:31","modified_gmt":"2020-05-06T22:43:31","slug":"wilcoxon-test-in-r","status":"publish","type":"dt_lessons","link":"https:\/\/www.datanovia.com\/en\/lessons\/wilcoxon-test-in-r\/","title":{"rendered":"Wilcoxon Test in R"},"content":{"rendered":"<div id=\"rdoc\">\n<p>The <strong>Wilcoxon test<\/strong> is a non-parametric alternative to the t-test for comparing two means. It\u2019s particularly recommended in a situation where the data are not normally distributed.<\/p>\n<p>Like the t-test, the Wilcoxon test comes in two forms, one-sample and two-samples. They are used in more or less the exact same situations as the corresponding t-tests.<\/p>\n<div class=\"warning\">\n<p>Note that, the sample size should be at least 6. Otherwise, the Wilcoxon test cannot become significant.<\/p>\n<\/div>\n<p>In this chapter, you will learn how to compute the different types of Wilcoxon tests in R, including:<\/p>\n<ul>\n<li><strong>One-sample Wilcoxon signed rank test<\/strong><\/li>\n<li><strong>Wilcoxon rank sum test<\/strong> and<\/li>\n<li><strong>Wilcoxon signed rank test on paired samples<\/strong><\/li>\n<li><strong>Check Wilcoxon test assumptions<\/strong><\/li>\n<li><strong>Calculate and report Wilcoxon test effect size (r value)<\/strong>.<\/li>\n<\/ul>\n<p>The effect size <code>r<\/code> is calculated as <code>Z<\/code> statistic divided by the square root of the sample size (N) (<code>Z\/sqrt(N)<\/code>). The <code>Z<\/code> value is extracted from either <code>coin::wilcoxsign_test()<\/code> (case of one- or paired-samples test) or <code>coin::wilcox_test()<\/code> (case of independent two-samples test).<\/p>\n<p>Note that <code>N<\/code> corresponds to the total sample size for independent-samples test and to the total number of pairs for paired samples test. The <code>r<\/code> value varies from 0 to close to 1. The interpretation values for <code>r<\/code> commonly in published literature are: 0.10 - &lt; 0.3 (small effect), 0.30 - &lt; 0.5 (moderate effect) and &gt;= 0.5 (large effect).<\/p>\n<p>We\u2019ll use the pipe-friendly function <code>wilcox_test()<\/code> [rstatix package].<\/p>\n<p>Contents:<\/p>\n<div id=\"TOC\">\n<ul>\n<li><a href=\"#prerequisites\">Prerequisites<\/a><\/li>\n<li><a href=\"#one-sample-wilcoxon-signed-rank-test\">One-sample Wilcoxon signed rank test<\/a>\n<ul>\n<li><a href=\"#demo-data\">Demo data<\/a><\/li>\n<li><a href=\"#summary-statistics\">Summary statistics<\/a><\/li>\n<li><a href=\"#visualization\">Visualization<\/a><\/li>\n<li><a href=\"#assumptions-and-preliminary-tests\">Assumptions and preliminary tests<\/a><\/li>\n<li><a href=\"#computation\">Computation<\/a><\/li>\n<li><a href=\"#effect-size\">Effect size<\/a><\/li>\n<li><a href=\"#report\">Report<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#two-sample-wilcoxon-test\">Wilcoxon rank sum test<\/a>\n<ul>\n<li><a href=\"#demo-data-1\">Demo data<\/a><\/li>\n<li><a href=\"#summary-statistics-1\">Summary statistics<\/a><\/li>\n<li><a href=\"#visualization-1\">Visualization<\/a><\/li>\n<li><a href=\"#computation-1\">Computation<\/a><\/li>\n<li><a href=\"#effect-size-1\">Effect size<\/a><\/li>\n<li><a href=\"#report-1\">Report<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#signed-rank-test-on-paired-samples\">Wilcoxon signed rank test on paired samples<\/a>\n<ul>\n<li><a href=\"#demo-dataset\">Demo dataset<\/a><\/li>\n<li><a href=\"#summary-statistics-2\">Summary statistics<\/a><\/li>\n<li><a href=\"#visualization-2\">Visualization<\/a><\/li>\n<li><a href=\"#assumptions-and-preliminary-tests-1\">Assumptions and preliminary tests<\/a><\/li>\n<li><a href=\"#computation-2\">Computation<\/a><\/li>\n<li><a href=\"#effect-size-2\">Effect size<\/a><\/li>\n<li><a href=\"#report-2\">Report<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#summary\">Summary<\/a><\/li>\n<\/ul>\n<\/div>\n<div class='dt-sc-hr-invisible-medium  '><\/div>\n<div class='dt-sc-ico-content type1'><div class='custom-icon' ><a href='https:\/\/www.datanovia.com\/en\/product\/practical-statistics-in-r-for-comparing-groups-numerical-variables\/' target='_blank'><span class='fa fa-book'><\/span><\/a><\/div><h4><a href='https:\/\/www.datanovia.com\/en\/product\/practical-statistics-in-r-for-comparing-groups-numerical-variables\/' target='_blank'> Related Book <\/a><\/h4>Practical Statistics in R II - Comparing Groups: Numerical Variables<\/div>\n<div class='dt-sc-hr-invisible-medium  '><\/div>\n<div id=\"prerequisites\" class=\"section level2\">\n<h2>Prerequisites<\/h2>\n<p>Make sure that you have installed the following R packages:<\/p>\n<ul>\n<li><code>tidyverse<\/code> for data manipulation and visualization<\/li>\n<li><code>ggpubr<\/code> for creating easily publication ready plots<\/li>\n<li><code>rstatix<\/code> provides pipe-friendly R functions for easy statistical analyses<\/li>\n<li><code>datarium<\/code>: contains required datasets for this chapter<\/li>\n<\/ul>\n<p>Start by loading the following required packages:<\/p>\n<pre class=\"r\"><code>library(tidyverse)\r\nlibrary(rstatix)\r\nlibrary(ggpubr)<\/code><\/pre>\n<\/div>\n<div id=\"one-sample-wilcoxon-signed-rank-test\" class=\"section level2\">\n<h2>One-sample Wilcoxon signed rank test<\/h2>\n<p>The one-sample Wilcoxon signed rank test is used to assess whether the median of the sample is equal to a known standard or theoretical value. This is a non-parametric equivalent of one-sample t-test.<\/p>\n<div id=\"demo-data\" class=\"section level3\">\n<h3>Demo data<\/h3>\n<p>Demo dataset: <code>mice<\/code> [in datarium package]. Contains the weight of 10 mice:<\/p>\n<pre class=\"r\"><code># Load and inspect the data\r\ndata(mice, package = \"datarium\")\r\nhead(mice, 3)<\/code><\/pre>\n<pre><code>## # A tibble: 3 x 2\r\n##   name  weight\r\n##   &lt;chr&gt;  &lt;dbl&gt;\r\n## 1 M_1     18.9\r\n## 2 M_2     19.5\r\n## 3 M_3     23.1<\/code><\/pre>\n<\/div>\n<div id=\"summary-statistics\" class=\"section level3\">\n<h3>Summary statistics<\/h3>\n<p>Compute the median and the interquartile range (IQR):<\/p>\n<pre class=\"r\"><code>mice %&gt;% get_summary_stats(weight, type = \"median_iqr\")<\/code><\/pre>\n<pre><code>## # A tibble: 1 x 4\r\n##   variable     n median   iqr\r\n##   &lt;chr&gt;    &lt;dbl&gt;  &lt;dbl&gt; &lt;dbl&gt;\r\n## 1 weight      10   19.8   1.8<\/code><\/pre>\n<\/div>\n<div id=\"visualization\" class=\"section level3\">\n<h3>Visualization<\/h3>\n<p>Create a box plot to visualize the distribution of mice weights. Add also jittered points to show individual observations. The big dot represents the mean point.<\/p>\n<pre class=\"r\"><code>bxp &lt;- ggboxplot(\r\n  mice$weight, width = 0.5, add = c(\"mean\", \"jitter\"), \r\n  ylab = \"Weight (g)\", xlab = FALSE\r\n  )\r\nbxp<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/r-statistics-2-comparing-groups-means\/figures\/036-wilcoxon-test-box-plot-one-sample-1.png\" width=\"288\" \/><\/p>\n<\/div>\n<div id=\"assumptions-and-preliminary-tests\" class=\"section level3\">\n<h3>Assumptions and preliminary tests<\/h3>\n<p>The Wilcoxon signed-rank test assumes that the data are distributed symmetrically around the median. This can be checked by visual inspection using histogram and density distribution.<\/p>\n<p>Create a histogram: As we have only 10 individuals in our data, we specify the option <code>bins = 4<\/code> instead of 30 (default).<\/p>\n<pre class=\"r\"><code>gghistogram(mice, x = \"weight\", y = \"..density..\", \r\n            fill = \"steelblue\",bins = 4, add_density = TRUE)<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/r-statistics-2-comparing-groups-means\/figures\/036-wilcoxon-test-histogram-one-sample-1.png\" width=\"336\" \/><\/p>\n<div class=\"success\">\n<p>From the plot above, it can be seen that the <code>weight<\/code> data are approximately symmetrical (you should not expect them to be perfect, particularly when you have smaller numbers of samples in your study). Therefore, we can use the Wilcoxon signed-rank test to analyse our data.<\/p>\n<\/div>\n<div class=\"warning\">\n<p>Note that, in the situation where your data is not symmetrically distributed, you could consider performing a <strong>sign test<\/strong>, instead of running the Wilcoxon signed-rank test.<\/p>\n<p>The sign test does not make the assumption of a symmetrically-shaped distribution. However, it will most likely be less powerful compared to the Wilcoxon test.<\/p>\n<\/div>\n<\/div>\n<div id=\"computation\" class=\"section level3\">\n<h3>Computation<\/h3>\n<p>We want to know, whether the median weight of the mice differs from 25g (two-tailed test)?<\/p>\n<pre class=\"r\"><code>stat.test &lt;- mice %&gt;% wilcox_test(weight ~ 1, mu = 25)\r\nstat.test<\/code><\/pre>\n<pre><code>## # A tibble: 1 x 6\r\n##   .y.    group1 group2         n statistic       p\r\n## * &lt;chr&gt;  &lt;chr&gt;  &lt;chr&gt;      &lt;int&gt;     &lt;dbl&gt;   &lt;dbl&gt;\r\n## 1 weight 1      null model    10         0 0.00195<\/code><\/pre>\n<p>Note that, to compute one-sided wilcoxon test, you can specify the option <code>alternative<\/code>, which possible values can be \u201cgreater\u201d, \u201cless\u201d or \u201ctwo.sided\u201d.<\/p>\n<\/div>\n<div id=\"effect-size\" class=\"section level3\">\n<h3>Effect size<\/h3>\n<p>We\u2019ll use the R function <code>wilcox_effsize()<\/code> [rstatix]. It requires the <code>coin<\/code> package for computing the Z statistic.<\/p>\n<pre class=\"r\"><code>mice %&gt;%  wilcox_effsize(weight ~ 1, mu = 25)<\/code><\/pre>\n<pre><code>## # A tibble: 1 x 6\r\n##   .y.    group1 group2     effsize     n magnitude\r\n## * &lt;chr&gt;  &lt;chr&gt;  &lt;chr&gt;        &lt;dbl&gt; &lt;int&gt; &lt;ord&gt;    \r\n## 1 weight 1      null model   0.886    10 large<\/code><\/pre>\n<div class=\"success\">\n<p>A large effect size is detected, r = 0.89.<\/p>\n<\/div>\n<\/div>\n<div id=\"report\" class=\"section level3\">\n<h3>Report<\/h3>\n<p>We could report the result as follow:<\/p>\n<p>A Wilcoxon signed-rank test was computed to assess whether the recruited mice median weight was different to the population normal median weight (25g).<\/p>\n<p>The mice weight value were approximately symmetrically distributed, as assessed by a histogram with superimposed density curve.<\/p>\n<p>The measured mice median weight (19.8) was statistically significantly lower than the population median weight 25g (p = 0.002, effect size r = 0.89).<\/p>\n<p>Create a box plot with p-value:<\/p>\n<pre class=\"r\"><code>bxp + \r\n  labs(subtitle = get_test_label(stat.test, detailed = TRUE))<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/r-statistics-2-comparing-groups-means\/figures\/036-wilcoxon-test-one-sample-box-plot-with-p-value-1.png\" width=\"364.8\" \/><\/p>\n<p>Create a density plot with p-value:<\/p>\n<ul>\n<li>Red line corresponds to the observed median<\/li>\n<li>Blue line corresponds to the theoretical median<\/li>\n<\/ul>\n<pre class=\"r\"><code>ggdensity(mice, x = \"weight\", rug = TRUE, fill = \"lightgray\") +\r\n  scale_x_continuous(limits = c(15, 27)) +\r\n  stat_central_tendency(type = \"median\", color = \"red\", linetype = \"dashed\") +\r\n  geom_vline(xintercept = 25, color = \"blue\", linetype = \"dashed\") + \r\n  labs(subtitle = get_test_label(stat.test, detailed = TRUE))<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/r-statistics-2-comparing-groups-means\/figures\/036-wilcoxon-test-one-sample-density-with-p-value-1.png\" width=\"384\" \/><\/p>\n<\/div>\n<\/div>\n<div id=\"two-sample-wilcoxon-test\" class=\"section level2\">\n<h2>Wilcoxon rank sum test<\/h2>\n<p>The <strong>Wilcoxon rank sum test<\/strong> is a non-parametric alternative to the independent two samples t-test for comparing two independent groups of samples, in the situation where the data are not normally distributed.<\/p>\n<p>Synonymous: <em>Mann-Whitney test<\/em>, <em>Mann-Whitney U test<\/em>, <em>Wilcoxon-Mann-Whitney test<\/em> and <em>two-sample Wilcoxon test<\/em>.<\/p>\n<div id=\"demo-data-1\" class=\"section level3\">\n<h3>Demo data<\/h3>\n<p>Demo dataset: <code>genderweight<\/code> [in datarium package] containing the weight of 40 individuals (20 women and 20 men).<\/p>\n<p>Load the data and show some random rows by groups:<\/p>\n<pre class=\"r\"><code># Load the data\r\ndata(\"genderweight\", package = \"datarium\")\r\n# Show a sample of the data by group\r\nset.seed(123)\r\ngenderweight %&gt;% sample_n_by(group, size = 2)<\/code><\/pre>\n<pre><code>## # A tibble: 4 x 3\r\n##   id    group weight\r\n##   &lt;fct&gt; &lt;fct&gt;  &lt;dbl&gt;\r\n## 1 6     F       65.0\r\n## 2 15    F       65.9\r\n## 3 29    M       88.9\r\n## 4 37    M       77.0<\/code><\/pre>\n<\/div>\n<div id=\"summary-statistics-1\" class=\"section level3\">\n<h3>Summary statistics<\/h3>\n<p>Compute some summary statistics by groups: median and interquartile range.<\/p>\n<pre class=\"r\"><code>genderweight %&gt;%\r\n  group_by(group) %&gt;%\r\n  get_summary_stats(weight, type = \"median_iqr\")<\/code><\/pre>\n<pre><code>## # A tibble: 2 x 5\r\n##   group variable     n median   iqr\r\n##   &lt;fct&gt; &lt;chr&gt;    &lt;dbl&gt;  &lt;dbl&gt; &lt;dbl&gt;\r\n## 1 F     weight      20   62.9  2.33\r\n## 2 M     weight      20   86.3  4.59<\/code><\/pre>\n<\/div>\n<div id=\"visualization-1\" class=\"section level3\">\n<h3>Visualization<\/h3>\n<p>Visualize the data using box plots. Plot weight by groups.<\/p>\n<pre class=\"r\"><code>bxp &lt;- ggboxplot(\r\n  genderweight, x = \"group\", y = \"weight\", \r\n  ylab = \"Weight\", xlab = \"Groups\", add = \"jitter\"\r\n  )\r\nbxp<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/r-statistics-2-comparing-groups-means\/figures\/036-wilcoxon-test-box-plot-two-samples-1.png\" width=\"364.8\" \/><\/p>\n<\/div>\n<div id=\"computation-1\" class=\"section level3\">\n<h3>Computation<\/h3>\n<p>Question : Is there any significant difference between women and men median weights?<\/p>\n<pre class=\"r\"><code>stat.test &lt;- genderweight %&gt;% \r\n  wilcox_test(weight ~ group) %&gt;%\r\n  add_significance()\r\nstat.test<\/code><\/pre>\n<pre><code>## # A tibble: 1 x 8\r\n##   .y.    group1 group2    n1    n2 statistic        p p.signif\r\n##   &lt;chr&gt;  &lt;chr&gt;  &lt;chr&gt;  &lt;int&gt; &lt;int&gt;     &lt;dbl&gt;    &lt;dbl&gt; &lt;chr&gt;   \r\n## 1 weight F      M         20    20         0 1.45e-11 ****<\/code><\/pre>\n<\/div>\n<div id=\"effect-size-1\" class=\"section level3\">\n<h3>Effect size<\/h3>\n<pre class=\"r\"><code>genderweight %&gt;% wilcox_effsize(weight ~ group)<\/code><\/pre>\n<pre><code>## # A tibble: 1 x 7\r\n##   .y.    group1 group2 effsize    n1    n2 magnitude\r\n## * &lt;chr&gt;  &lt;chr&gt;  &lt;chr&gt;    &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;ord&gt;    \r\n## 1 weight F      M        0.855    20    20 large<\/code><\/pre>\n<div class=\"success\">\n<p>A large effect size is detected, r = 0.86.<\/p>\n<\/div>\n<\/div>\n<div id=\"report-1\" class=\"section level3\">\n<h3>Report<\/h3>\n<p>We could report the result as follow:<\/p>\n<p>The median weight in female group was 62.9 (IQR = 2.33), whereas the median in male group was 86.3 (IQR = 4.59). The Wilcoxon test showed that the difference was significant (p &lt; 0.0001, effect size r = 0.86).<\/p>\n<pre class=\"r\"><code>stat.test &lt;- stat.test %&gt;% add_xy_position(x = \"group\")\r\nbxp + \r\n  stat_pvalue_manual(stat.test, tip.length = 0) +\r\n  labs(subtitle = get_test_label(stat.test, detailed = TRUE))<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/r-statistics-2-comparing-groups-means\/figures\/036-wilcoxon-test-two-sample-box-plot-with-p-values-1.png\" width=\"412.8\" \/><\/p>\n<\/div>\n<\/div>\n<div id=\"signed-rank-test-on-paired-samples\" class=\"section level2\">\n<h2>Wilcoxon signed rank test on paired samples<\/h2>\n<p>The <strong>Wilcoxon signed rank test on paired sample<\/strong> is a non-parametric alternative to the paired samples t-test for comparing paired data. It\u2019s used when the data are not normally distributed.<\/p>\n<div id=\"demo-dataset\" class=\"section level3\">\n<h3>Demo dataset<\/h3>\n<p>Here, we\u2019ll use a demo dataset <code>mice2<\/code> [datarium package], which contains the weight of 10 mice before and after the treatment.<\/p>\n<pre class=\"r\"><code># Wide format\r\ndata(\"mice2\", package = \"datarium\")\r\nhead(mice2, 3)<\/code><\/pre>\n<pre><code>##   id before after\r\n## 1  1    187   430\r\n## 2  2    194   404\r\n## 3  3    232   406<\/code><\/pre>\n<pre class=\"r\"><code># Transform into long data: \r\n# gather the before and after values in the same column\r\nmice2.long &lt;- mice2 %&gt;%\r\n  gather(key = \"group\", value = \"weight\", before, after)\r\nhead(mice2.long, 3)<\/code><\/pre>\n<pre><code>##   id  group weight\r\n## 1  1 before    187\r\n## 2  2 before    194\r\n## 3  3 before    232<\/code><\/pre>\n<\/div>\n<div id=\"summary-statistics-2\" class=\"section level3\">\n<h3>Summary statistics<\/h3>\n<p>Compute some summary statistics by groups: median and interquartile range (IQR).<\/p>\n<pre class=\"r\"><code>mice2.long %&gt;%\r\n  group_by(group) %&gt;%\r\n  get_summary_stats(weight, type = \"median_iqr\")<\/code><\/pre>\n<pre><code>## # A tibble: 2 x 5\r\n##   group  variable     n median   iqr\r\n##   &lt;chr&gt;  &lt;chr&gt;    &lt;dbl&gt;  &lt;dbl&gt; &lt;dbl&gt;\r\n## 1 after  weight      10   405   28.3\r\n## 2 before weight      10   197.  19.2<\/code><\/pre>\n<\/div>\n<div id=\"visualization-2\" class=\"section level3\">\n<h3>Visualization<\/h3>\n<pre class=\"r\"><code>bxp &lt;- ggpaired(mice2.long, x = \"group\", y = \"weight\", \r\n         order = c(\"before\", \"after\"),\r\n         ylab = \"Weight\", xlab = \"Groups\")\r\nbxp<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/r-statistics-2-comparing-groups-means\/figures\/036-wilcoxon-test-paired-t-test-box-plot-1.png\" width=\"364.8\" \/><\/p>\n<\/div>\n<div id=\"assumptions-and-preliminary-tests-1\" class=\"section level3\">\n<h3>Assumptions and preliminary tests<\/h3>\n<p>The test assumes that differences between paired samples should be distributed symmetrically around the median.<\/p>\n<p>Compute the differences between pairs and create histograms:<\/p>\n<pre class=\"r\"><code>mice2 &lt;- mice2 %&gt;% mutate(differences = after - before)\r\ngghistogram(mice2, x = \"differences\", y = \"..density..\", \r\n            fill = \"steelblue\",bins = 5, add_density = TRUE)<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/r-statistics-2-comparing-groups-means\/figures\/036-wilcoxon-test-histogram-distribution-paired-wilcoxon-1.png\" width=\"336\" \/><\/p>\n<div class=\"success\">\n<p>From the plot above, it can be seen that the <code>differences<\/code> data are approximately symmetrical (you should not expect them to be perfect, particularly when you have smaller numbers of samples in your study). Therefore, we can use the Wilcoxon signed-rank test to analyse our data.<\/p>\n<\/div>\n<div class=\"warning\">\n<p>Note that, in the situation where your data is not symmetrically distributed, you could consider performing a <strong>sign test<\/strong>, instead of running the Wilcoxon signed-rank test.<\/p>\n<p>The sign test does not make the assumption of a symmetrically-shaped distribution. However, it will most likely be less powerful compared to the Wilcoxon test.<\/p>\n<\/div>\n<\/div>\n<div id=\"computation-2\" class=\"section level3\">\n<h3>Computation<\/h3>\n<p>Question : Is there any significant changes in the weights of mice after treatment?<\/p>\n<pre class=\"r\"><code>stat.test &lt;- mice2.long  %&gt;%\r\n  wilcox_test(weight ~ group, paired = TRUE) %&gt;%\r\n  add_significance()\r\nstat.test<\/code><\/pre>\n<pre><code>## # A tibble: 1 x 8\r\n##   .y.    group1 group2    n1    n2 statistic       p p.signif\r\n##   &lt;chr&gt;  &lt;chr&gt;  &lt;chr&gt;  &lt;int&gt; &lt;int&gt;     &lt;dbl&gt;   &lt;dbl&gt; &lt;chr&gt;   \r\n## 1 weight after  before    10    10        55 0.00195 **<\/code><\/pre>\n<\/div>\n<div id=\"effect-size-2\" class=\"section level3\">\n<h3>Effect size<\/h3>\n<pre class=\"r\"><code>mice2.long  %&gt;%\r\n  wilcox_effsize(weight ~ group, paired = TRUE)<\/code><\/pre>\n<pre><code>## # A tibble: 1 x 7\r\n##   .y.    group1 group2 effsize    n1    n2 magnitude\r\n## * &lt;chr&gt;  &lt;chr&gt;  &lt;chr&gt;    &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;ord&gt;    \r\n## 1 weight after  before   0.886    10    10 large<\/code><\/pre>\n<div class=\"success\">\n<p>A large effect size is detected, r = 0.89.<\/p>\n<\/div>\n<\/div>\n<div id=\"report-2\" class=\"section level3\">\n<h3>Report<\/h3>\n<p>From the output above, it can be concluded that the median weight of the mice before treatment is significantly different from the median weight after treatment with a p-value = 0.002, effect size r = 0.89.<\/p>\n<pre class=\"r\"><code>stat.test &lt;- stat.test %&gt;% add_xy_position(x = \"group\")\r\nbxp + \r\n  stat_pvalue_manual(stat.test, tip.length = 0) +\r\n  labs(subtitle = get_test_label(stat.test, detailed= TRUE))<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/r-statistics-2-comparing-groups-means\/figures\/036-wilcoxon-test-paired-test-box-plot-with-p-values-1.png\" width=\"364.8\" \/><\/p>\n<\/div>\n<\/div>\n<div id=\"summary\" class=\"section level2\">\n<h2>Summary<\/h2>\n<p>This chapter describes how to compare two means in R using the Wilcoxon test, which is a non-parametric alternative of the t-test.<\/p>\n<p>Quick start R codes, to compute the different Wilcoxon tests, are:<\/p>\n<pre class=\"r\"><code># One-sample Wilcoxon signed rank test\r\nmice %&gt;% wilcox_test(weight ~ 1, mu = 25)\r\n# Wilcoxon rank sum test: independent samples\r\ngenderweight %&gt;% wilcox_test(weight ~ group)\r\n# Wilcoxon signed rank test on paired samples\r\nmice2.long %&gt;% wilcox_test(weight ~ group, paired = TRUE)<\/code><\/pre>\n<p>Note that, to compute one-sided Wilcoxon tests, you can specify the option <code>alternative<\/code>, which possible values can be \u201cgreater\u201d, \u201cless\u201d or \u201ctwo.sided\u201d.<\/p>\n<\/div>\n<\/div>\n<p><!--end rdoc--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This chapter describes how to compute and interpret the wilcoxon test in R. This test is a non-parametric alternative to the t-test for comparing two means. You will learn how to compute the different types of Wilcoxon tests in R, including: One-sample Wilcoxon signed rank test, Wilcoxon rank sum test and Wilcoxon signed rank test on paired samples. We will also show how to check the assumptions and compute effect size.<\/p>\n","protected":false},"author":1,"featured_media":9093,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","class_list":["post-10866","dt_lessons","type-dt_lessons","status-publish","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Wilcoxon Test in R: The Ultimate Guide - Datanovia<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.datanovia.com\/en\/lessons\/wilcoxon-test-in-r\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Wilcoxon Test in R: The Ultimate Guide - Datanovia\" \/>\n<meta property=\"og:description\" content=\"This chapter describes how to compute and interpret the wilcoxon test in R. This test is a non-parametric alternative to the t-test for comparing two means. You will learn how to compute the different types of Wilcoxon tests in R, including: One-sample Wilcoxon signed rank test, Wilcoxon rank sum test and Wilcoxon signed rank test on paired samples. We will also show how to check the assumptions and compute effect size.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.datanovia.com\/en\/lessons\/wilcoxon-test-in-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Datanovia\" \/>\n<meta property=\"article:modified_time\" content=\"2020-05-06T22:43:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X26731206_567196586953623_460691146121416825_n.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"512\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/wilcoxon-test-in-r\/\",\"url\":\"https:\/\/www.datanovia.com\/en\/lessons\/wilcoxon-test-in-r\/\",\"name\":\"Wilcoxon Test in R: The Ultimate Guide - Datanovia\",\"isPartOf\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/wilcoxon-test-in-r\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/wilcoxon-test-in-r\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X26731206_567196586953623_460691146121416825_n.jpg\",\"datePublished\":\"2019-11-28T21:54:00+00:00\",\"dateModified\":\"2020-05-06T22:43:31+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/wilcoxon-test-in-r\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.datanovia.com\/en\/lessons\/wilcoxon-test-in-r\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/wilcoxon-test-in-r\/#primaryimage\",\"url\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X26731206_567196586953623_460691146121416825_n.jpg\",\"contentUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X26731206_567196586953623_460691146121416825_n.jpg\",\"width\":1024,\"height\":512},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/wilcoxon-test-in-r\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.datanovia.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Lessons\",\"item\":\"https:\/\/www.datanovia.com\/en\/lessons\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Wilcoxon Test in R\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#website\",\"url\":\"https:\/\/www.datanovia.com\/en\/\",\"name\":\"Datanovia\",\"description\":\"Data Mining and Statistics for Decision Support\",\"publisher\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.datanovia.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#organization\",\"name\":\"Datanovia\",\"url\":\"https:\/\/www.datanovia.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png\",\"contentUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png\",\"width\":98,\"height\":99,\"caption\":\"Datanovia\"},\"image\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Wilcoxon Test in R: The Ultimate Guide - Datanovia","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.datanovia.com\/en\/lessons\/wilcoxon-test-in-r\/","og_locale":"en_US","og_type":"article","og_title":"Wilcoxon Test in R: The Ultimate Guide - Datanovia","og_description":"This chapter describes how to compute and interpret the wilcoxon test in R. This test is a non-parametric alternative to the t-test for comparing two means. You will learn how to compute the different types of Wilcoxon tests in R, including: One-sample Wilcoxon signed rank test, Wilcoxon rank sum test and Wilcoxon signed rank test on paired samples. We will also show how to check the assumptions and compute effect size.","og_url":"https:\/\/www.datanovia.com\/en\/lessons\/wilcoxon-test-in-r\/","og_site_name":"Datanovia","article_modified_time":"2020-05-06T22:43:31+00:00","og_image":[{"width":1024,"height":512,"url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X26731206_567196586953623_460691146121416825_n.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.datanovia.com\/en\/lessons\/wilcoxon-test-in-r\/","url":"https:\/\/www.datanovia.com\/en\/lessons\/wilcoxon-test-in-r\/","name":"Wilcoxon Test in R: The Ultimate Guide - Datanovia","isPartOf":{"@id":"https:\/\/www.datanovia.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.datanovia.com\/en\/lessons\/wilcoxon-test-in-r\/#primaryimage"},"image":{"@id":"https:\/\/www.datanovia.com\/en\/lessons\/wilcoxon-test-in-r\/#primaryimage"},"thumbnailUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X26731206_567196586953623_460691146121416825_n.jpg","datePublished":"2019-11-28T21:54:00+00:00","dateModified":"2020-05-06T22:43:31+00:00","breadcrumb":{"@id":"https:\/\/www.datanovia.com\/en\/lessons\/wilcoxon-test-in-r\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.datanovia.com\/en\/lessons\/wilcoxon-test-in-r\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.datanovia.com\/en\/lessons\/wilcoxon-test-in-r\/#primaryimage","url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X26731206_567196586953623_460691146121416825_n.jpg","contentUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X26731206_567196586953623_460691146121416825_n.jpg","width":1024,"height":512},{"@type":"BreadcrumbList","@id":"https:\/\/www.datanovia.com\/en\/lessons\/wilcoxon-test-in-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.datanovia.com\/en\/"},{"@type":"ListItem","position":2,"name":"Lessons","item":"https:\/\/www.datanovia.com\/en\/lessons\/"},{"@type":"ListItem","position":3,"name":"Wilcoxon Test in R"}]},{"@type":"WebSite","@id":"https:\/\/www.datanovia.com\/en\/#website","url":"https:\/\/www.datanovia.com\/en\/","name":"Datanovia","description":"Data Mining and Statistics for Decision Support","publisher":{"@id":"https:\/\/www.datanovia.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.datanovia.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.datanovia.com\/en\/#organization","name":"Datanovia","url":"https:\/\/www.datanovia.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png","contentUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png","width":98,"height":99,"caption":"Datanovia"},"image":{"@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/"}}]}},"multi-rating":{"mr_rating_results":[]},"_links":{"self":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/dt_lessons\/10866","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/dt_lessons"}],"about":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/types\/dt_lessons"}],"author":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/comments?post=10866"}],"version-history":[{"count":2,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/dt_lessons\/10866\/revisions"}],"predecessor-version":[{"id":16085,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/dt_lessons\/10866\/revisions\/16085"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/media\/9093"}],"wp:attachment":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/media?parent=10866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}