seb-29
seb-29

Reputation: 41

Pairwise comparisons with weights in R

I ran a weighted Kruskal Wallis test using the survey package in R.

The result shows that there is a significant difference between groups, but does not specify between which ones. Therefore, I´d like to follow up with weighted pairwise comparisons (post-hoc test).

In the stats package, there is the 'pairwise.wilcox.test' function, which does exactly that. However, as far as I know, it does not allow to use weights. Therefore, I´ve tried looping a weighted wilcoxon test through the levels of the grouping variable - without success though. And even if this had worked, I would still need to manually adjust all the p-values to correct for multiple comparisons...

Hence my question: How can I run pairwise comparisons with weights in R?

Upvotes: 1

Views: 603

Answers (1)

Thomas Lumley
Thomas Lumley

Reputation: 2765

You don't say what you tried for looping over levels, or provide an example, but using the example from the help page:

svyranktest(ell~stype, dclus1)

l<-c("E","M","H")

for(i in 1:2){
    for(j in (i+1):3){
      print(l[c(i,j)])
      print(svyranktest(ell~stype, subset(dclus1, stype %in% l[c(i,j)])))
        
    }
}

Or if you want the p-values in an object

> l<-c("E","M","H")
> ps<-matrix(NA,3,3)
> dimnames(ps)<-list(l,l)
> for(i in 1:2){
+   for(j in (i+1):3){
+     ps[i,j]<-svyranktest(ell~stype, subset(dclus1, stype %in% l[c(i,j)]))$p.value
+       
+   }
+ }
> ps
   E           M           H
E NA 0.001687062 0.003378391
M NA          NA 0.053809416
H NA          NA          NA

Once you have the p-values, you can just call p.adjust to get whatever p-value adjustment you would have used with pairwise.wilcox.test

> p.adjust(ps[!is.na(ps)],method="hochberg")
[1] 0.005061186 0.006756783 0.053809416

Upvotes: 0

Related Questions