Open In App

JavaScript Program for Kruskal-Wallis Test Calculator

Last Updated : 28 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to implement the Kruskal-Wallis Test Calculator in JavaScript.It is designed to help researchers and analysts compare multiple groups of data and determine if there are significant differences in medians. This calculator can be particularly useful when the data doesn’t meet the assumptions of the parametric tests or when dealing with ranked or ordinal data.

Approach

In this approach, we are taking inputData and then combining all the data into a single dataset. After sorting the data set, we assign a position to all the data, and if two data points have the same value, we assign them an average. Then, for each group, we are summing up the positions of its data, and then we are applying the test statistic formula of mathematics.

H = (12 / (N(N+1))) * (Σ(Ri^2) - (N(N+1)^2)/4)

Then we are calculating the degree of freedom by subtracting one from the number of groups. At this point, we are calculating the p-value by chi-square distribution.

Example: The code presented below illustrates the approach.

Javascript




function test(inputData) {
    // Step 1: Combine inputData and assign position
    let wholeData = [];
    for (
        let i = 0;
        i < inputData.length;
        i++
    ) {
        wholeData = wholeData.concat(
            inputData[i]
        );
    }
    wholeData.sort((a, b) => a - b);
    const position = {};
    let currentPosition = 1;
    for (
        let i = 0;
        i < wholeData.length;
        i++
    ) {
        if (
            i > 0 &&
            wholeData[i] !==
                wholeData[i - 1]
        ) {
            currentPosition++;
        }
        position[wholeData[i]] =
            currentPosition;
    }
  
    // Step 2: Calculate sum of position 
    // for each group
    let sumRanks = [];
    for (
        let i = 0;
        i < inputData.length;
        i++
    ) {
        let sum = 0;
        for (
            let j = 0;
            j < inputData[i].length;
            j++
        ) {
            sum +=
                position[
                    inputData[i][j]
                ];
        }
        sumRanks.push(sum);
    }
  
    // Step 3: Calculate H (test statistic)
    const n = wholeData.length;
    const k = inputData.length;
    const H =
        (12 / (n * (n + 1))) *
        (sumRanks.reduce(
            (acc, curr) =>
                acc + curr ** 2,
            0
        ) -
            (n * (n + 1) ** 2) / 4);
  
    // Step 4: Calculate degrees of freedom
    const df = k - 1;
  
    // Step 5: Calculate p-value using 
    // Chi-square distribution
    const pValue = 1 - Math.exp(-H);
  
    return { H, df, pValue };
}
  
// Example usage
const group1 = [
    12, 14, 16, 19, 20, 21, 22, 23, 25,
    27, 28,
];
const group2 = [
    10, 11, 15, 18, 22, 24, 26, 28, 29,
    30,
];
const group3 = [
    8, 9, 13, 17, 19, 21, 24, 26, 28,
    30, 32,
];
  
const result = test([
    group1,
    group2,
    group3,
]);
console.log(
    "Test Statistic (H):",
    result.H
);
console.log(
    "Degrees of Freedom (df):",
    result.df
);
console.log("p-Value:", result.pValue);


Output

Test Statistic (H): 641.3409090909091
Degrees of Freedom (df): 2
p-Value: 1


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads