I have a not normally distributed variable, measured on subjects from many cities, these subjects being categorized into 3 groups.
Given the final dataframe format (see below), how can I compare the weighted medians from group 'B' and 'C' to that from group 'A' chosen as reference?
Note:
- I could use the unpaired wilcox.test for each pairs comparison (?), but in my real data, I have 10 groups to compare to another reference one.
- Is there a function/test equivalent to the wilcox.test which would take weight into account? In other words, how can we compare weighted medians from unpaired groups?
- Is it possible to use such a function/test with dplyr so that all comparisons are summarize using 'summary()'?
Thanks for help
library(gamlss)
library(dplyr)
library(spatstat)
# data
xa <- rGB2(100, mu=5, sigma=3, nu=2, tau=1)
ga <- rep("A", 200)
ca <- sample (c('chicago','shangai','madrid','madrid','denver','madrid','new-york','madrid','roma','pekin','stockholm','rio','montreal'), size = 200, replace = TRUE)
xb <- rBCTo(100, mu=5, sigma=0.1, nu=1, tau=2)
gb <- rep("B", 100)
cb <- sample (c('chicago','paris','chicago','roma','pekin','chicago','tokyo','rio','tokyo','tokyo','london','oslo'), size = 100, replace = TRUE)
xc <- rBCPEo(50, mu=5, sigma=0.1, nu=1, tau=2)
gc <- rep("C", 50)
cc <- sample (c('stockholm','denver','boston','boston','boston','denver','boston'), size = 50, replace = TRUE)
# make the dataframe
dfa <- data.frame(ga, xa, ca) %>% rename(group=ga, variable=xa, city=ca) %>% add_count(city) %>% mutate(weight = n/sum(n))
dfb <- data.frame(gb, xb, cb) %>% rename(group=gb, variable=xb, city=cb) %>% add_count(city) %>% mutate(weight = n/sum(n))
dfc <- data.frame(gc, xc, cc) %>% rename(group=gc, variable=xc, city=cc) %>% add_count(city) %>% mutate(weight = n/sum(n))
df <- rbind(dfa, dfb, dfc)
head(df, 10)
# outpout:
group variable city n weight
1 A 6.454502 montreal 11 0.001625296
2 A 23.100112 denver 12 0.001773050
3 A 6.703525 shangai 12 0.001773050
4 A 3.570637 madrid 69 0.010195035
5 A 10.321184 roma 14 0.002068558
6 A 5.540665 shangai 12 0.001773050
7 A 4.646998 stockholm 22 0.003250591
8 A 4.923428 madrid 69 0.010195035
9 A 6.497164 pekin 16 0.002364066
10 A 3.751503 madrid 69 0.010195035
...
350 C 4.7523650 boston 30 0.025597270