Command reference

pwcorr — correlate variables using available pairs

Calculate Pearson correlations while using every available pair of values. Different coefficients can use different observations; request a common sample when you need directly comparable matrices.

Syntax

pwcorr [varlist] [if condition] [in range] [, sig listwise]

Provide at least two numeric variables, or omit varlist to use all numeric variables. if and in restrict the sample before each pair's nonmissing observations are selected. The default is pairwise deletion: a missing third variable does not exclude a row from a correlation between the first two.

Options

sig
Print a two-sided p-value beneath each off-diagonal correlation for a test that the population correlation is zero. The calculation uses that pair's sample size and a t statistic with N − 2 degrees of freedom. At least three paired observations are needed.
listwise or casewise
Use only observations that are nonmissing on every listed numeric variable. These synonymous options give every coefficient a common analysis sample, as correlate does by default. Choose either spelling.

Example

The example loads 24 synthetic student records and copies reading scores to read_gapped. It deliberately removes the first four copied scores, then correlates math, the incomplete reading variable, and writing.

clear
import delimited "data/student_scores.csv"
generate read_gapped = read
replace read_gapped = . if id <= 4
pwcorr math read_gapped write
Open example in browser

Sample data included. Opens in a new tab; click Run.

What to look for

Math–reading uses 20 pairs, math–writing uses 23, and reading–writing uses 19. The corresponding correlations are approximately 0.9860, 0.9457, and 0.9801. These different sample sizes are the point of the example: each coefficient uses the observations available for its two variables.