{"id":10869,"date":"2019-11-29T00:00:13","date_gmt":"2019-11-28T22:00:13","guid":{"rendered":"https:\/\/www.datanovia.com\/en\/?post_type=dt_lessons&#038;p=10869"},"modified":"2019-11-29T00:00:13","modified_gmt":"2019-11-28T22:00:13","slug":"sign-test-in-r","status":"publish","type":"dt_lessons","link":"https:\/\/www.datanovia.com\/en\/lessons\/sign-test-in-r\/","title":{"rendered":"Sign Test in R"},"content":{"rendered":"<div id=\"rdoc\">\n<p>The <strong>sign test<\/strong> is used to compare the medians of paired or matched observations. It is an alternative to the <em>paired-samples t-test<\/em> (Chapter @ref(t-test)) and the <em>Wilcoxon signed-rank test<\/em> (Chapter @ref(wilcoxon-test)) in the situation, where the distribution of differences between paired data values is neither normal (in t-test) nor symmetrical (in Wilcoxon test).<\/p>\n<div class=\"warning\">\n<p>Note that, the sign test does not make any assumptions about the data distributions. However, it will most likely be less powerful compared to the Wilcoxon test and the t-test.<\/p>\n<p>Therefore, if the distribution of the differences between the two paired groups is symmetrical in shape, you could consider using the more powerful Wilcoxon signed-rank test instead of the sign test.<\/p>\n<\/div>\n<p>In this chapter, you will learn how to compute paired-samples sign test using the R function <code>sign_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=\"#demo-dataset\">Demo dataset<\/a><\/li>\n<li><a href=\"#statistical-hypotheses\">Statistical hypotheses<\/a><\/li>\n<li><a href=\"#summary-statistics\">Summary statistics<\/a><\/li>\n<li><a href=\"#visualization\">Visualization<\/a><\/li>\n<li><a href=\"#computation\">Computation<\/a><\/li>\n<li><a href=\"#report\">Report<\/a><\/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=\"demo-dataset\" class=\"section level2\">\n<h2>Demo dataset<\/h2>\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 data\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=\"statistical-hypotheses\" class=\"section level2\">\n<h2>Statistical hypotheses<\/h2>\n<p>The paired-samples sign test evaluates whether the median of paired differences is statistically significantly different to 0.<\/p>\n<ul>\n<li><strong>Null hypotheses, H0<\/strong>: median of the paired differences = 0<\/li>\n<li><strong>Alternative hypotheses, Ha<\/strong>: median of the paired differences is different to 0<\/li>\n<\/ul>\n<\/div>\n<div id=\"summary-statistics\" class=\"section level2\">\n<h2>Summary statistics<\/h2>\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\" class=\"section level2\">\n<h2>Visualization<\/h2>\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\/041-sign-test-paired-sign-test-box-plot-1.png\" width=\"364.8\" \/><\/p>\n<\/div>\n<div id=\"computation\" class=\"section level2\">\n<h2>Computation<\/h2>\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  sign_test(weight ~ group) %&gt;%\r\n  add_significance()\r\nstat.test<\/code><\/pre>\n<pre><code>## # A tibble: 1 x 9\r\n##   .y.    group1 group2    n1    n2 statistic    df       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;dbl&gt; &lt;chr&gt;   \r\n## 1 weight after  before    10    10        10    10 0.00195 **<\/code><\/pre>\n<\/div>\n<div id=\"report\" class=\"section level2\">\n<h2>Report<\/h2>\n<p>We could report the results as follow:<\/p>\n<p>The median weight of the mice before treatment is significantly different from the median weight after treatment using sign test, p-value = 0.002.<\/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(\r\n  subtitle = get_test_label(stat.test, detailed= TRUE)\r\n  )<\/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\/041-sign-test-box-plot-with-p-values-1.png\" width=\"364.8\" \/><\/p>\n<\/div>\n<div id=\"summary\" class=\"section level2\">\n<h2>Summary<\/h2>\n<p>This chapter describes how to compute and report the Sign test in R.<\/p>\n<\/div>\n<\/div>\n<p><!--end rdoc--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Sign test is used to compare the medians of paired or matched observations. It is an alternative to the paired-samples t-test and the Wilcoxon signed-rank test in the situation, where the distribution of differences between paired data values is neither normal (in t-test) nor symmetrical (in Wilcoxon test). In this chapter, you will learn how to compute paired-samples sign test in R <\/p>\n","protected":false},"author":1,"featured_media":9105,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","class_list":["post-10869","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>Sign 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\/sign-test-in-r\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Sign Test in R: The Ultimate Guide - Datanovia\" \/>\n<meta property=\"og:description\" content=\"The Sign test is used to compare the medians of paired or matched observations. It is an alternative to the paired-samples t-test and the Wilcoxon signed-rank test in the situation, where the distribution of differences between paired data values is neither normal (in t-test) nor symmetrical (in Wilcoxon test). In this chapter, you will learn how to compute paired-samples sign test in R\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.datanovia.com\/en\/lessons\/sign-test-in-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Datanovia\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X25994934_559944101012205_930034181282162827_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=\"3 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\/sign-test-in-r\/\",\"url\":\"https:\/\/www.datanovia.com\/en\/lessons\/sign-test-in-r\/\",\"name\":\"Sign Test in R: The Ultimate Guide - Datanovia\",\"isPartOf\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/sign-test-in-r\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/sign-test-in-r\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X25994934_559944101012205_930034181282162827_n.jpg\",\"datePublished\":\"2019-11-28T22:00:13+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/sign-test-in-r\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.datanovia.com\/en\/lessons\/sign-test-in-r\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/sign-test-in-r\/#primaryimage\",\"url\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X25994934_559944101012205_930034181282162827_n.jpg\",\"contentUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X25994934_559944101012205_930034181282162827_n.jpg\",\"width\":1024,\"height\":512},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/sign-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\":\"Sign 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":"Sign 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\/sign-test-in-r\/","og_locale":"en_US","og_type":"article","og_title":"Sign Test in R: The Ultimate Guide - Datanovia","og_description":"The Sign test is used to compare the medians of paired or matched observations. It is an alternative to the paired-samples t-test and the Wilcoxon signed-rank test in the situation, where the distribution of differences between paired data values is neither normal (in t-test) nor symmetrical (in Wilcoxon test). In this chapter, you will learn how to compute paired-samples sign test in R","og_url":"https:\/\/www.datanovia.com\/en\/lessons\/sign-test-in-r\/","og_site_name":"Datanovia","og_image":[{"width":1024,"height":512,"url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X25994934_559944101012205_930034181282162827_n.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.datanovia.com\/en\/lessons\/sign-test-in-r\/","url":"https:\/\/www.datanovia.com\/en\/lessons\/sign-test-in-r\/","name":"Sign Test in R: The Ultimate Guide - Datanovia","isPartOf":{"@id":"https:\/\/www.datanovia.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.datanovia.com\/en\/lessons\/sign-test-in-r\/#primaryimage"},"image":{"@id":"https:\/\/www.datanovia.com\/en\/lessons\/sign-test-in-r\/#primaryimage"},"thumbnailUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X25994934_559944101012205_930034181282162827_n.jpg","datePublished":"2019-11-28T22:00:13+00:00","breadcrumb":{"@id":"https:\/\/www.datanovia.com\/en\/lessons\/sign-test-in-r\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.datanovia.com\/en\/lessons\/sign-test-in-r\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.datanovia.com\/en\/lessons\/sign-test-in-r\/#primaryimage","url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X25994934_559944101012205_930034181282162827_n.jpg","contentUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X25994934_559944101012205_930034181282162827_n.jpg","width":1024,"height":512},{"@type":"BreadcrumbList","@id":"https:\/\/www.datanovia.com\/en\/lessons\/sign-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":"Sign 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\/10869","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=10869"}],"version-history":[{"count":0,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/dt_lessons\/10869\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/media\/9105"}],"wp:attachment":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/media?parent=10869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}