utils.stats.partial_correlation
utils.stats.partial_correlation(x, method='pearson')
Calculate the partial correlation matrix for a given data set.
Parameters
| x |
pandas.DataFrame or numpy.ndarray |
The data matrix with variables as columns. |
required |
| method |
str |
Correlation method, one of ‘pearson’, ‘kendall’, or ‘spearman’. |
'pearson' |
Returns
| dict |
dict |
A dictionary containing the partial correlation estimates, p-values, statistics, sample size (n), number of given parameters (gp), and method used. |
Raises
|
ValueError |
If input is not a matrix-like structure or not numeric. |
References
- Kim, S. ppcor: An R package for a fast calculation to semi-partial correlation coefficients. Commun Stat Appl Methods 22, 6 (Nov 2015), 665–674.
Examples
>>> from spotpython.utils.stats import partial_correlation
>>> import numpy as np
>>> import pandas as pd
>>> data = pd.DataFrame({
>>> 'A': [1, 2, 3],
>>> 'B': [4, 5, 6],
>>> 'C': [7, 8, 9]
>>> })
>>> partial_correlation(data, method='pearson')
{'estimate': array([[ 1. , -1. , 1. ],
[-1. , 1. , -1. ],
[ 1. , -1. , 1. ]]),
'p_value': array([[0. , 0. , 0. ],
[0. , 0. , 0. ],
[0. , 0. , 0. ]]), ...
}