Skip to content

association

Ivan Svetunkov edited this page Feb 19, 2026 · 3 revisions

Association — Measures of Association

Overview

The association module provides functions for calculating various measures of association between variables, including partial correlations, multiple correlations, and general association analysis. These complement the regression modelling tools in greybox by helping identify which variables are related and how strongly.

Import

# R — functions are in the greybox namespace
library(greybox)
# Python
from greybox.association import association, pcor, mcor, determination

Functions

association — General Measures of Association

Calculates a matrix of association measures between all pairs of variables, automatically selecting the appropriate method based on variable types.

Parameters

Parameter R Python Type Default Description
Data x x data.frame / ndarray Matrix or data frame of variables
Response y y vector / ndarray NULL / None Optional response variable (appended to x)
Method use method character / str "everything" / "auto" Method: "auto", "pearson", "spearman", "kendall"

Returns

Dictionary (Python) or list (R) containing:

  • value — matrix of association coefficients
  • p.value — matrix of p-values
  • type — matrix of methods used for each pair

Example

# R
x <- cbind(rnorm(100), rnorm(100), rnorm(100))
result <- association(x)
print(result$value)
print(result$p.value)
# Python
import numpy as np
from greybox.association import association

x = np.column_stack([np.random.randn(100),
                     np.random.randn(100),
                     np.random.randn(100)])
result = association(x)
print(result["value"])     # Correlation matrix
print(result["p.value"])   # P-values
print(result["type"])      # Method used for each pair

pcor — Partial Correlations

Calculates partial correlations between variables. The partial correlation between two variables measures their linear association after removing the effect of all other variables, computed via multiple linear regressions.

Parameters

Parameter R Python Type Default Description
Data x x matrix / ndarray Matrix of variables (>= 2 columns)
Response y y vector / ndarray NULL / None Optional response variable (appended to x)
Method method method character / str "pearson" Correlation method: "pearson", "spearman", "kendall"

Returns

Dictionary (Python) or list (R) containing:

  • value — matrix of partial correlation coefficients
  • p.value — matrix of p-values
  • method — method used

Example

# R
x <- cbind(rnorm(100), rnorm(100), rnorm(100))
result <- pcor(x, method="pearson")
print(result$value)
# Python
import numpy as np
from greybox.association import pcor

x = np.column_stack([np.random.randn(100),
                     np.random.randn(100),
                     np.random.randn(100)])
result = pcor(x, method="pearson")
print(result["value"])     # Partial correlation matrix
print(result["p.value"])   # P-values
print(result["method"])    # "pearson"

mcor — Multiple Correlation

Calculates the multiple correlation coefficient between a set of predictor variables and a dependent variable. This is the square root of R-squared from the regression of y on all other variables.

Parameters

Parameter R Python Type Default Description
Data x x matrix / ndarray Matrix where the last column is the response
Response y y vector / ndarray NULL / None Optional response variable

Returns

float — The multiple correlation coefficient.

Example

# R
x <- cbind(rnorm(100), rnorm(100))
y <- rnorm(100)
mcor(x, y)
# Python
import numpy as np
from greybox.association import mcor

x = np.column_stack([np.random.randn(100), np.random.randn(100)])
y = np.random.randn(100)
result = mcor(x, y)
print(f"Multiple correlation: {result:.4f}")

determination — Coefficient of Determination

Calculates R-squared and adjusted R-squared from actual and predicted values. Useful for evaluating model fit outside of the ALM framework.

Parameters

Parameter R Python Type Default Description
Actuals actual actual vector / ndarray Actual (observed) values
Predicted predicted predicted vector / ndarray Predicted (fitted) values
Predictors k k integer / int 1 Number of predictors (for adjusted R-squared)

Returns

Dictionary with:

  • r2 — R-squared (coefficient of determination)
  • adjR2 — Adjusted R-squared

Example

# R
actual <- c(1, 2, 3, 4, 5)
predicted <- c(1.1, 2.0, 3.2, 3.9, 5.1)
determination(actual, predicted, k=1)
# Python
import numpy as np
from greybox.association import determination

actual = np.array([1, 2, 3, 4, 5])
predicted = np.array([1.1, 2.0, 3.2, 3.9, 5.1])
result = determination(actual, predicted, k=1)
print(f"R-squared:     {result['r2']:.4f}")
print(f"Adj R-squared: {result['adjR2']:.4f}")

R vs Python Differences

Feature R Python
Cramer's V cramer() available Not implemented
Auto method Selects based on variable types (numeric/factor) Uses Pearson by default
Output class S3 objects with print/plot methods Plain dictionaries

References

Clone this wiki locally