1

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?

seb-29
  • 51
  • 2
  • May I ask you what do you mean by `weights` exactly? What do you want to achieve by weighted KW test? The KW test doesn't assume normality but assumes `same distributional form`. Are you wishing to consider this assumption which is violated in your data? – S-SHAAF Mar 25 '23 at 23:57
  • Sampling weights, so that with unequal probability sampling the null hypothesis is that the distributions are the same **in the population**, rather than in the sample, because that's what you'd want to know – Thomas Lumley Apr 01 '23 at 06:22

1 Answers1

0

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
Thomas Lumley
  • 1,893
  • 5
  • 8
  • Thank you very much, [@Thomas Lumley](https://stackoverflow.com/users/13523536/thomas-lumley). How can I define how `svyranktest` handles **NAs**? My reason for asking is that pairwise comparisons involving a group with missing values result in "NA". – seb-29 Apr 06 '23 at 16:22
  • You need to do the subsetting yourself. – Thomas Lumley Apr 06 '23 at 18:04