[R] Pairwise testing with pairwise.prop.test

peter dalgaard pd@|gd @end|ng |rom gm@||@com
Tue Apr 2 13:10:26 CEST 2019


>> My questions:1) Why is there a "-" instead of a numerical result for pairs 1-2,
>> 1-16, and 2-16?
> 
> When the difference between the pair is zero, the p.value is NaN (not a number).
> 

Not quite: When both groups have 0 successes (or both 0 failures), the test stat has a divide-by-zero condition.

>> 2) Is there an easy way to export/convert the result to a list with two columns
>> instead of the matrix? Column 1 would be the pair being compared, and column 2
>> would be the p-value. For example, Column 1 would say "6-8" so column 2 would
>> say "0.9532".
> 
> Fairly easy:
> 
> idx <- expand.grid(2:16, 1:15)
> pair <- as.matrix(idx[idx[,1] > idx[,2], ])
> mode(pair) <- "character"
> comp <- apply(pair, 1, paste0, collapse="-")
> tbl <- data.frame(comp, p.value=EggResults$p.value[pair])
> head(tbl)
> #   comp      p.value
> # 1  2-1          NaN
> # 2  3-1 2.706354e-24
> # 3  4-1 1.487240e-23
> # 4  5-1 1.946384e-31
> # 5  6-1 4.888537e-25
> # 6  7-1 7.683167e-41
> 

Somewhat neater:

> out <- pairwise.prop.test(smokers, patients)
[....]
> pmat <- out$p.value
> ix <- lower.tri(pmat, diag=TRUE)
> R <- rownames(pmat)[row(pmat)[ix]]
> C <- colnames(pmat)[col(pmat)[ix]]
> data.frame(row.vs.col = paste(R,C,sep="-"), adj.p = pmat[ix])
  row.vs.col      adj.p
1        2-1 1.00000000
2        3-1 1.00000000
3        4-1 0.11856482
4        3-2 1.00000000
5        4-2 0.09321728
6        4-3 0.12376805

Also, labeling seems to work if you label the original data appropriately, e.g.

> names(smokers) <- names(patients) <- as.roman(1:4)
> out <- pairwise.prop.test(smokers, patients)
[....]
> ix <- lower.tri(pmat, diag=TRUE)
> pmat <- out$p.value
> R <- rownames(pmat)[row(pmat)[ix]]
> C <- colnames(pmat)[col(pmat)[ix]]
> data.frame(row.vs.col = paste(R,C,sep="-"), adj.p = pmat[ix])
  row.vs.col      adj.p
1       II-I 1.00000000
2      III-I 1.00000000
3       IV-I 0.11856482
4     III-II 1.00000000
5      IV-II 0.09321728
6     IV-III 0.12376805

-pd

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes using cbs.dk  Priv: PDalgd using gmail.com



More information about the R-help mailing list