utils.stats.partial_correlation

utils.stats.partial_correlation(x, method='pearson')

Calculate the partial correlation matrix for a given data set.

Parameters

Name Type Description Default
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

Name Type Description
dict dict A dictionary containing the partial correlation estimates, p-values, statistics, sample size (n), number of given parameters (gp), and method used.

Raises

Name Type Description
ValueError If input is not a matrix-like structure or not numeric.

References

  1. 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. ]]), ...
}