9 Workshop 9: Non-Parametric Statistics

Aims:

  • Practice importing and wrangling data ready for analysis.
  • Practice using non-parametric statistics for a one-way repeated design, including any follow-up analyses.

9.1 Exercise 1: Import and Prepare Data

Import the data set called exercise_uni.csv

This is a data set which looked at the average number of hours per week university students engaged in any form of physical exercise, across three time points: before, during, and after university. This is a repeated measures design.

The aim of this study was to determine if levels of physical exercise changed over time.

view(mydata)
names(mydata)

The data are not in long format. Use the code below to change this changing NULL where necessary. The code below assumes you have saved the original data set as an object called mydata.

longd <- mydata %>%
  pivot_longer(cols = c(NULL:NULL),
               names_to = "time",
               values_to = "exercise")
               
view(longd) #take a look at what the above code has done. nice and long!

You should probably code time as a factor, but also reorder the levels. As a standard, where there are multiple levels, RStudio will pop them in alphabetical order. However, our levels have a meaningful order. At the moment alphabetically they are: after, before, druing which is unhelpful.

Change these so that they’re in the correct order: before, during, after. Change NULL in the code to do this.

longd$time <- factor(longd$time, levels = c("NULL", "NULL", "NULL"))

9.2 Exercise 2: Descriptive Statistics

desc <- longd %>%
  group_by(NULL) %>%
  summarise(median = NULL(NULL))

view(desc)

Change NULL in the code above. Remember, as you are using non-parametric statistics you should consider if you want to use the mean or something else…

ggplot(longd, aes(x = time, y = exercise)) +
  geom_violin() +
  geom_boxplot(width = 0.2) +
  theme_classic()

9.3 Exercise 3: Friedman’s ANOVA

Run the ANOVA and ask for effect size. Remember to use the formula DV ~ IV | id. Change NULL

friedman.test(NULL ~ NULL | NULL, data = longd)
friedman_effsize(NULL ~ NULL | NULL, data = longd)

Interpret the output. If the main effect is significant, post-hoc tests are needed. Amend the code below by changing NULL so your data set variables are in the correct place.

longd %>%
  pairwise_wilcox_test(
    NULL ~ NULL,
    paired = TRUE,
    p.adjust.method = "holm")
    
wilcox_effsize(NULL ~ NULL, data = longd, paired = TRUE)
Well Done. You have reached the end of the workshop

9.4 Code for all Four Non-Parametric Tests

The code below is for all four statistics covered in the lecture. These are included below as you might want the code for your year 3 final year project, if you use non-parametric analyses for your project data analysis.

9.5 Mann-Whitney U (or Wilcoxon Rank-Sum)

To use this code you will need to change:

  • DV to the dependent variable.
  • IV to the independent variable.
  • DATA to the name of the object which has your data.
wilcox.test(DV ~ IV, data = DATA) # provides W and p statistics
wilcox_effsize(rt ~ drink, data = mydata) # provides rank-biserial correlation r statistic and magnitude

9.6 Kruskal-Wallis

  • DV to the dependent variable.
  • IV to the independent variable.
  • DATA to the name of the object which has your data.
kruskal.test(DV ~ IV, data = DATA)
kruskal_effsize(DV ~ IV, data = DATA)

#pairwirse comparisons if main effect is signifcant
DATA %>%
  pairwise_wilcox_test(
    DV ~ IV,
    paired = FALSE,
    p.adjust.method = "holm")
    
wilcox_effsize(DV ~ IV, data = DATA) # provides rank-biserial correlation r statistic and magnitude

9.7 Wilcoxon-Signed Rank

  • DV to the dependent variable.
  • IV to the independent variable.
  • DATA to the name of the object which has your data.

Your data need to be in long format for this analysis, as it is a repeated measures design.

pairwise_wilcox_test(DV ~ IV, data = DATA, paired = TRUE)
wilcox_effsize(DV ~ IV, data = DATA, paired = TRUE) # provides rank-biserial correlation r statistic and magnitude

9.8 Friedman’s ANOVA

  • DV to the dependent variable.
  • IV to the independent variable.
  • DATA to the name of the object which has your data.
  • id should be the name of the id variable (identifier for each participant)

Your data need to be in long format for this analysis, as it is a repeated measures design.

friedman.test(DV ~ IV | id, data = DATA)
friedman_effsize(DV ~ IV | id, data = DATA)

#pairwirse comparisons if main effect is signifcant
DATA %>%
  pairwise_wilcox_test(
    DV ~ IV,
    paired = TRUE,
    p.adjust.method = "holm")
wilcox_effsize(DV ~ IV, data = DATA, paired = TRUE)