Test - Nonparametric - Single-Sample Wilcoxon Test

From Q
Jump to navigation Jump to search


Performs the Wilcoxon signed rank test to assess whether the median of the sample is equal to a specified value.

How to run this test

  1. In Displayr, go to Insert > More > Test > Nonparametric > Single-Sample Wilcoxon Test. In Q, go to Create > Test > Nonparametric > Single-Sample Wilcoxon Test
  2. Specify the variable to use under Inputs > Single-Sample Wilcoxon Test > Variable.
  3. Adjust options as per the notes below in the Object Inspector.

SSWT.PNG

Example

An example output is shown below:

Options

Variable Sample to analyse.

mu Median of the variable, 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 = "Single-Sample Wilcoxon Test";
if (!!form.setObjectInspectorTitle)
    form.setObjectInspectorTitle(heading_text);
else
    form.setHeading(heading_text);

form.dropBox({label: "Variable",
              types:["Variable: Numeric, Date, Money, Categorical, OrderedCategorical"],
              name: "formOutcomeVariable", prompt: "Select the Variable containing the sample"});
form.textBox({label: "Null hypothesis: mu =", default_value: "0", name: "formMu", type: "number",
              prompt: "Median of the variable, assumed under the null hypothesis"});
form.comboBox({label: "Alternative hypothesis", alternatives: ["Two-sided", "Median < mu", "Median > 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(flipFormat)
library(flipTransformations)
data <- AsNumeric(ProcessQVariables(QDataFrame(formOutcomeVariable)), binary = FALSE)
if (!is.null(QCalibratedWeight))
    data <- data.frame(AdjustDataToReflectWeights(data, QCalibratedWeight))
Variable <- data[QFilter, 1]

test.output <- wilcox.test(Variable, 
    mu = as.numeric(formMu),
    alternative = switch(hypothesis, "Two-sided" = "two.sided", "Median < mu" = "less", "Median > 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))
single.sample.wilcoxon <- if (formOutput == "Summary") {
    decimal.places <- if (formDecimals) 8 else NULL
    SignificanceTest(test.output, "Single-Sample Wilcoxon Test",
                     list(formOutcomeVariable),
                     filter = QFilter, weight = QCalibratedWeight,
                     p.value.method = type,
                     show.labels = !formNames, decimal.places = decimal.places,
                     resample = TRUE)
} else
    test.output