Association Between Variables
Context: FIT1043_MOC, FIT2086_MOC · is there a relationship between two variables? · method depends on the type pair · Pearson via df.corr() FIT2086 framing: Pearson correlation measures linear association only — does not imply independence ( and points on a circle both give despite deterministic association); and correlation causation.
Quick Revision
- 🎯 Objective: detect a relationship between two variables ➔ pick the tool by the variable-type pair.
- 📦 Core Components: cont–cont → scatter + Pearson | cat–cat → side-by-side bars | cat–num → side-by-side boxplots.
- ⚡ Key Constraint: Pearson measures linear association only — can still hide a strong non-linear relationship; and correlation causation.
📝 How It Works
1. Two Continuous Variables
- Pearson correlation ➔ measures linear association; (−1 perfectly negative, +1 perfectly positive, 0 = no linear association).
- Scatter plot ➔ plot vs to see the relationship; combine with for strength + shape.
- Non-linear blind spot ➔ gives though clearly associated; a deterministic curve can give .
2. Two Categorical Variables
- Side-by-side bar graphs ➔ compare each category’s distribution; if the bar shapes differ across groups, a possible association (e.g. cancer frequency vs ethnicity — if unchanged, unlikely associated).
- Contingency table ➔
table(a, b)quantifies the same comparison; association shows as a category’s split departing from the overall base rate (mechanics ➔ Categorical Summaries and Cross-Tabulation in R).
3. Categorical + Numeric
- Side-by-side boxplots ➔ split the numeric variable by category and compare boxplots; different distributions ⇒ association (e.g. price varies with number of rooms, but not much between two suburbs).
⚖️ Core Decision Matrix
| Variable pair | Visualisation | Quantify |
|---|---|---|
| continuous × continuous | scatter plot | Pearson |
| categorical × categorical | side-by-side bars | compare distributions |
| categorical × numeric | side-by-side boxplots | compare group boxplots |
When It Flips: near tightens the scatter toward a line (e.g. vs ); but only sees linear trend, so always plot as well as compute.
🔧 Screening many predictors in R (FIT2086 studio — wine.csv)
wine <- read.csv("wine.csv")
wine_corr = c()
for (i in 1:11) # columns 1..11 are the attributes
{
wine_corr[i] = cor(wine[,i], wine$quality)
cat("Correlation between", names(wine)[i], "and quality = ", wine_corr[i], "\n")
}
cat("Variable with maximum correlation is:", names(wine)[which.max(abs(wine_corr))], "\n")Expected output ( wines):
| attribute | with quality | reading |
|---|---|---|
| alcohol | strongest — the one real signal | |
| density | moderate negative | |
| chlorides | weak negative | |
| volatile acidity | weak negative | |
| total sulfur dioxide | weak negative | |
| fixed acidity · residual sugar · pH · sulphates | negligible | |
| citric acid · free sulfur dioxide | no linear association |
which.max(abs(...))notwhich.max(...)➔ a strong negative association is just as informative as a positive one; taking the absolute value first is what makes the screen correct.- Building a vector in a loop ➔
wine_corr = c()creates an empty vector that grows by index assignment;names(wine)[i]recovers the -th column’s name for the printout. - What this screen is for ➔ ranking candidate predictors before modelling ➔ but a near-zero only rules out a linear relationship, so a variable dismissed here can still matter in a non-linear model.
⚠️ Common Mistakes
- 💡 Correlation ≠ causation ➔ a strong never proves one variable causes the other.
- 💡 ≠ no relationship ➔ it means no linear relationship; a scatter may reveal a clear curve (e.g. ).
- 💡 Ranking predictors by signed ➔ use
which.max(abs(r)); the strongest association can be negative, and a signed sort silently buries it.
🧠 Active Recall
Data with has — is associated with ? What does this teach?
- Hint: Linear-only measure.
Answer
- Short answer: Yes — they are clearly associated, but non-linearly, so Pearson ; always plot the data, don’t rely on alone.
- Why: Linear scope ➔ Pearson captures only straight-line association; symmetric curves cancel to .
Which visualisation detects association for each variable-type pair?
- Hint: Match tool to types.
Answer
- Short answer: cont–cont → scatter plot (+ Pearson ); cat–cat → side-by-side bar graphs; cat–num → side-by-side boxplots.
- Why: Distribution comparison ➔ association shows as differing distributions across groups, or a trend in the scatter.