Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • how to create a hexbins chart in R
    How to create a hexbin chart in R R
  • How to Get a Job as a Data Engineer
    How to Get a Job as a Data Engineer? R
  • How to Replace String in Column in R
    How to Replace String in Column using R R
  • Algorithm Classifications in Machine Learning
    Algorithm Classifications in Machine Learning Machine Learning
  • Replace NA with Zero in R
    Replace NA with Zero in R R
  • Find the Maximum Value by Group in R
    Find the Maximum Value by Group in R R
  • Subset rows based on their integer locations
    Subset rows based on their integer locations-slice in R R
  • Is Data Science a Dying Profession
    Is Data Science a Dying Profession? R
Two-Way ANOVA Example in R

How to perform One-Sample Wilcoxon Signed Rank Test in R?

Posted on May 9May 12 By Jim No Comments on How to perform One-Sample Wilcoxon Signed Rank Test in R?
Tweet
Share
Share
Pin

One-Sample Wilcoxon Signed Rank Test in R?, When the data cannot be assumed to be normally distributed, the one-sample Wilcoxon signed-rank test is a non-parametric alternative to the one-sample t-test.

It’s used to see if the sample’s median is the same as a known standard value (i.e. theoretical value).

The data should be symmetrically distributed around the median. In other words, the number of numbers above and below the median should be nearly equal.

Common research questions include:

  1. whether the sample’s median (m) equals the theoretical value (m0)?

2. whether the sample’s median (m) lower than the theoretical value (m0)?

3. whether the sample’s median (m) higher than the theoretical value (m0)?

In statistics, the analogous null hypothesis (H0) is defined as follows:

H0:m=m0
H0:m≤m0
H0:m≥m0

The following are the relevant alternative hypothesis (H1):

Ha:m≠m0 (different)
Ha:m>m0 (greater)
Ha:m<m0 (less)

Note that:

Two-tailed tests are used to test hypotheses 1.

One-tailed tests are used to test hypotheses 2 and 3.

Visualize your data and do one-sample calculations. Install the ggpubr R package for data visualization to perform the Wilcoxon test in R.

R base graphs can be created as explained here: R base graphs. For easy ggplot2-based data visualization,

we’ll use the ggpubr R tool. install from CRAN as follow:

install.packages("ggpubr")

Wilcoxon one-sample test with the R function

The R function wilcox.test() can be used to do a one-sample Wilcoxon test as follows.

wilcox.test(x, mu = 0, alternative = "two.sided")

x: a numeric vector containing your data values

mu: the theoretical value of mean/median The default value is zero, but you can modify it.

alternative: the opposite hypothesis “two.sided” (default), “greater” or “less” are all valid values.

We’ll use a data collection including the weights of ten mice as an example.

If the mice’s median weight differs from 22g, we’d like to know.

set.seed(123)
data <- data.frame(
  name = paste0(rep("P_", 10), 1:10),
  weight = round(rnorm(20, 30, 2), 1))

Let’s print the first 10 rows of the data

head(data, 10)
name weight
1   P_1   28.9
2   P_2   29.5
3   P_3   33.1
4   P_4   30.1
5   P_5   30.3
6   P_6   33.4
7   P_7   30.9
8   P_8   27.5
9   P_9   28.6
10 P_10   29.1

 Statistical summaries of weight

summary(data$weight)
  Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  26.10   29.05   30.25   30.28   31.10   33.60

Minimum: the lowest value

The first quartile: Only 25% of the values are lower than this.

The median is the middle value. Half of the readings are lower, while the other half are higher.

The third quartile:  The majority of the values are higher.

Maximum: the highest value

Use box plots to visualize your data.

library(ggpubr)
ggboxplot(data$weight,
          ylab = "Weight (g)", xlab = FALSE,
         ggtheme = theme_minimal())

In R, compute a one-sample Wilcoxon signed-rank test.

If the average weight of the mice differs from 22g (two-tailed test), we want to know.

# One-sample Wilcoxon test

res <- wilcox.test(data$weight, mu = 22)
res

Wilcoxon signed-rank test with continuity correction

data:  data$weight
V = 210, p-value = 9.542e-05
alternative hypothesis: true location is not equal to 22

Now we can print only the p-value

res$p.value
9.542331e-05

The test’s p-value is less than the significance level of alpha = 0.05. We can rule out the null hypothesis and infer that the mice’s average weight differs significantly from 22g.

Keep in mind:

Type this if you wish to see if the median weight of mice is less than 22g (one-tailed test):

wilcox.test(data$weight, mu = 22,
              alternative = "less")

Wilcoxon signed-rank test with continuity correction

data:  data$weight
V = 210, p-value = 1
alternative hypothesis: true location is less than 22

Alternatively, type this to see if the median weight of mice is larger than 22g (one-tailed test).

Statistical test assumptions and requirements

wilcox.test(data$weight, mu = 22,
              alternative = "greater")

Wilcoxon signed-rank test with continuity correction

data:  data$weight
V = 210, p-value = 4.771e-05
alternative hypothesis: true location is greater than 22
Tweet
Share
Share
Pin
R Tags:ttest, wilcoxon

Post navigation

Previous Post: How to perform a one-sample t-test in R?
Next Post: One way ANOVA Example in R-Quick Guide

Related Posts

  • Cumulative Sum calculation in R
    Cumulative Sum calculation in R R
  • R Percentage by Group Calculation
    R Percentage by Group Calculation R
  • How to put margins on tables or arrays in R?
    How to put margins on tables or arrays in R? R
  • best books about data analytics
    Best Books About Data Analytics Course
  • glm function in R
    glm function in r-Generalized Linear Models R
  • Hypothesis Testing in R
    Hypothesis Testing in R R

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Top 7 Skills Required to Become a Data Scientist
  • Learn Hadoop for Data Science
  • How Do Online Criminals Acquire Sensitive Data
  • Top Reasons To Learn R in 2023
  • Linear Interpolation in R-approx

Check your inbox or spam folder to confirm your subscription.

 https://www.r-bloggers.com
  • 5 Free Books to Learn Statistics For Data Science
    5 Free Books to Learn Statistics For Data Science Course
  • How to Add a title to ggplot2 Plots in R
    How to Add a caption to ggplot2 Plots in R? R
  • Augmented Dickey-Fuller Test in R
    Augmented Dickey-Fuller Test in R R
  • Detecting and Dealing with Outliers
    Detecting and Dealing with Outliers: First Step R
  • rejection region in hypothesis testing
    Rejection Region in Hypothesis Testing Statistics
  • How to Create a Frequency Table by Group in R
    How to Create a Frequency Table by Group in R? R
  • How to Rank by Group in R?
    How to Rank by Group in R? R
  • Best Books to Learn R Programming
    Best Books to Learn R Programming Course

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme