View source: R/Correlation_test.R
| test.correlation | R Documentation |
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.
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, ...)
x |
Numeric vector or data frame with exactly two numeric columns. |
y |
Numeric vector (optional if |
method |
Correlation method: |
main |
Plot title. If |
xlab |
Label for the x-axis. If |
ylab |
Label for the y-axis. If |
style |
Plot style: |
plot_normality |
Logical. If |
help |
Logical. If |
verbose |
Logical. If |
... |
Additional arguments passed to other print methods (currently ignored) |
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.
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.
cor.test,
ggplot
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.