1

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.

  • Can you check your example data - it generates an error when I paste it into R. Also `dat1` looks to have more weight values than `dat2`. – neilfws May 11 '23 at 23:59
  • 1
    For R to read it in properly, `` should be quoted (i.e. `""`) – jared_mamrot May 12 '23 at 00:02
  • 1
    Note also that there is no element named `weight` in the example data, so `dat1$weight, dat2$weight` = `NULL, NULL`, which explains the lack of output. – neilfws May 12 '23 at 00:11
  • Not really paired data. The pairing is like a propensity analysis. Invalid method. – IRTFM May 12 '23 at 03:59

1 Answers1

1

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

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
  • This is great except I'm confused why there is only one number output. Any advice is greatly appreciated. – Marnee Roundtree May 12 '23 at 00:30
  • 2
    The number corresponds to the difference in distributions between `dat1$weight = c(0.227890554864407, 0.291851490123247, 0.222986891666136, 0.273019105913787, 0.270047304490458, 0.308713136488867, 0.31127336565632, 0.313224653422152, 0.270579464114338)` and `dat2$weight = c(0.282369461120595, 0.321658527239868, 0.307638016777122, 0.322400613641658, 0.342550960146532)`; is this not the expected outcome? – jared_mamrot May 12 '23 at 00:39
  • Hmm, okay. I may need to read up more on the `pairwise.wilcox.test`, as I expected it to be comparing each value in `dat1_df` to the corresponding association value in `dat2_df` and giving me a z or p-value for significance. – Marnee Roundtree May 12 '23 at 00:58
  • Okay, am I understanding this correctly in that it is comparing the entirety of my `dat1$weight` distribution to the entirety of my `dat2$weight` distribution rather than testing them individually? So the difference is 0.32 to 1 with no p-value given. – Marnee Roundtree May 12 '23 at 14:43
  • 1
    Yes, that's correct @MarneeRoundtree; if you need advice on how to go about analysing your data (i.e. what statistical test is most appropriate, or 'how to set up a pairwise wilcox test) I highly recommend https://stats.stackexchange.com/ – jared_mamrot May 14 '23 at 23:30