-
Notifications
You must be signed in to change notification settings - Fork 8
association
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.
# R — functions are in the greybox namespace
library(greybox)# Python
from greybox.association import association, pcor, mcor, determinationCalculates a matrix of association measures between all pairs of variables, automatically selecting the appropriate method based on variable types.
| 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"
|
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
# 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 pairCalculates 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.
| 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"
|
Dictionary (Python) or list (R) containing:
-
value— matrix of partial correlation coefficients -
p.value— matrix of p-values -
method— method used
# 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"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.
| 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 |
float — The multiple correlation coefficient.
# 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}")Calculates R-squared and adjusted R-squared from actual and predicted values. Useful for evaluating model fit outside of the ALM framework.
| 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) |
Dictionary with:
-
r2— R-squared (coefficient of determination) -
adjR2— Adjusted R-squared
# 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}")| 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 |
- Svetunkov, I. (2023). Statistics for Business Analytics. https://openforecast.org/sba/