I would like to perform a Mann-Whitney U Test (also called Wilcoxon rank-sum test) on a weighted sample in R. Such a non-parametric test is required, as neither of the two variables used follow normal distribution. The sample is weighted: a variable assigns a given weight to each row. The weights are numbers with decimals.
The built-in wilcox.test argument in R does not take weights into account. The '[survey]' package does offer a Wilcoxon test for weighted data but I am puzzled by the “degree of freedom” value I get upon performing it. Here is an example, with data formatted like my actual data:
install.packages(‘survey’) library(survey) ordinal = c(4, 1, 1, 2, 3, 6, 5, 7, 6, 1) #outcome variable: ordinal variable with 7 levels groups = c(1, 1, 2, 2, 2, 2, 2, 1, 1, 2) #groups variable: factor with 2 levels w = c(1.3, 1.3, 0.7, 0.5, 1.5, 1.6, 1.6, 0.4, 0.4, 0.7) #weights data = data.frame(ordinal, groups, w) data$groups<-as.factor(data$groups)....sd <- svydesign(ids=~1, probs=data$w, data=data) #survey design, used to apply weights to test svyranktest(ordinal~groups, sd, test="wilcoxon")
Test result is displayed as follows:
Design-based KruskalWallis test data: ordinal ~ groups t = -2.5834, df = 8, p-value = 0.03244 alternative hypothesis: true difference in mean rank score is not equal to 0 sample estimates: difference in mean rank score -0.3626219
Does it make sense that the test’s degree of freedom equals to 8? Or should it rather equal to 1, i.e. the amount of groups minus 1?
In a Kruskal-Wallis test, the degree of freedom is the amount of groups minus 1. I would have expected the degree of freedom in the Mann-Whitney test to be calculated the same way, as both test are very like-minded.
Yet, in this discussion thread, someone says "I suppose you could say the sample sizes are the 'degrees of freedom'", but I am not sure whether this applies to rank-sum tests (independent variables) and/or to signed-rank tests (paired variables).
From what I have also read on the internet, I get a sense that “degrees of freedom” don’t mean much when applied to a rank-sum test, yet I would really like to know whether the test is correct the way I perform it.