test.correlation: Correlation Analysis with Automatic Method Selection

View source: R/Correlation_test.R

test.correlationR Documentation

Correlation Analysis with Automatic Method Selection

Description

Performs correlation analysis between two numeric variables using Pearson, Spearman, or Kendall methods. When method = "auto", the function automatically selects the most appropriate method based on normality, presence of ties, and proportion of outliers.

Usage

test.correlation(
  x,
  y = NULL,
  method = "auto",
  main = NULL,
  xlab = NULL,
  ylab = NULL,
  style = c("simple", "inference", "structure", "density", "distribution"),
  plot_normality = FALSE,
  help = FALSE,
  verbose = TRUE
)

## S3 method for class 'test.correlation'
print(x, ...)

Arguments

x

Numeric vector or data frame with exactly two numeric columns.

y

Numeric vector (optional if x is a data frame).

method

Correlation method: "auto" (default), "pearson", "spearman", or "kendall".

main

Plot title. If NULL, an automatic title is generated. If FALSE, no title is displayed.

xlab

Label for the x-axis. If NULL, a label is inferred from the input. If FALSE, no label is shown.

ylab

Label for the y-axis. If NULL, a label is inferred from the input. If FALSE, no label is shown.

style

Plot style: "simple" (default), "inference", "structure", "density", or "distribution".

plot_normality

Logical. If TRUE, displays QQ-plots for normality assessment.

help

Logical. If TRUE, displays a detailed usage guide.

verbose

Logical. If TRUE, prints information about method selection and diagnostics.

...

Additional arguments passed to other print methods (currently ignored)

Details

In addition to hypothesis testing, the function provides diagnostic information, effect size interpretation, and publication-ready visualizations.

Method selection in automatic mode follows these rules:

  • If more than 5\ Kendall's tau is used.

  • If both variables are normally distributed and no ties are present, Pearson's correlation is used.

  • Otherwise, Spearman's rank correlation is applied.

Normality is assessed using Shapiro-Wilk (n <= 50), Anderson-Darling (50 < n <= 300), or Kolmogorov-Smirnov (n > 300) tests.

Value

An object of class "test.correlation" containing:

  • call: Matched function call.

  • data: Input data (if n <= 5000).

  • n: Sample size.

  • method: Selected correlation method.

  • estimate: Correlation coefficient.

  • effect: Effect size information (r, rho, or tau, and r² when applicable).

  • conf.int: Confidence interval.

  • p.value: P-value of the test.

  • interpretation: Qualitative interpretation of effect size.

  • diagnostics: Normality, ties, and outlier diagnostics.

  • decision: Automatic selection rationale.

  • htest: Underlying stats::cor.test object.

  • plot: ggplot2 object.

See Also

cor.test, ggplot

Examples

# Pearson (approximately normal data)
set.seed(123)
x <- rnorm(50, sd = 0.1)
y <- x + rnorm(50, sd = 0.1)
test.correlation(x, y)

# Spearman (non-normal data)
set.seed(123)
x <- runif(300)
y <- log(x + 0.1) + rnorm(300, sd = 0.5)
test.correlation(x, y)

# Kendall (ties and outliers)
set.seed(123)
x <- runif(1000, 1, 100)
y <- sin(x) * 30 + rnorm(1000, 0, 10)
x[sample(1:500, 50)] <- 50
y[sample(1:500, 50)] <- 0
x_out <- runif(100, 10, 20)
y_out <- runif(100, 80, 120)
x <- c(x, x_out)
y <- c(y, y_out)
test.correlation(x, y)


autotestR documentation built on April 29, 2026, 1:09 a.m.