partialcorr

Linear or rank partial correlation coefficients

Description

example

rho = partialcorr(x) returns the sample linear partial correlation coefficients between pairs of variables in x, controlling for the remaining variables in x.

example

rho = partialcorr(x,z) returns the sample linear partial correlation coefficients between pairs of variables in x, controlling for the variables in z.

example

rho = partialcorr(x,y,z) returns the sample linear partial correlation coefficients between pairs of variables in x and y, controlling for the variables in z.

example

rho = partialcorr(___,Name,Value) returns the sample linear partial correlation coefficients with additional options specified by one or more name-value pair arguments, using input arguments from any of the previous syntaxes. For example, you can specify whether to use Pearson or Spearman partial correlations, or specify how to treat missing values.

example

[rho,pval] = partialcorr(___) also returns a matrix pval of p-values for testing the hypothesis of no partial correlation against the one- or two-sided alternative that there is a nonzero partial correlation.

Examples

collapse all

Compute partial correlation coefficients between pairs of variables in the input matrix.

Load the sample data. Convert the genders in hospital.Sex to numeric group identifiers.

hospital.SexID = grp2idx(hospital.Sex);

Create an input matrix containing the sample data.

x = [hospital.SexID hospital.Age hospital.Smoker hospital.Weight];

Each row in x contains a patient’s gender, age, smoking status, and weight.

Compute partial correlation coefficients between pairs of variables in x, while controlling for the effects of the remaining variables in x.

rho = partialcorr(x)
rho = 4×4

1.0000   -0.0105    0.0273    0.9421
-0.0105    1.0000    0.0419    0.0369
0.0273    0.0419    1.0000    0.0451
0.9421    0.0369    0.0451    1.0000

The matrix rho indicates, for example, a correlation of 0.9421 between gender and weight after controlling for all other variables in x. You can return the $p$-values as a second output, and examine them to confirm whether these correlations are statistically significant.

For a clearer display, create a table with appropriate variable and row labels.

rho = array2table(rho, ...
'VariableNames',{'SexID','Age','Smoker','Weight'},...
'RowNames',{'SexID','Age','Smoker','Weight'});

disp('Partial Correlation Coefficients')
Partial Correlation Coefficients
disp(rho)
SexID        Age        Smoker      Weight
________    ________    ________    ________

SexID            1    -0.01052    0.027324      0.9421
Age       -0.01052           1    0.041945    0.036873
Smoker    0.027324    0.041945           1    0.045106
Weight      0.9421    0.036873    0.045106           1

Test for partial correlation between pairs of variables in the input matrix, while controlling for the effects of a second set of variables.

Load the sample data. Convert the genders in hospital.Sex to numeric group identifiers.

hospital.SexID = grp2idx(hospital.Sex);

Create two matrices containing the sample data.

x = [hospital.Age hospital.BloodPressure];
z = [hospital.SexID hospital.Smoker hospital.Weight];

The x matrix contains the variables to test for partial correlation. The z matrix contains the variables to control for. The measurements for BloodPressure are contained in two columns: The first column contains the upper (systolic) number, and the second column contains the lower (diastolic) number. partialcorr treats each column as a separate variable.

Test for partial correlation between pairs of variables in x, while controlling for the effects of the variables in z. Compute the correlation coefficients.

[rho,pval] = partialcorr(x,z)
rho = 3×3

1.0000    0.1300    0.0462
0.1300    1.0000    0.0012
0.0462    0.0012    1.0000

pval = 3×3

0    0.2044    0.6532
0.2044         0    0.9903
0.6532    0.9903         0

The large values in pval indicate that there is no significant correlation between age and either blood pressure measurement after controlling for gender, smoking status, and weight.

For a clearer display, create tables with appropriate variable and row labels.

rho = array2table(rho, ...
'VariableNames',{'Age','BPTop','BPBottom'},...
'RowNames',{'Age','BPTop','BPBottom'});

pval = array2table(pval, ...
'VariableNames',{'Age','BPTop','BPBottom'},...
'RowNames',{'Age','BPTop','BPBottom'});

disp('Partial Correlation Coefficients')
Partial Correlation Coefficients
disp(rho)
Age         BPTop      BPBottom
________    _________    _________

Age                1         0.13     0.046202
BPTop           0.13            1    0.0012475
BPBottom    0.046202    0.0012475            1
disp('p-values')
p-values
disp(pval)
Age       BPTop     BPBottom
_______    _______    ________

Age               0    0.20438    0.65316
BPTop       0.20438          0    0.99032
BPBottom    0.65316    0.99032          0

Test for partial correlation between pairs of variables in the x and y input matrices, while controlling for the effects of a third set of variables.

Load the sample data. Convert the genders in hospital.Sex to numeric group identifiers.

hospital.SexID = grp2idx(hospital.Sex);

Create three matrices containing the sample data.

x = [hospital.BloodPressure];
y = [hospital.Weight hospital.Age];
z = [hospital.SexID hospital.Smoker];

partialcorr can test for partial correlation between the pairs of variables in x (the systolic and diastolic blood pressure measurements) and y (weight and age), while controlling for the variables in z (gender and smoking status). The measurements for BloodPressure are contained in two columns: The first column contains the upper (systolic) number, and the second column contains the lower (diastolic) number. partialcorr treats each column as a separate variable.

Test for partial correlation between pairs of variables in x and y, while controlling for the effects of the variables in z. Compute the correlation coefficients.

[rho,pval] = partialcorr(x,y,z)
rho = 2×2

-0.0257    0.1289
0.0292    0.0472

pval = 2×2

0.8018    0.2058
0.7756    0.6442

The results in pval indicate that, after controlling for gender and smoking status, there is no significant correlation between either of a patient’s blood pressure measurements and that patient’s weight or age.

