Search code examples
rerror-handlingfriedman-test

Friedman test gives back error: not an unreplicated complete block design. How do I fix this?


Whatever I try, I keep getting the error 'not an unreplicated complete block design' when I try to perform a Friedman test in R. I have a dataset with the total number of flower visits that I counted on different shrub species, which were then divided into different categories (locations). Since the data did not meet the two-way ANOVA assumptions I turned to the Friedman test. My data frame is as follows:

> diversity_columns
# A tibble: 74 × 3
   Category_ID Species         Total_visits
   <fct>       <fct>                  <dbl>
 1 UN          Common hawthorn           22
 2 UN          Common hawthorn           42
 3 UN          Common hawthorn            3
 4 UN          Common hawthorn           13
 5 UN          Common hawthorn           76
 6 UN          Common hawthorn           95
 7 UN          Common hawthorn           53
 8 RN          Common hawthorn           50
 9 RN          Common hawthorn           18
10 UN          Common hawthorn            6

The labels for the Category_IN are "RA", "RN" and "UN" and for Species "Common hawthorn", "Blackberry" and "Rose"

I tried the following command:

friedman.test(Total_visits ~ Category_ID | Species, data = diversity_columns)

However, I got this error in return:

Error in friedman.test.default(mf[[1L]], mf[[2L]], mf[[3L]]) : 
  not an unreplicated complete block design

I already tried to transform my dataset with the function as.matrix:

as.matrix(diversity_columns)

And I also tried transformed my variables into factors:

Shannon_diversity$Species <- factor(Shannon_diversity$Species)
Shannon_diversity$Category_ID <- factor(Shannon_diversity$Category_ID)

Solution

  • This indicates that your missing at least one of the six cross-categories of Species/Category.ID.

    You can see which one(s) are missing by using table:

    with(diversity_columns, table(Category_ID, Species))
    

    I can replicate this error using the built-in warpbreaks dataset.

    wb <- aggregate(warpbreaks$breaks,
                    by = list(w = warpbreaks$wool,
                              t = warpbreaks$tension),
                    FUN = mean)
    wb
    

      w t        x
    1 A L 44.55556
    2 B L 28.22222
    3 A M 24.00000
    4 B M 28.77778
    5 A H 24.55556
    6 B H 18.77778
    
    friedman.test(x ~ w | t, data = wb)
    

        Friedman rank sum test
    
    data:  x and w and t
    Friedman chi-squared = 0.33333, df = 1, p-value = 0.5637
    

    This works. Now remove one of the rows from wb:

    wb2 <- wb[-2,]
    with(wb2, table(w, t))
    

       t
    w   L M H
      A 1 1 1
      B 0 1 1
    
    friedman.test(x ~ w | t, data = wb2)
    #Error in friedman.test.default(mf[[1L]], mf[[2L]], mf[[3L]]) : 
      #not an unreplicated complete block design