Test - Correlation

From Q
Jump to navigation Jump to search


Tests for association between paired samples.

How to run this test

  1. In Displayr, go to Insert > More > Test > Correlation. In Q, go to Create > Test > Correlation
  2. Specify the two variables to use under Inputs > Correlation Test > Variable 1 and Inputs > Correlation Test > Variable 2
  3. Adjust any options (noted below)

Correlation.PNG

You should use numeric variables as inputs. If you use categorical or ordinal variables, they will be coerced to numeric based on their values for the purposes of running the test.

Example

An example output is shown below:

Options

INPUTS

Variable 1 Sample to analyse.
Variable 2 Second sample to compare to Variable 1.
Type of correlation used in the test. The default is the Pearson product-moment, but Kendall's tau or Spearman's rho statistic is a rank-based measure of association which is useful when the data does not necessarily come from a bivariate normal distribution.
Alternative hypothesis Use a two-sided or one-sided test.
Output
Summary Shows a nicely formatted table of the test results (default).
R The original text-based output from the cor.test function.

Variable names Display Variable Names in the output, instead of Variable Labels.

More decimal places Display numeric values with 8 decimal places.

Additional Properties

When using this feature you can obtain additional information that is stored by the R code which produces the output.

  1. To do so, select Create > R Output.
  2. In the R CODE, paste: item = YourReferenceName
  3. Replace YourReferenceName with the name of your item. (eg: 'correlation.test'). You can find this by selecting the item and then going to Properties > General > Name from the object inspector on the right.
  4. Below the first line of code, you can paste in snippets from below or type in str(item) to see a list of available information.

Correlation str.PNG

Acknowledgements

Uses the cor.test function from the stats R package.

Code

var heading_text = "Correlation 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.comboBox({label: "Type of correlation",
              alternatives: ["Pearson product-moment", "Spearman rank-order", "Kendall's Tau-b"],
              name: "formType", default_value: "Pearson product-moment", prompt: "Specify a correlation measure"})
form.comboBox({label: "Alternative hypothesis",
              alternatives: ["Two-sided", "Correlation < 0", "Correlation > 0"],
              name: "formHypothesis", default_value: "Two-sided",
              prompt: "Run a one-sided or two-sided test"})
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.")
dat <- ProcessQVariables(data.frame(formVariable1, formVariable2))
for (i in 1:2)
    if (is.factor(dat[[i]]))
        dat[[i]] <- unclass(dat[[i]])
dat$subset <- QFilter
if (!is.null(QCalibratedWeight))
    dat <- AdjustDataToReflectWeights(dat, QCalibratedWeight)
method <- switch(formType, "Pearson product-moment" = "pearson", "Spearman rank-order" = "spearman", "Kendall's Tau-b" = "kendall")
cor.name <- switch(formType,
                   "Pearson product-moment" = "Pearson's product-moment correlation",
                   "Spearman rank-order" = "Spearman's rank correlation rho",
                   "Kendall's Tau-b" = "Kendall's rank correlation tau")
alternative.hypothesis <- switch(formHypothesis, "Two-sided" = "two.sided",  "Correlation < 0" = "less",  "Correlation > 0" = "greater")
test.output <- cor.test( ~ formVariable1 + formVariable2, data = dat, method = method, alternative = alternative.hypothesis, subset = subset, na.action = "na.omit")
correlation.test <- if (formOutput == "Summary") {
    decimal.places <- if (formDecimals) 8 else NULL
    SignificanceTest(test.output, cor.name, list(formVariable1, formVariable2), filter = QFilter,
                     weight = QCalibratedWeight,
                     p.value.method = alternative.hypothesis,
                     show.labels = !formNames, decimal.places = decimal.places, resample = TRUE)
} else
    test.output

See Also

See What is Correlation? for an introduction to correlation.