Search code examples
rstatisticspairwise.wilcox.test

R - Wilcoxon signed-rank test


I reviewed Wilcoxon signed-ranks test with R vs by hand,Multiple Wilcoxon Signed Rank test in R, and Wilcoxon signed rank test for heavily tied data.

I have two datasets that I is creating pairwise associations between species based on a specific weight that's calculated through an affinity matrix and simple ratio index. I want to test if the weights are different between the test groups. This is what I've tried but I don't get an actual output to read. Any advice is greatly appreciated!

Dataset 1

dat1 <- structure(list(5, FALSE, c(1, 3, 4, 2, 3, 4, 3, 4, 4), c(0, 0, 
0, 1, 1, 1, 2, 2, 3), c(0, 3, 1, 4, 6, 2, 5, 7, 8), c(0, 1, 2, 
3, 4, 5, 6, 7, 8), c(0, 0, 1, 2, 5, 9), c(0, 3, 6, 8, 9, 9), 
    list(c(1, 0, 1), structure(list(), names = character(0)), 
        list(name = c("BABO", "BW", "MANG", "RC", "SKS"), n_obs = c(219L, 
        1377L, 197L, 1881L, 1232L), n_grps = c(34L, 535L, 61L, 
        665L, 339L)), list(weight = c(0.227890554864407, 0.291851490123247, 
        0.222986891666136, 0.273019105913787, 0.270047304490458, 
        0.308713136488867, 0.31127336565632, 0.313224653422152, 
        0.270579464114338))), <environment>), class = c("tbl_graph", 
"igraph"), active = "nodes")

Dataset 2

dat2 <- structure(list(5, FALSE, c(4, 3, 3, 4, 4), c(0, 1, 2, 2, 3), 
    c(1, 2, 0, 3, 4), c(0, 1, 2, 3, 4), c(0, 0, 0, 0, 2, 5), 
    c(0, 1, 2, 4, 5, 5), list(c(1, 0, 1), structure(list(), names = character(0)), 
        list(name = c("BABO", "BW", "MANG", "RC", "SKS"), n_obs = c(23L, 
        8L, 117L, 29L, 668L), n_grps = c(2L, 6L, 21L, 10L, 25L
        )), list(weight = c(0.282369461120595, 0.321658527239868, 
        0.307638016777122, 0.322400613641658, 0.342550960146532
        ))), <environment>), class = c("tbl_graph", "igraph"), active = "nodes")
pairwise.wilcox.test(dat1$weight, dat2$weight) -> wilcox.test

My code runs but there is no output.


Solution

  • The data in your example is in the igraph / tbl_graph format. This data structure is complicated, but you can pull out the weights to compare them using e.g.

    library(tidyverse)
    library(tidygraph)
    #> 
    #> Attaching package: 'tidygraph'
    #> The following object is masked from 'package:stats':
    #> 
    #>     filter
    
    dat1 <- structure(list(5, FALSE, c(1, 3, 4, 2, 3, 4, 3, 4, 4), c(0, 0, 
                                                                     0, 1, 1, 1, 2, 2, 3), c(0, 3, 1, 4, 6, 2, 5, 7, 8), c(0, 1, 2, 
                                                                                                                           3, 4, 5, 6, 7, 8), c(0, 0, 1, 2, 5, 9), c(0, 3, 6, 8, 9, 9), 
                           list(c(1, 0, 1), structure(list(), names = character(0)), 
                                list(name = c("BABO", "BW", "MANG", "RC", "SKS"), n_obs = c(219L, 
                                                                                            1377L, 197L, 1881L, 1232L), n_grps = c(34L, 535L, 61L, 
                                                                                                                                   665L, 339L)), list(weight = c(0.227890554864407, 0.291851490123247, 
                                                                                                                                                                 0.222986891666136, 0.273019105913787, 0.270047304490458, 
                                                                                                                                                                 0.308713136488867, 0.31127336565632, 0.313224653422152, 
                                                                                                                                                                 0.270579464114338))), "<environment>"), class = c("tbl_graph", 
                                                                                                                                                                                                                 "igraph"), active = "nodes")
    
    dat2 <- structure(list(5, FALSE, c(4, 3, 3, 4, 4), c(0, 1, 2, 2, 3), 
                           c(1, 2, 0, 3, 4), c(0, 1, 2, 3, 4), c(0, 0, 0, 0, 2, 5), 
                           c(0, 1, 2, 4, 5, 5), list(c(1, 0, 1), structure(list(), names = character(0)), 
                                                     list(name = c("BABO", "BW", "MANG", "RC", "SKS"), n_obs = c(23L, 
                                                                                                                 8L, 117L, 29L, 668L), n_grps = c(2L, 6L, 21L, 10L, 25L
                                                                                                                 )), list(weight = c(0.282369461120595, 0.321658527239868, 
                                                                                                                                     0.307638016777122, 0.322400613641658, 0.342550960146532
                                                                                                                 ))), "<environment>"), class = c("tbl_graph", "igraph"), active = "nodes")
    
    str(dat1)
    #> Classes 'tbl_graph', 'igraph'  hidden list of 10
    #>  $ : num 5
    #>  $ : logi FALSE
    #>  $ : num [1:9] 1 3 4 2 3 4 3 4 4
    #>  $ : num [1:9] 0 0 0 1 1 1 2 2 3
    #>  $ : num [1:9] 0 3 1 4 6 2 5 7 8
    #>  $ : num [1:9] 0 1 2 3 4 5 6 7 8
    #>  $ : num [1:6] 0 0 1 2 5 9
    #>  $ : num [1:6] 0 3 6 8 9 9
    #>  $ :List of 4
    #>   ..$ : num [1:3] 1 0 1
    #>   ..$ : Named list()
    #>   ..$ :List of 3
    #>   .. ..$ name  : chr [1:5] "BABO" "BW" "MANG" "RC" ...
    #>   .. ..$ n_obs : int [1:5] 219 1377 197 1881 1232
    #>   .. ..$ n_grps: int [1:5] 34 535 61 665 339
    #>   ..$ :List of 1
    #>   .. ..$ weight: num [1:9] 0.228 0.292 0.223 0.273 0.27 ...
    #>  $ : chr "<environment>"
    #>  - attr(*, "active")= chr "nodes"
    
    dat1_df <- as_tibble(dat1[2])
    dat2_df <- as_tibble(dat2[2])
    
    pairwise.wilcox.test(dat1_df$value, dat2_df$value) -> wilcox.results
    wilcox.results
    #> 
    #>  Pairwise comparisons using Wilcoxon rank sum exact test 
    #> 
    #> data:  dat1_df$value and dat2_df$value 
    #> 
    #>                   0
    #> 0.321658527239868 1
    #> 
    #> P value adjustment method: holm
    

    Created on 2023-05-12 with reprex v2.0.2