Skip to content

association

Ivan Svetunkov edited this page Jun 24, 2026 · 3 revisions

Association — Measures of Association

Function signatures

R

association(x, y=NULL, use=c("na.or.complete","complete.obs","everything","all.obs"),
            method=c("auto","pearson","spearman","kendall","cramer"))
pcor(x, y=NULL, use=c("na.or.complete","complete.obs","everything","all.obs"),
     method=c("pearson","spearman","kendall"))
mcor(x, y, use=c("na.or.complete","complete.obs","everything","all.obs"))
determination(xreg, bruteforce=TRUE, ...)

Python

def association(x, y=None, method: str = "auto") -> dict: ...
def pcor(x, y=None,
         method: Literal["pearson", "spearman", "kendall"] = "pearson") -> dict: ...
def mcor(x, y=None) -> float: ...
def determination(xreg, bruteforce: bool = True) -> np.ndarray: ...

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)
What to use use - character / str "na.or.complete" What to do with missing values
Method method method character / str "auto" What method to use for measures calculation

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)
What to use use - character / str "na.or.complete"
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
What to use use - character / str "na.or.complete"

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 — Coefficients of Determination

Calculates coefficients of determination (R²) between all provided variables. The higher the coefficient for a variable, the higher the potential multicollinearity effect when that variable is used in a regression model with other predictors.

The coefficient is connected directly to Variance Inflation Factor (VIF): VIF = 1 / (1 - R²). The multicollinearity can be considered serious when determination > 0.9 (which corresponds to VIF > 10).

Parameters

Parameter R Python Type Default Description
Data xreg xreg matrix / ndarray Data frame or matrix of exogenous variables
Bruteforce bruteforce bruteforce logical / bool TRUE If TRUE, use all variables for regression (sink). If FALSE, use stepwise selection when n_obs <= n_vars

Returns

np.ndarray (Python) or numeric (R) — Vector of determination coefficients, one for each variable.

Example

# R
xreg <- cbind(rnorm(100, 10, 3), rnorm(100, 50, 5))
xreg <- cbind(100 + 0.5*xreg[,1] - 0.75*xreg[,2] + rnorm(100, 0, 3), xreg)
colnames(xreg) <- c("x1", "x2", "x3")
result <- determination(xreg)
print(result)
# x3 will be high (derived from x1, x2)
# Python
import numpy as np
from greybox.association import determination

np.random.seed(42)
x1 = np.random.normal(10, 3, 100)
x2 = np.random.normal(50, 5, 100)
x3 = 100 + 0.5*x1 - 0.75*x2 + np.random.normal(0, 3, 100)
xreg = np.column_stack([x3, x1, x2])
result = determination(xreg)
print(result)  # array([1.0, 0.28, 0.56])
# x3 will be high (derived from x1, x2)

VIF Calculation

To calculate VIF from determination coefficients:

# Python
vif = 1 / (1 - result)
print(vif)
# Values > 10 indicate serious multicollinearity
# R
vif <- 1 / (1 - result)
print(vif)
# Values > 10 indicate serious multicollinearity

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