Learn R Programming

mariposa (version 0.7.3)

spearman_rho: Spearman's Rank Correlation Analysis

Description

Calculates Spearman's rank correlation coefficients (rho) between variables with support for weighted correlations, grouped data, and multiple variable pairs. Provides significance testing and SPSS-compatible output formatting.

Spearman's rho is a non-parametric measure of rank correlation that assesses monotonic relationships between variables. It is particularly suitable for ordinal data or when the assumptions of Pearson correlation are not met.

Usage

spearman_rho(
  data,
  ...,
  weights = NULL,
  alternative = c("two.sided", "less", "greater"),
  use = c("pairwise", "listwise"),
  na.rm = NULL
)

Value

Correlation results showing rank-based relationships between variables, including the rho coefficient, p-value, t-statistic, and sample size for each pair. For multiple variables, correlation, significance, and sample size matrices are also provided. Use summary() for the full SPSS-style output with toggleable sections.

Arguments

data

Your survey data (a data frame or tibble)

...

The variables you want to correlate. List two for a single correlation or more for a correlation matrix. You can use helpers like starts_with("trust").

weights

Optional survey weights. Following the SPSS NONPAR CORR convention, weights are used only to filter cases (rows with weight <= 0 or NA are dropped); the rank correlation itself is computed unweighted on the remaining sample. For design-based weighted Spearman use survey::svyolr() or wCorr::weightedCorr(method = "Spearman").

alternative

Direction of the test:

  • "two.sided" (default): Two-tailed test

  • "less": One-tailed test (negative correlation)

  • "greater": One-tailed test (positive correlation)

use

How to handle missing values:

  • "pairwise" (default): Pairwise deletion - each correlation uses all available cases

  • "listwise": Listwise deletion - only complete cases across all variables

na.rm

Deprecated. Use use instead.

Details

Understanding the Results

Spearman's rho measures the strength and direction of a monotonic relationship between two variables. Unlike Pearson correlation, it does not require a linear relationship -- it just needs one variable to consistently increase (or decrease) as the other increases. Think of it as ranking your data first, then checking if the ranks tend to go together.

The rho value ranges from -1 to +1:

  • Strong positive (0.7 to 1.0): High ranks in one variable go with high ranks in the other

  • Moderate positive (0.3 to 0.7): Moderate tendency for ranks to increase together

  • Weak positive (0 to 0.3): Slight tendency for ranks to increase together

  • No correlation (near 0): No relationship between the variables

  • Negative values: As one variable's rank increases, the other's tends to decrease

The output also provides:

  • p-value: Probability of seeing this correlation by chance

  • n: Number of observation pairs used

  • significance stars: Visual indicator (*** very strong, ** strong, * moderate evidence)

When to Use This

Choose Spearman's rho when:

  • Your relationship is monotonic but not necessarily linear

  • Your data has outliers (they have less impact on ranks)

  • Your variables are ordinal (ordered categories)

  • You are not sure if your data meets Pearson correlation assumptions

  • You want to detect any monotonic trend, not just linear ones

Spearman vs. Kendall

  • Spearman's rho is usually larger in magnitude than Kendall's tau

  • Spearman's rho is better for detecting linear relationships in ranks

  • Kendall's tau is more robust and has better statistical properties

  • Spearman's rho is more commonly reported in research

References

Spearman, C. (1904). The proof and measurement of association between two things. American Journal of Psychology, 15(1), 72--101.

Lehmann, E. L. (1975). Nonparametrics: Statistical Methods Based on Ranks. Holden-Day.

See Also

cor with method = "spearman" for the base R implementation.

kendall_tau for Kendall's rank correlation.

pearson_cor for Pearson correlation analysis.

summary.spearman_rho for detailed output with toggleable sections.

Other correlation: kendall_tau(), partial_cor(), pearson_cor()

Examples

Run this code
# Load required packages and data
library(dplyr)
data(survey_data)

# Basic correlation between two variables
survey_data %>%
  spearman_rho(life_satisfaction, political_orientation)

# Correlation matrix for multiple variables
survey_data %>%
  spearman_rho(life_satisfaction, political_orientation, trust_media)

# Weighted correlations (mathematically correct, though SPSS may not apply weights)
survey_data %>%
  spearman_rho(age, income, weights = sampling_weight)

# Grouped correlations
survey_data %>%
  group_by(region) %>%
  spearman_rho(age, income, life_satisfaction)

# Using tidyselect helpers
survey_data %>%
  spearman_rho(starts_with("trust"), weights = sampling_weight)

# Listwise deletion for missing data
survey_data %>%
  spearman_rho(age, income, use = "listwise")

# One-tailed test
survey_data %>%
  spearman_rho(age, income, alternative = "greater")

# --- Three-layer output ---
result <- spearman_rho(survey_data, age, income, life_satisfaction)
result              # compact one-line overview
summary(result)     # full correlation, p-value, and N matrices
summary(result, pvalue_matrix = FALSE)  # hide p-values

Run the code above in your browser using DataLab