

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 12,
116, and 216?
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 pvalue. For example, Column 1 would say "68" 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/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Inline

David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 778434352
Original Message
> From: Rhelp < [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 12,
> 116, and 216?
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 pvalue. For example, Column 1 would say "68" 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 21 NaN
# 2 31 2.706354e24
# 3 41 1.487240e23
# 4 51 1.946384e31
# 5 61 4.888537e25
# 6 71 7.683167e41
> 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/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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 778434352
>
> Original Message
> > From: Rhelp < [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 12,
> > 116, and 216?
>
> 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 pvalue. For example, Column 1 would say "68" 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 21 NaN
> # 2 31 2.706354e24
> # 3 41 1.487240e23
> # 4 51 1.946384e31
> # 5 61 4.888537e25
> # 6 71 7.683167e41
>
> > 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/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


> > 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 pvalue matrix), you could relabel the htest pvalue 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:(n1)
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/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


>> My questions:1) Why is there a "" instead of a numerical result for pairs 12,
>> 116, and 216?
>
> 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 dividebyzero 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 pvalue. For example, Column 1 would say "68" 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 21 NaN
> # 2 31 2.706354e24
> # 3 41 1.487240e23
> # 4 51 1.946384e31
> # 5 61 4.888537e25
> # 6 71 7.683167e41
>
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 21 1.00000000
2 31 1.00000000
3 41 0.11856482
4 32 1.00000000
5 42 0.09321728
6 43 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 III 1.00000000
2 IIII 1.00000000
3 IVI 0.11856482
4 IIIII 1.00000000
5 IVII 0.09321728
6 IVIII 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/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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
12,
>> 116, and 216?
>
> 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 dividebyzero 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 pvalue. For example, Column 1 would say "68" 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 21 NaN
> # 2 31 2.706354e24
> # 3 41 1.487240e23
> # 4 51 1.946384e31
> # 5 61 4.888537e25
> # 6 71 7.683167e41
>
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 21 1.00000000
2 31 1.00000000
3 41 0.11856482
4 32 1.00000000
5 42 0.09321728
6 43 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 III 1.00000000
2 IIII 1.00000000
3 IVI 0.11856482
4 IIIII 1.00000000
5 IVII 0.09321728
6 IVIII 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/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.

