Search code examples
rpairwisesurvminer

Compact Letter Display from a matrix of significancies or by hand


I am running a multiple pairwise comparison in R. I'm using the survival package survminer. I'm using the function: pairwise_survdiff {survminer}

It gives the pairwise comparisons with significance as expected, but doesn't seem to have a way to give a compact letter display (CLD) of the results. I'm looking at pairs of 19 levels. I ended up printing the results, putting them into excel by hand and then doing letters by hand. But now I need to do it again and am hoping for an easier way.

  1. Can I have R do a CLD from the pairwise_survdiff {survminer} results directly?

Baring that

  1. Is there a way to get it to print results into a table that can be read by a spreadsheet?

  2. If I make the logic matrix by hand, how do I have R take that and turn it into a CLD?

And 4) If I'm doing it all by hand, I'm wondering if there is a more compact method of showing this list of comparisons. Can I eliminate any of these letters due to redundancy? hand made CLD for comparisons

Thank you


Solution

  • Here's the example from survminer

    library(survminer)
    library(multcomp)
    library(tidyr)
    data(myeloma)
    
    res <- pairwise_survdiff(Surv(time, event) ~ molecular_group,
                             data = myeloma)
    

    Looking at the internals of the glht.summary method from the multcomp package, we create the lvl_order vector which identifies the ordering of the levels of x from smallest to largest.

    x <- myeloma$molecular_group
    levs <- levels(x)
    y <- Surv(myeloma$time, myeloma$event)
    lvl_order <- levels(x)[order(tapply(as.numeric(y)[1:length(x)], 
                                            x, mean))]
    

    Then we can re-arrange the p-values from the res object into a matrix. mycomps is a matrix of the two sides of the paired comparisons. The signif vector is logical indicating whether differences are significant or not.

    comps <- as_tibble(res$p.value, rownames="row") %>% 
      pivot_longer(-row, names_to="col", values_to="p") %>% 
      na.omit()
    mycomps <- as.matrix(comps[,1:2])
    signif <- comps$p < .05
    

    Then, you can use the insert_absorb internal function to create the letters:

    multcomp:::insert_absorb(signif, 
                             decreasing=FALSE, 
                             comps=mycomps, 
                             lvl_order=lvl_order)
    # $Letters
    # MAF    Proliferation       Cyclin D-2            MMSET     Hyperdiploid 
    # "ab"              "a"              "b"             "ab"              "b" 
    # Low bone disease       Cyclin D-1 
    # "ab"             "ab" 
    # 
    # $monospacedLetters
    # MAF    Proliferation       Cyclin D-2            MMSET     Hyperdiploid 
    # "ab"             "a "             " b"             "ab"             " b" 
    # Low bone disease       Cyclin D-1 
    # "ab"             "ab" 
    # 
    # $LetterMatrix
    # a     b
    # MAF               TRUE  TRUE
    # Proliferation     TRUE FALSE
    # Cyclin D-2       FALSE  TRUE
    # MMSET             TRUE  TRUE
    # Hyperdiploid     FALSE  TRUE
    # Low bone disease  TRUE  TRUE
    # Cyclin D-1        TRUE  TRUE
    # 
    # $aLetters
    # [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v"
    # [23] "w" "x" "y" "z" "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R"
    # [45] "S" "T" "U" "V" "W" "X" "Y" "Z"
    # 
    # $aseparator
    # [1] "."
    # 
    # attr(,"class")
    # [1] "multcompLetters"