Test - Nonparametric - Two-Sample Wilcoxon Rank Sum Test

From Q
Jump to navigation Jump to search


Performs the Wilcoxon rank sum test to compare the median between two samples.

This test can be applied to two samples of equal or different sizes. For paired samples see Test - Nonparametric - Paired Samples Wilcoxon Test.

How to run this test

  1. In Displayr, go to Insert > More > Test > Nonparametric > Two-Sample Wilcoxon Test. In Q, go to Create > Test > Nonparametric > Two-Sample Wilcoxon Test
  2. Specify the two variables to use under Inputs > Two-Sample Wilcoxon Test > Outcome and Inputs > Two-Sample Wilcoxon Test > Group. Note that the latter should be in categorical format, and that numeric variables will be automatically converted by the function.
  3. Adjust any other settings as required.

TSWRS.PNG

Example

An example output is shown below:

Options

Outcome Variable containing the sample to analyze.

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.

mu Difference in the median of the two samples, as assumed under the null hypothesis.

Alternative hypothesis Use a two-sided or one-sided test.

Type of test Method by which the p-value is computed.

Output

Summary Shows a nicely formatted table of the test results (default).
R The original text-based output from the wilcox.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 wilcox.test function from the stats R package.

Code

var heading_text = "Two-sample Wilcoxon Test";
if (!!form.setObjectInspectorTitle)
    form.setObjectInspectorTitle(heading_text);
else
    form.setHeading(heading_text);
form.dropBox({label: "Outcome",
            types:["Variable: Numeric, Date, Money, Categorical, OrderedCategorical"],
            name: "OutcomeVariable", prompt: "Select the Variable to analyse"});
form.dropBox({label: "Group",
            types:["Variable: Numeric, Date, Money, Categorical, OrderedCategorical"],
            name: "GroupVariable", prompt: "Select the Variable specifying the group that each observation comes from"});
form.textBox({label: "mu", default_value: "0", name: "formMu", type: "number",
              prompt: "Difference between the median of the two samples, assumed under the null hypothesis"});
form.comboBox({label: "Alternative hypothesis: ", alternatives: ["Two-sided", "Difference < mu", "Difference > mu"],
               default_value: "Two-sided", name: "hypothesis", prompt: "Run a one-sided or two-sided test"});
form.comboBox({label: "Type of test", alternatives: ["Default", "Exact", "Normal approximation", "Normal approximation with continuity correction"],
               name: "type", default_value: "Default", prompt: "Method by which the p-value is computed"});
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(OutcomeVariable) != length(GroupVariable))
    stop("The outcome and group variables have different lengths. Please ensure that the variables are from the same data set or have the same length.")

data <- ProcessQVariables(QDataFrame(OutcomeVariable, GroupVariable))
if (is.factor(OutcomeVariable))
    data[, 1] <- AsNumeric(data[, 1], binary=FALSE)
data[, 2] <- CreatingBinaryVariableIfNecessary(data, names(data)[2])
if (!is.null(QCalibratedWeight))
    data <- AdjustDataToReflectWeights(data, QCalibratedWeight)
test.output <- wilcox.test(QFormula(OutcomeVariable ~ GroupVariable), data=data, conf.level=.95, subset = QFilter,
    mu = as.numeric(formMu), 
    alternative = switch(hypothesis, "Two-sided" = "two.sided", "Difference < mu" = "less", "Difference > mu" = "greater"), 
    exact = switch(type, "Default" = NULL, "Exact" = TRUE, "Normal approximation" = FALSE, "Normal approximation with continuity correction" = FALSE), 
    correct = switch(type, "Default" = TRUE, "Exact" = FALSE, "Normal approximation" = FALSE, "Normal approximation with continuity correction" = TRUE), 
    paired = FALSE)
two.sample.wilcoxon <- if (formOutput == "Summary") {
    decimal.places <- if (formDecimals) 8 else NULL
    SignificanceTest(test.output, "Two-Sample Wilcoxon Rank Sum Test",
                     list(OutcomeVariable, GroupVariable),
                     filter = QFilter, weight = QCalibratedWeight,
                     p.value.method = type,
                     show.labels = !formNames, decimal.places = decimal.places,
                     resample = TRUE)
} else
    test.output