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 tabletable(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 pairVisualisationQuantify
continuous × continuousscatter plotPearson
categorical × categoricalside-by-side barscompare distributions
categorical × numericside-by-side boxplotscompare 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 qualityreading
alcoholstrongest — the one real signal
densitymoderate negative
chloridesweak negative
volatile acidityweak negative
total sulfur dioxideweak negative
fixed acidity · residual sugar · pH · sulphatesnegligible
citric acid · free sulfur dioxideno linear association
  • which.max(abs(...)) not which.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 loopwine_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