Asking for a code of a standard error when performing wilcoxon rank sum test and kruskal-wallis test in R.

My data is ordinal with a large sample size, i have found some code about calculating standard error when wilcoxon and kruskal was performed but it does not work well on my data. Is there any code that you may recommend for this problem?

Those are both non-parametric tests, so there is no standard error returned by either wilcox.test() or kruskal.test(). Are you looking for the effect size?

Yes there is no standard error returned by wilcox.test() and kruskal.test(), that is why I am looking for a possible code or function in R that could help me compute a standard error of a variable. I have found a function that could get a standard error of each group within variable but i have not yet found a code in R that get a standard error of the variable.

To proceed can you provide a reprex for the code you are looking at. I think it may be for effects. See the FAQ: How to do a minimal reproducible example reprex for beginners.

This is the sample dataset of 5 point likert scale by Sex variable
I also used library(psych) to get standard error of each group within sex variable.

library(psych)
# two groups of ordinal data (Sex Variable)

female <- c(2,4,5,3,5,5,5,4,3,4,5,2,2,3,3,2,5,4,4,3,3)
male <- c(1,3,5,4,4,4,4,5,5,3,4,4,2,2,5,5,4,1)
describeBy(female)
describeBy(male)

# Wilcoxon rank sum test
result <- wilcox.test(female, male, exact = F)

# Calculating standard error
n1 <- length(female)
n2 <- length(male)
Ties1 <- sum(table(female) * (table(female) - 1) * (table(female) + 1))
Ties2 <- sum(table(male) * (table(male) - 1) * (table(male) + 1))
SE <- sqrt((n1 * n2 / (n1 + n2) * (n1 + n2 + 1) / 12) * ((n1 + n2 + 1) - ((Ties1^3 - Ties1) / (n1^3 - n1)) - ((Ties2^3 - Ties2) / (n2^3 - n2))))

and i got this error with a warning message:
image

The error comes from taking the square root of a negative number.

Yes, I am wondering whether the code is right or I'm having a problem on my data.

If the data is ordinal, a standard error is not definable in the usual sense because the interval between ranks has no unit of measurement. The top three teams, A, B & C might have racked winning scores of c(3,2,1) or c(10,2,1) or any other, but all we know is the rank. For survey data, we have no way of knowing if the difference between "quite a bit" and "not much" is the same as the difference between "somewhat* and not at all. Non-parametric tests exist to be able to say something about how two groups might differ in terms of a measure of central tendency (usually dispersion around median rank). There are different flavors depending on whether the data is paired (usually repeated measurements on the same subject), how many independent groups, and whether ties exist. For these data, the appropriate test statistic is the Wilcoxon rank sum test with continuity correction. Its application to the data is expressed in terms of a true location shift difference; in this case we reject the null, that it is equal to zero.

library(MASS)
# two groups of ordinal data (Sex Variable)

female <- c(2,4,5,3,5,5,5,4,3,4,5,2,2,3,3,2,5,4,4,3,3)
male <- c(1,3,5,4,4,4,4,5,5,3,4,4,2,2,5,5,4,1)
gender <- as.factor(c(rep("female",length(female)),
            rep("male",length(male))))
d <- data.frame(answer = c(female,male),
                gender = gender)

# contingency table
ct <- table(d)
m <- cbind(as.matrix(ct[,1]),as.matrix(ct[,2]))
# Wilcoxon Rank-Sum Test
# genders do not share the same measure of central tendency
(test1 <- wilcox.test(m[,1], m[,2], exact = FALSE, paired = FALSE))
#> 
#>  Wilcoxon rank sum test with continuity correction
#> 
#> data:  m[, 1] and m[, 2]
#> W = 14.5, p-value = 0.7496
#> alternative hypothesis: true location shift is not equal to 0

Created on 2023-06-23 with reprex v2.0.2

In other words, there is no need for me to have a standard error since I have ordinal data. Thank you so much @technocrat, you really help me a lot. Have a nice day.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.