Search code examples
rstatisticsanovafactors

How to perform a 1-way ANOVA test separated by categories and subcategories using R?


Here is a brief context:

The experiments were conducted (Experiment A and Experiment B). Each experiment was separated into categories (Low and High). Each of the categories was evaluated using 3 samples (Sample 1, Sample 2 and Sample 3). The value of each sample was analyzed 3 times. With this information, the following database was obtained

Experiment <- c(rep("A", 18), rep("B", 18))
Level <- rep((c(rep("Low", 9), rep("High", 9))), 2)
Sample_ID <- rep((c(rep(1,3), rep(2,3), rep(3,3))), 4)
Values <- rnorm(36)

DF <- data.frame(Experiment, Level, Sample_ID, Values)

DF$Experiment <- factor(DF$Experiment,
                        levels = c("A", "B"))
DF$Level <- factor(DF$Level,
                   levels = c("Low", "High"))
DF$Sample_ID <- factor(DF$Sample_ID,
                       levels = c(1, 2, 3))

I started by checking the normality of each sample, categorizing the analysis by Experiment, Level and Sample_ID. I used the RVAideMemoire package that allows you to perform the Shapiro-Wilk test in this way, with the function byf.shapiro()

library(RVAideMemoire)

byf.shapiro(Values ~ Experiment*Level*Sample_ID, data = DF)

Shapiro-Wilk normality tests

data:  Values by Experiment:Level:Sample_ID 

              W p-value  
A:Low:1  0.9143 0.43251  
B:Low:1  0.9880 0.79001  
A:High:1 0.7771 0.06098 .
B:High:1 0.9959 0.87703  
A:Low:2  0.9905 0.81312  
B:Low:2  0.9573 0.60234  
A:High:2 0.8126 0.14497  
B:High:2 0.9980 0.91461  
A:Low:3  0.9698 0.66665  
B:Low:3  0.9893 0.80236  
A:High:3 0.7659 0.03534 *
B:High:3 0.9906 0.81418  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

I tried to perform the 1-way ANOVA test between samples 1, 2, and 3 at each Level and for each Experiment using the following function:

ANOVA <- aov(Values ~ Experiment*Level*Sample_ID, data = DF)
summary(ANOVA)

The result of this function, I think it shows the result of a 3-way ANOVA test instead of a 1-way ANOVA test between the three samples of each Level and also separated by Experiment.

Does anyone know how I can perform a 1-way ANOVA test between levels and experiments? Or does anyone know any packages that make this differentiation?

Just to clarify my question graphically, here is a table where I show the grouping of values to do the 1-way ANOVA test

enter image description here


Solution

  • Using the rstatix package, pipe functions can be created to indicate where to perform the statistical analysis:

    library(rstatix)
    
    Experiment <- c(rep("A", 18), rep("B", 18))
    Level <- rep((c(rep("Low", 9), rep("High", 9))), 2)
    Sample_ID <- rep((c(rep(1,3), rep(2,3), rep(3,3))), 4)
    Values <- rnorm(36)
    
    DF <- data.frame(Experiment, Level, Sample_ID, Values)
    
    DF$Experiment <- factor(DF$Experiment,
                            levels = c("A", "B"))
    DF$Level <- factor(DF$Level,
                       levels = c("Low", "High"))
    DF$Sample_ID <- factor(DF$Sample_ID,
                           levels = c(1, 2, 3))
    
    # Shapiro-Wilk test
    
    my.shapiro <- DF %>%
      group_by(Level, Experiment, Sample_ID) %>%
      shapiro_test(Values) %>%
      add_significance()
    my.shapiro
    
    # ANOVA
    
    ANOVA <- DF %>%
      group_by(Level, Experiment) %>%
      anova_test(Values ~ Sample_ID) %>%
      add_significance()
    ANOVA