Test - Nonparametric - Kruskal-Wallis Rank Sum Test
Performs the Kruskal-Wallis rank sum test to assess whether the population median of at least one group is different from the population median of at least one other group, assuming identical shape and scale. This test can be used to test samples from two or more groups of equal or different sample sizes.
How to run this test
- In Displayr, go to Insert > More > Test > Nonparametric > Kruskal-Wallis Rank Sum Test. In Q, go to Create > Test > Nonparametric > Kruskal-Wallis Rank Sum Test
- Specify the variables to use under Inputs > Kruskal-Wallis > Outcome and Inputs > Kruskal-Wallis > Group. The Group variable should be categorical, but if your data is numeric, then it will be automatically converted.
Example
An example output is shown below:
Options
Outcome The variable to analyse.
Group Variable specifying the group that each observation comes from. Where this variable is numeric, it is automatically grouped into two approximately-equally-sized groups, using the median.
Output
- Summary Shows a nicely formatted table of the test results (default).
- R The original text-based output from the kruskal.test function.
Variable names Display Variable Names in the output, instead of Variable Labels.
More decimal places Display numeric values with 8 decimal places.
Acknowledgements
Uses the kruskal.test function from the stats R package.
Code
var heading_text = "Kruskal-Wallis";
if (!!form.setObjectInspectorTitle)
form.setObjectInspectorTitle(heading_text, heading_text);
else
form.setHeading(heading_text);
form.dropBox({label: "Outcome",
types:["Variable: Numeric, Date, Money, Categorical, OrderedCategorical"],
name: "formOutcomeVariable", prompt: "Select the Variable to analyse"});
form.dropBox({label: "Group",
types:["Variable: Numeric, Date, Money, Categorical, OrderedCategorical"],
name: "formGroupVariable", prompt: "Select the Variable specifying the group that each observation comes from"});
var formOutput = form.comboBox({label: "Output",
alternatives: ["Summary", "R"],
name: "formOutput", default_value: "Summary", prompt: "Select the output type"});
if (formOutput.getValue() == "Summary")
{
form.checkBox({label: "Variable names", name: "formNames", default_value: false,
prompt: "Display names instead of labels"});
form.checkBox({label: "More decimal places", name: "formDecimals", default_value: false,
prompt: "Display numeric values with eight decimal places"});
}
library(flipTransformations)
library(flipFormat)
if (length(formOutcomeVariable) != length(formGroupVariable))
stop("The outcome and groups variables have different lengths. Please ensure that the variables are from the same data set or have the same length.")
data <- ProcessQVariables(QDataFrame(formOutcomeVariable, formGroupVariable))
if (is.factor(data[, 1]))
data[, 1] <- AsNumeric(data[, 1], binary = FALSE)
if (!is.factor(data[, 2]))
data[, 2] <- CreatingBinaryVariableIfNecessary(data, names(data)[2])
data$subset <- QFilter
if (!is.null(QCalibratedWeight))
data <- AdjustDataToReflectWeights(data, QCalibratedWeight)
test.output <- kruskal.test(QFormula(formOutcomeVariable ~ formGroupVariable), data = data, subset = subset)
kruskal.wallis <- if (formOutput == "Summary") {
decimal.places <- if (formDecimals) 8 else NULL
SignificanceTest(test.output, "Kruskal-Wallis Rank Sum Test",
list(formOutcomeVariable, formGroupVariable),
filter = QFilter, weight = QCalibratedWeight,
show.labels = !formNames, decimal.places = decimal.places,
resample = TRUE)
} else
test.output