Accelerating the pace of engineering and science

# 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

expand all

### Compute Partial Correlation Coefficients

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

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

```load hospital;
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 =

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')
disp(rho)
```
```Partial Correlation Coefficients
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 Correlations with Controlled Variables

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 strings in hospital.Sex to numeric group identifiers.

```load hospital;
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 =

1.0000    0.1300    0.0462
0.1300    1.0000    0.0012
0.0462    0.0012    1.0000

pval =

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',{'SexID','Smoker','Weight'});

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

disp('Partial Correlation Coefficients')
disp(rho)
disp('p-values')
disp(pval)```
```Partial Correlation Coefficients
Age         BPTop      BPBottom
--------    ---------    ---------
SexID            1         0.13     0.046202
Smoker        0.13            1    0.0012475
Weight    0.046202    0.0012475            1

p-values
Age       BPTop     BPBottom
-------    -------    --------
SexID           0    0.20438    0.65316
Smoker    0.20438          0    0.99032
Weight    0.65316    0.99032          0 ```

### Test for Paired Partial Correlation Coefficients

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 strings in hospital.Sex to numeric group identifiers.

```load hospital;
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 =

-0.0257    0.1289
0.0292    0.0472

pval =

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, ...
'VariableNames',{'BPTop','BPBottom'},...
'RowNames',{'Weight','Age'});

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

disp('Partial Correlation Coefficients')
disp(rho)
disp('p-values')
disp(pval)```
```Partial Correlation Coefficients
BPTop      BPBottom
--------    --------
Weight    -0.02568     0.12893
Age       0.029168    0.047226

p-values
BPTop     BPBottom
-------    --------
Weight    0.80182     0.2058
Age       0.77556    0.64424 ```

### One-Tailed Partial Correlation Test

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 strings in hospital.Sex to numeric group identifiers.

```load hospital;
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 =

-0.0257    0.1289
0.0292    0.0472

pval =

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, ...
'VariableNames',{'BPTop','BPBottom'},...
'RowNames',{'Weight','Age'});

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

disp('Partial Correlation Coefficients')
disp(rho)
disp('p-values')
disp(pval)```
```Partial Correlation Coefficients
BPSys       BPDia
Weight    -0.02568     0.12893
Age       0.029168    0.047226

p-values
BPSys      BPDia
Weight    0.59909     0.1029
Age       0.38778    0.32212 ```

## Input Arguments

expand all

### x — Data matrixmatrix

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

### y — Data matrixmatrix

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

### z — Data matrixmatrix

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 Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

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

### 'Type' — Type of partial correlations'Pearson' (default) | 'Spearman'

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' — Rows to use in computation'all' (default) | 'complete' | 'pairwise'

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

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

Using 'pairwise' can produce a rho that is not positive definite. 'complete' always produces a positive definite rho, but when data is missing, the estimates are generally based on fewer observations.

Example: 'Rows','complete'

### 'Tail' — Alternative hypothesis'both' (default) | 'right' | 'left'

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

expand all

### rho — Sample linear partial correlation coefficientsmatrix

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 Sxy = Sxx – (SxzSzz–1SxzT)

### pval — p-valuesmatrix

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.