correlate
Shows how strongly each pair of columns moves together.
What we have
- the correlation matrix, dropping a row if any variable is missing
Coming later
Nothing known to be missing.
You may shorten it to cor.
How you write it
correlate varlist
Square brackets mean optional. A vertical bar means choose one.
What it does
A grid of correlation coefficients, each between -1 and 1. Near 1 means the two rise together, near -1 that one rises as the other falls, near 0 that knowing one tells you little about the other.
Rows with a blank in *any* of the named columns are dropped entirely (casewise deletion), so every number in the grid is computed on the same people. pwcorr makes the opposite choice.
Watch out. Correlation is about straight-line relationships only, and it is not causation. Two columns can be strongly related and show a correlation near zero if the relationship bends.
Try it
Every example below was run after sysuse vira.sample02, clear.
. correlate mpce age education
(obs=6,000)
| mpce age education
-------------+---------------------------------
mpce | 1.0000
age | 0.0683 1.0000
education | 0.2803 -0.0329 1.0000
The same thing in pandas and in R
pandas
df[['mpce', 'age', 'education']].dropna().corr()
R
cor(df[, c("mpce", "age", "education")], use = "complete.obs")
# This R was run in a real R (4.6.1) and gave the same numbers.