ashokkrish / CougarStats

5 stars 2 forks source link

Nonparametric tests: Wilcoxon Rank-Sum Test #31

Closed ashokkrish closed 1 week ago

ashokkrish commented 1 month ago

@bryce-carson

Under Statistical Inference when the user selects:

image

and further selects

image

then we should have a radio button toggle like this

Wilcoxon Rank Sum Test (also called the Mann–Whitney U test) Workflow

Step 1

Step 2

Step 3

Step 4

Step 5

# Sample data
Before <- c(83.3, 86, 82.5, 86.7, 79.6, 87.3, 76.9, 94.2, 73.4, 80.5, 81.6, 83.8, 82.1, 77.6, 83.5, 89.9, 86)
After <- c(94.3, 91.5, 91.9, 100.3, 76.7, 98, 76.8, 101.6, 94.9, 75.2, 77.8, 95.2, 95.5, 90.7, 92.5, 93.8, 91.7)

# Wilcoxon Rank Sum Test
result <- wilcox.test(Before, After, paired = TRUE, exact = TRUE, alternative = "less")
V <- result$statistic
cat("V:", V, "\n")

# Test statistic W
r <- abs(Before - After)
ranks <- rank(r)
signs <- sign(Before - After)
W <- sum(ranks[signs == 1])
cat("W:", W, "\n")

# Expected value and standard deviation
n <- length(Before)
mu <- n * (n + 1) / 4
sd <- sqrt(n * (n + 1) * (2 * n + 1) / 24)
cat("Expected value:", mu, "\n")
cat("Standard deviation:", sd, "\n")

# Standardized value using normal approximation
z <- (W - mu) / sd
cat("Standardized value (using normal approximation):", z, "\n")

# P-value
p_value <- 2 * pnorm(abs(z), lower.tail = FALSE)
cat("P-value:", p_value, "\n")