For a clearer display, create tables with appropriate variable and row labels.

rho = array2table(rho, ...
'RowNames',{'BPTop','BPBottom'},...
'VariableNames',{'Weight','Age'});

pval = array2table(pval, ...
'RowNames',{'BPTop','BPBottom'},...
'VariableNames',{'Weight','Age'});

disp('Partial Correlation Coefficients')
Partial Correlation Coefficients
disp(rho)
Weight       Age
________    ________

BPTop       -0.02568     0.12893
BPBottom    0.029168    0.047226
disp('p-values')
p-values
disp(pval)
Weight       Age
_______    _______

BPTop       0.80182     0.2058
BPBottom    0.77556    0.64424

Test the hypothesis that pairs of variables have no correlation, against the alternative hypothesis that the correlation is greater than 0.

Load the sample data. Convert the genders in hospital.Sex to numeric group identifiers.

hospital.SexID = grp2idx(hospital.Sex);

Create three matrices containing the sample data.

x = [hospital.BloodPressure];
y = [hospital.Weight hospital.Age];
z = [hospital.SexID hospital.Smoker];

partialcorr can test for partial correlation between the pairs of variables in x (the systolic and diastolic blood pressure measurements) and y (weight and age), while controlling for the variables in z (gender and smoking status). The measurements for BloodPressure are contained in two columns: The first column contains the upper (systolic) number, and the second column contains the lower (diastolic) number. partialcorr treats each column as a separate variable.

Compute the correlation coefficients using a right-tailed test.

[rho,pval] = partialcorr(x,y,z,'Tail','right')
rho = 2×2

-0.0257    0.1289
0.0292    0.0472

pval = 2×2

0.5991    0.1029
0.3878    0.3221

The results in pval indicate that partialcorr does not reject the null hypothesis of nonzero correlations between the variables in x and y, after controlling for the variables in z, when the alternative hypothesis is that the correlations are greater than 0.

For a clearer display, create tables with appropriate variable and row labels.

rho = array2table(rho, ...
'RowNames',{'BPTop','BPBottom'},...
'VariableNames',{'Weight','Age'});

pval = array2table(pval, ...
'RowNames',{'BPTop','BPBottom'},...
'VariableNames',{'Weight','Age'});

disp('Partial Correlation Coefficients')
Partial Correlation Coefficients
disp(rho)
Weight       Age
________    ________

BPTop       -0.02568     0.12893
BPBottom    0.029168    0.047226
disp('p-values')
p-values
disp(pval)
Weight       Age
_______    _______

BPTop       0.59909     0.1029
BPBottom    0.38778    0.32212

Input Arguments

collapse all

Data matrix, specified as an n-by-px matrix. The rows of x correspond to observations, and the columns correspond to variables.

Data Types: single | double

Data matrix, specified as an n-by-py matrix. The rows of y correspond to observations, and the columns correspond to variables.

Data Types: single | double

Data matrix, specified as an n-by-pz matrix. The rows of z correspond to observations, and columns correspond to variables.

Data Types: single | double

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: 'Type','Spearman','Rows','complete' computes Spearman partial correlations using only the data in rows that contain no missing values.

Type of partial correlations to compute, specified as the comma-separated pair consisting of 'Type' and one of the following.

 'Pearson' Compute Pearson (linear) partial correlations. 'Spearman' Compute Spearman (rank) partial correlations.

Example: 'Type','Spearman'

Rows to use in computation, specified as the comma-separated pair consisting of 'Rows' and one of the following.

 'all' Use all rows of the input regardless of missing values (NaNs). 'complete' Use only rows of the input with no missing values. 'pairwise' Compute rho(i,j) using rows with no missing values in column i or j.

Example: 'Rows','complete'

Alternative hypothesis to test against, specified as the comma-separated pair consisting of 'Tail' and one of the following.

 'both' Test the alternative hypothesis that the correlation is not 0. 'right' Test the alternative hypothesis that the correlation is greater than 0. 'left' Test the alternative hypothesis that the correlation is less than 0.

Example: 'Tail','right'

Output Arguments

collapse all

Sample linear partial correlation coefficients, returned as a matrix.

• If you input only an x matrix, rho is a symmetric px-by-px matrix. The (i,j)th entry is the sample linear partial correlation between the i-th and j-th columns in x.

• If you input x and z matrices, rho is a symmetric px-by-px matrix. The (i,j)th entry is the sample linear partial correlation between the ith and jth columns in x, controlled for the variables in z.

• If you input x, y, and z matrices, rho is a px-by-py matrix, where the (i,j)th entry is the sample linear partial correlation between the ith column in x and the jth column in y, controlled for the variables in z.

If the covariance matrix of [x,z] is

$S=\left(\begin{array}{cc}{S}_{xx}& {S}_{xz}\\ {S}_{xz}{}^{T}& {S}_{zz}\end{array}\right)\text{\hspace{0.17em}},$

then the partial correlation matrix of x, controlling for z, can be defined formally as a normalized version of the covariance matrix: Sxx – (SxzSzz–1SxzT).

p-values, returned as a matrix. Each element of pval is the p-value for the corresponding element of rho.

If pval(i,j) is small, then the corresponding partial correlation rho(i,j) is statistically significantly different from 0.

partialcorr computes p-values for linear and rank partial correlations using a Student's t distribution for a transformation of the correlation. This is exact for linear partial correlation when x and z are normal, but is a large-sample approximation otherwise.

References

[1] Stuart, Alan, K. Ord, and S. Arnold. Kendall's Advanced Theory of Statistics. 6th edition, Volume 2A, Chapter 28, Wiley, 2004.

[2] Fisher, Ronald A. "The Distribution of the Partial Correlation Coefficient." Metron 3 (1924): 329-332

Version History

Introduced before R2006a