Friedman Test Results:
Understanding the Friedman Test
The Friedman Test is a non-parametric test used to detect differences across multiple treatments or conditions in a repeated measures experiment. It's an alternative to the one-way ANOVA with repeated measures when the data does not meet the assumptions of normality.
Key Components of the Friedman Test
- Rank Sums (\( R_j \)): The sum of ranks for each treatment across all subjects.
- Number of Subjects (\( n \)): The number of repeated measures or subjects in the experiment.
- Number of Treatments (\( k \)): The total number of treatments or conditions being compared.
- Degrees of Freedom: The degrees of freedom for the test is calculated as \( k - 1 \).
Formula for the Friedman Test Statistic (\( χ^2 \))
The formula for calculating the Friedman Test statistic, \( χ^2 \), is:
where:
- \( R_j \): Rank sum for treatment \( j \)
- \( n \): Number of subjects
- \( k \): Number of treatments
Calculating Effect Size (W)
The effect size for the Friedman Test can be calculated as:
This provides a measure of the magnitude of differences across the treatments.
Programmatically Calculating the Friedman Test
Below are examples for calculating the Friedman Test statistic in JavaScript, Python, and R.
1. Using JavaScript (with jStat)
In JavaScript, you can use the jStat library to calculate ranks, but you'll need custom code for the test statistic:
// Define data
const groups = [
[14, 15, 13, 16, 15], // Group 1 data
[10, 12, 11, 13, 12], // Group 2 data
[8, 9, 10, 7, 9], // Group 3 data
[12, 15, 14, 13, 10], // Group 4 data
[11, 12, 9, 8, 10] // Group 5 data
];
const n = groups[0].length; // Number of subjects
const k = groups.length; // Number of treatments
// Rank each subject's scores
const rankedGroups = groups[0].map((_, i) => groups.map(group => group[i]))
.map(row => jStat.rank(row));
// Sum ranks for each treatment
const sumRanks = Array(k).fill(0);
rankedGroups.forEach(row => {
row.forEach((rank, j) => sumRanks[j] += rank);
});
// Calculate Friedman test statistic
const sumRanksSquared = sumRanks.reduce((sum, Rj) => sum + Math.pow(Rj, 2), 0);
const chiSquare = (12 / (n * k * (k + 1))) * sumRanksSquared - (3 * n * (k + 1));
const df = k - 1;
const pValue = 1 - jStat.chisquare.cdf(chiSquare, df);
const effectSize = chiSquare / (n * (k - 1));
console.log(`Chi-Square: ${chiSquare.toFixed(5)}`);
console.log(`p-value: ${pValue.toFixed(5)}`);
console.log(`Effect Size (W): ${effectSize.toFixed(5)}`);
2. Using Python
In Python, you can calculate the Friedman Test statistic as follows:
from scipy.stats import friedmanchisquare
# Define data
group1 = [14, 15, 13, 16, 15]
group2 = [10, 12, 11, 13, 12]
group3 = [8, 9, 10, 7, 9]
group4 = [12, 15, 14, 13, 10]
group5 = [11, 12, 9, 8, 10]
# Calculate Friedman Test
chi_square, p_value = friedmanchisquare(group1, group2, group3, group4, group5)
effect_size = chi_square / (len(group1) * (5 - 1))
print(f"Chi-Square: {chi_square:.5f}")
print(f"p-value: {p_value:.5f}")
print(f"Effect Size (W): {effect_size:.5f}")
3. Using R
In R, you can calculate the Friedman Test statistic as follows:
# Define data
group1 <- c(14, 15, 13, 16, 15)
group2 <- c(10, 12, 11, 13, 12)
group3 <- c(8, 9, 10, 7, 9)
group4 <- c(12, 15, 14, 13, 10)
group5 <- c(11, 12, 9, 8, 10)
# Perform Friedman Test
friedman_test <- friedman.test(y = c(group1, group2, group3, group4, group5),
groups = rep(1:5, each = length(group1)),
blocks = rep(1:length(group1), times = 5))
# Effect size calculation
chi_square <- friedman_test$statistic
effect_size <- chi_square / (length(group1) * (5 - 1))
cat("Chi-Square:", chi_square, "\n")
cat("p-value:", friedman_test$p.value, "\n")
cat("Effect Size (W):", round(effect_size, 5), "\n")
Further Reading
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.