Pairwise testing with pairwise.prop.test

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Pairwise testing with pairwise.prop.test

JLfish
Hi all,

I have a few questions about the pairwise.prop.test function.
I run the test using on the attached dataset (EggMortNoTemp) using the following
code:
EggMatrix<-as.matrix(EggMortNoTemp) #making the egg mortality data a matrix.
pairwise.prop.test needs it to be a matrix
EggResults<-pairwise.prop.test(EggMatrix) EggResults
I get the attached result (EggResults).
My questions:1) Why is there a "-" instead of a numerical result for pairs 1-2,
1-16, and 2-16?
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".
3) Is there a way to assign the numbers to the treatment group that they were
originally in? For example, "1" in the result should be "3", indicating that
that was the temperature the experiment took place at. Pairwise.prop.test
doesn't seem to accept matrices with more than 2 columns.
Any help would be greatly appreciated! Thank you in advance.
Cheers,Jenny
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

EggMortNoTemp.txt (122 bytes) Download Attachment
EggResults.PNG (25K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Pairwise testing with pairwise.prop.test

David Carlson
Inline

----------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77843-4352

-----Original Message-----

> From: R-help <[hidden email]> On Behalf Of Jenny Liu
> Sent: Sunday, March 31, 2019 6:10 PM
> To: [hidden email]
> Subject: [R] Pairwise testing with pairwise.prop.test
>
> Hi all,
>
> I have a few questions about the pairwise.prop.test function.
> I run the test using on the attached dataset (EggMortNoTemp) using the following
> code:
> EggMatrix<-as.matrix(EggMortNoTemp) #making the egg mortality data a matrix.
> pairwise.prop.test needs it to be a matrix
> EggResults<-pairwise.prop.test(EggMatrix) EggResults
> I get the attached result (EggResults).
> 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).

> 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

> 3) Is there a way to assign the numbers to the treatment group that they were
> originally in? For example, "1" in the result should be "3", indicating that
> that was the temperature the experiment took place at. Pairwise.prop.test
> doesn't seem to accept matrices with more than 2 columns.

No. The test is to do the pairwise comparisons between the samples. The treatment group is not considered. You would have to use another test that compared the samples by treatment group (instead of pairwise comparisons).

> Any help would be greatly appreciated! Thank you in advance.
> Cheers,Jenny

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Pairwise testing with pairwise.prop.test

JLfish
Thank you very much David, your answers are so clear and helpful!!

Have a great week.

Cheers,
Jenny

On Mon, Apr 1, 2019, 11:53 AM David L Carlson, <[hidden email]> wrote:

> Inline
>
> ----------------------------------------
> David L Carlson
> Department of Anthropology
> Texas A&M University
> College Station, TX 77843-4352
>
> -----Original Message-----
> > From: R-help <[hidden email]> On Behalf Of Jenny Liu
> > Sent: Sunday, March 31, 2019 6:10 PM
> > To: [hidden email]
> > Subject: [R] Pairwise testing with pairwise.prop.test
> >
> > Hi all,
> >
> > I have a few questions about the pairwise.prop.test function.
> > I run the test using on the attached dataset (EggMortNoTemp) using the
> following
> > code:
> > EggMatrix<-as.matrix(EggMortNoTemp) #making the egg mortality data a
> matrix.
> > pairwise.prop.test needs it to be a matrix
> > EggResults<-pairwise.prop.test(EggMatrix) EggResults
> > I get the attached result (EggResults).
> > 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).
>
> > 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
>
> > 3) Is there a way to assign the numbers to the treatment group that they
> were
> > originally in? For example, "1" in the result should be "3", indicating
> that
> > that was the temperature the experiment took place at. Pairwise.prop.test
> > doesn't seem to accept matrices with more than 2 columns.
>
> No. The test is to do the pairwise comparisons between the samples. The
> treatment group is not considered. You would have to use another test that
> compared the samples by treatment group (instead of pairwise comparisons).
>
> > Any help would be greatly appreciated! Thank you in advance.
> > Cheers,Jenny
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Pairwise testing with pairwise.prop.test

S Ellison-2
In reply to this post by David Carlson
> > 3) Is there a way to assign the numbers to the treatment group that they
> were
> > originally in? For example, "1" in the result should be "3", indicating that
> > that was the temperature the experiment took place at. Pairwise.prop.test
> > doesn't seem to accept matrices with more than 2 columns.
>
> No. The test is to do the pairwise comparisons between the samples.

A possible wrinkle, there: If you know the treatment labels for your group numbers (1...n in a simple htest  p-value matrix), you could relabel the htest p-value matrix via its dimnames property. For example, in the smokers example under ?pairwise.prop.test:

smk.pwtst <- pairwise.prop.test(smokers, patients)
groupnames <- paste("group", 1:4) #This would have to be manual for your example

new.dims <- list(groupnames[1:3],       #we know this is from 1:(n-1)
                                 groupnames[2:4])       #and this is 2:n

dimnames(smk.pwtst$p.value) <- new.dims

smk.pwtst
#         group 1 group 2 group 3
# group 2 1.000   -       -      
# group 3 1.000   1.000   -      
# group 4 0.119   0.093   0.124  

Steve Ellison



*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Pairwise testing with pairwise.prop.test

Peter Dalgaard-2
In reply to this post by David Carlson
>> 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: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Pairwise testing with pairwise.prop.test

JLfish
Thank you Stephen and Peter for your code and your answers!
Stephen, your NaN explanation makes sense - thank you for putting it so clearly.
Cheers,
Jenny






On Tue, Apr 2, 2019 11:10 AM, peter dalgaard [hidden email]  wrote:
>> 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: [hidden email] Priv: [hidden email]
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.