Test - Nonparametric - Paired Samples Wilcoxon Test
Jump to navigation
Jump to search
Performs Wilcoxon signed rank test comparing the median between a pair of samples.
For samples of different sizes see Test - Nonparametric - Two-Sample Wilcoxon Rank Sum Test.
How to run this test
- In Displayr, go to Insert > More > Test > Nonparametric > Paired Samples Wilcoxon Test. In Q, go to Create > Test > Nonparametric > Paired Samples Wilcoxon Test
- Specify the two input variables under Inputs > Paired Samples Wilcoxon Test > Variable 1 and Variable 2.
- Adjust any other options and settings as required.
Example
An example output is shown below:
Outcome
Variable 1 Sample to analyse.
Variable 2 Second sample to compare to Variable 1.
mu Difference between 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 = "Paired Samples Wilcoxon Test";
if (!!form.setObjectInspectorTitle)
form.setObjectInspectorTitle(heading_text);
else
form.setHeading(heading_text);
form.dropBox({label: "Variable 1",
types:["Variable: Numeric, Date, Money, Categorical, OrderedCategorical"],
name: "formVariable1", prompt: "Select the Variable containing the first sample"});
form.dropBox({label: "Variable 2",
types:["Variable: Numeric, Date, Money, Categorical, OrderedCategorical"],
name: "formVariable2", prompt: "Select the Variable containing the second sample"});
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(flipFormat)
library(flipTransformations)
if (length(formVariable1) != length(formVariable2))
stop("Variables 1 and 2 have different lengths. Please ensure that the variables are from the same data set or have the same length.")
data <- AsNumeric(ProcessQVariables(QDataFrame(formVariable1, formVariable2)), binary = FALSE)
if (!is.null(QCalibratedWeight))
data <- AdjustDataToReflectWeights(data, QCalibratedWeight)
Variable1 <- data[QFilter,1]
Variable2 <- data[QFilter,2]
test.output <- wilcox.test(Variable1, Variable2,
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 = TRUE)
paired.samples.wilcoxon <- if (formOutput == "Summary") {
decimal.places <- if (formDecimals) 8 else NULL
SignificanceTest(test.output, "Paired Samples Wilcoxon Test",
list(formVariable1, formVariable2),
filter = QFilter, weight = QCalibratedWeight,
p.value.method = type,
show.labels = !formNames, decimal.places = decimal.places,
resample = TRUE)
} else
test.output