Bivariate Analysis
Bivariate data has two variables recorded for every observation. The goal is to understand the relationship or association between them.
Questions bivariate analysis answers
- Is the relationship positive, negative or absent?
- How strong is it? (correlation coefficient from -1 to +1)
- Is it linear or curved?
- Are there outliers that break the pattern?
Choosing the right tool
| Variable 1 | Variable 2 | Tool |
|---|---|---|
| Numeric | Numeric | scatter plot, correlation, regression line |
| Numeric | Categorical | box plot, violin plot, group means, t-test / ANOVA |
| Categorical | Categorical | cross-tab (pd.crosstab), stacked bar, chi-square test |
Worked example: temperature and ice-cream sales
| Temperature (C) | Ice cream sales |
|---|---|
| 20 | 2000 |
| 25 | 2500 |
| 30 | 4000 |
| 35 | 5000 |
As temperature rises, sales rise too. That is a positive relationship.
data = {
"Temperature (C)": [20, 25, 30, 35],
"Ice Cream Sales": [2000, 2500, 4000, 5000],
}
df2 = pd.DataFrame(data)
print(df2.corr(numeric_only=True)) # r is about 0.98
sns.scatterplot(data=df2, x="Temperature (C)", y="Ice Cream Sales", s=90)
sns.regplot(data=df2, x="Temperature (C)", y="Ice Cream Sales", scatter=False)
plt.title("Temperature vs Ice Cream Sales")
plt.show()
How the correlation is computed (by hand)
- Mean temperature = 27.5, mean sales = 3375.
- Multiply the deviations of each pair, sum them, and divide by the product of the spreads.
- Result: r is about 0.98, a very strong positive linear relationship.
The blue regression line in the plot summarises this trend and can be used for a rough prediction (for example, expected sales at 32 C).
Numeric vs categorical example (wine data)
# Does alcohol differ across quality levels?
print(df.groupby("quality")["alcohol"].agg(["mean", "median", "std"]))
sns.boxplot(x="quality", y="alcohol", data=df)
plt.show()
If the median alcohol rises as quality rises, alcohol is a promising predictor of quality.
Categorical vs categorical example
ct = pd.crosstab(df["quality"], df["alcohol_level"], normalize="index")
print(ct.round(2))
Applications
- Marketing: how advertising budget affects sales.
- Education: how study hours relate to exam scores.
- Health: how BMI relates to blood pressure.
Advantages and limitations
Advantages: reveals relationships and dependencies; useful for first hypotheses.
Limitations: only two variables at a time; cannot explain multi-factor influence. Correlation is not causation - ice-cream sales and drowning both rise in summer, yet ice cream does not cause drowning; temperature is the hidden third variable.