Do two variables go together?

Bivariate analysis describes the relationship between two variables observed on the same units: education and salary of the same people, height and weight of the same adults. The right tool depends on the pair of types, which is why classifying the variables comes first. With two categorical variables, the categories are crossed in a table; with two quantitative ones, the pairs become points on a scatterplot; with one of each, the quantitative variable is compared across the groups of the categorical one.

When one variable helps explain the other, it is worth naming the roles. Agresti and Finlay call the variable whose behavior you want to understand the response, and the one that defines the groups or the horizontal axis the explanatory variable. Choosing roles does not prove causation, since everything here is descriptive, but it guides how tables and charts are read.

Test yourself with the quiz, use the assistant for a pair of variables of your own and then visit each tab, with simulated data, charts and the measures from Agresti and Finlay (Statistical Methods for the Social Sciences, ch. 3) and Morettin and Bussab (Estatística Básica, ch. 4).

Quiz: which analysis?

Assistant: classify your pair
Which table, chart and measure to use
Pair of variables Table Chart Measure of association
Categorical × categorical contingency table, with row, column or total percentages stacked bars with the profile of each category χ², contingency coefficient C and coefficient T
Quantitative × quantitative means, standard deviations and covariance scatterplot correlation coefficient (and regression line)
Categorical × quantitative summary measures of the quantitative variable in each group side-by-side box plots (and Q-Q plot for two groups) R² = 1 − mean within-group variance / total variance

Ordinal variables count as categorical. Variances, standard deviations and covariances divide by n, as Morettin and Bussab do in ch. 4; Agresti and Finlay use n − 1. The correlation is the same either way.

Categorical × categorical

Does the distribution of one variable change across the categories of the other? χ², contingency coefficient C and coefficient T

Chi-squared (χ²)

Contingency coefficient (C)

Coefficient T

Contingency table
Measures of association
Watch out
  • Base of the percentages: row and column percentages answer different questions. "What share of college graduates use the internet" is not the same as "what share of internet users are college graduates".
  • χ² on its own: grows with sample size. To compare tables of different sizes, use C or T.
  • Maximum value: C never reaches 1, and T only reaches 1 in 2 × 2 tables. Read each one against the maximum possible for the size of the table, shown in the measures panel.
  • Association is not causation: the table shows that the variables go together, not why. A third variable, such as income, may be behind both.
  • Formal test: deciding whether the association goes beyond chance is the job of the chi-squared test of independence (Morettin and Bussab, ch. 14; Agresti and Finlay, ch. 8). In R, chisq.test() applies Yates' correction to 2 × 2 tables; use correct = FALSE to get the descriptive χ² shown here.
Key ideas

With two categorical variables, the starting point is the contingency table: each cell counts the observations that fall at the same time in one row category and one column category. How you read it depends on the base of the percentages, and the base comes from the question. When one variable helps explain the other, as education explains internet use, the explanatory variable goes in the rows and the percentages are computed within each row. Agresti and Finlay describe the table this way, with the response as percentages within each category of the explanatory variable.

If the variables were independent, every row would have the same profile as the Total row. Morettin and Bussab measure how far the table is from that by comparing each observed frequency \(n_{ij}\) with the frequency expected under independence, \(n^*_{ij} = n_{i\cdot}\, n_{\cdot j} / n\). The result is Pearson's chi-squared, \(\chi^2 = \sum_i \sum_j (n_{ij} - n^*_{ij})^2 / n^*_{ij}\), which is 0 when observed and expected match and grows as they move apart. Since \(\chi^2\) also grows with \(n\), the book offers two coefficients that do not depend on sample size: the contingency coefficient, \(C = \sqrt{\chi^2 / (\chi^2 + n)}\), and \(T = \sqrt{(\chi^2 / n) / ((r - 1)(s - 1))}\), where \(r\) and \(s\) are the numbers of rows and columns.

Try it: set the strength of the association to 0 and draw a few samples. The variables are independent in the population, but the sample \(\chi^2\) is almost never exactly 0, since chance always pulls the rows a little apart. Then raise the strength and watch the profiles separate in the chart.

Quantitative × quantitative

When one variable goes up, does the other tend to go up, go down, or neither? correlation coefficient (and regression line)

Correlation (r)

Covariance

Regression line

Measures
Key ideas

With two quantitative variables, the first step is always the scatterplot, with one point per observation. Agresti and Finlay put the response variable on the vertical axis and the explanatory one on the horizontal axis. The chart is where the form of the relationship (a line, a curve or nothing) shows up, before any number.

The lines at the two means split the chart into four quadrants. In the blue quadrants, \(x\) and \(y\) are on the same side of their means, and the product \((x_i - \bar{x})(y_i - \bar{y})\) is positive; in the red ones, the product is negative. The covariance is the mean of these products, \(\text{cov}(X, Y) = \sum (x_i - \bar{x})(y_i - \bar{y}) / n\) in Morettin and Bussab. Its sign tells whether the relationship goes up or down, but its size depends on the units. The correlation fixes that by dividing by both standard deviations: \(r = \text{cov}(X, Y) / (s_X\, s_Y)\), always between −1 and 1.

Correlation only measures the linear part of a relationship. A U-shaped relationship can be strong and still have a correlation near zero, since the points on the left and on the right cancel each other out. The regression line, \(\hat{y} = a + b x\), summarizes the linear trend and predicts \(y\) from \(x\); its slope is \(b = r\, s_Y / s_X\).

Try it: choose temperature and energy use. The relationship is clear in the chart, but the correlation stays near zero. Then go back to height and weight, add the outlier with a small sample and see how much a single point moves the correlation and the line.

Watch out
  • Correlation near zero: does not mean no relationship. The relationship may be curved, as in the U-shaped example.
  • Outliers: a single point far from the rest can change the correlation and the line a lot, especially in small samples.
  • Covariance: depends on the units: measuring height in meters instead of centimeters divides the covariance by 100. To talk about the strength of the relationship, use the correlation.
  • Association is not causation: selling more when the price drops is plausible, but the correlation does not prove the mechanism. A third variable may drive both.
  • Predicting beyond the data: the line describes the observed range of x. Stretching it far beyond that range is extrapolation.

Categorical × quantitative

Do the values of the quantitative variable change from one group to another? R² = 1 − mean within-group variance / total variance

Total variance, var(S)

Mean within-group variance

R²

Summary measures by group
Key ideas

When one variable is categorical and the other quantitative, the analysis compares the distribution of the quantitative variable across the groups defined by the categorical one. Agresti and Finlay suggest comparing means and medians and placing box plots side by side; Morettin and Bussab do the same, with summary measures for each group.

To sum up the association in one number, Morettin and Bussab start from the variance. If the groups account for much of the variation, the variance within each group is much smaller than the total variance. The mean within-group variance, weighted by group size, is \(\overline{var}(S) = \sum n_i\, var_i(S) / \sum n_i\), and the degree of association is \(R^2 = 1 - \overline{var}(S) / var(S)\). It is 0 when the groups remove none of the variance and gets close to 1 when almost all the variation lies between groups rather than within them. In other words, \(R^2\) is the share of the total variance explained by differences between groups.

Try it: with education and salary, move the difference between groups from 0 to the maximum. The boxes pull apart, the within-group variances fall below the total and \(R^2\) rises. Compare with region and commute time, where the differences are small and \(R^2\) stays near zero even at the maximum.

Watch out
  • Similar means: do not guarantee similar distributions. Also look at the spread and shape of each box.
  • Small groups: have unstable means and variances. Check the n of each group before comparing.
  • Skewed distributions: such as salaries, pull the mean and median apart. In that case, compare medians and read R² with care, because a few high values weigh heavily on the variance.
  • Variance divisor: this tab divides by n, as Morettin and Bussab do in ch. 4. With that divisor, R² is exactly the share of the between-group sum of squares, the same as in analysis of variance (ch. 15).
  • Association is not causation: the salary gap between education levels also carries age, region and family background, among other variables.

Q-Q plot

Do the two samples have the same distribution? If not, do they differ in location, spread or shape? same-order quantiles of both samples

Difference in medians (B − A)

Location

Ratio of interquartile ranges (B ÷ A)

Spread; close to the slope of the points
Quantiles of the two classes
Watch out
  • Same size: with equal n, each point is a pair of sorted observations. With different sizes, the quantiles of one sample must be interpolated.
  • Small samples: wander quite a bit around the line, even when they come from the same distribution. Draw again before concluding.
  • Ends of the plot: extreme quantiles rest on few observations and vary more than the central ones.
  • Not the normality plot: here the two samples are compared with each other. The normal quantile plot, used to check normality, follows the same idea but compares one sample with a theoretical distribution.
Key ideas

The Q-Q plot compares two distributions point by point instead of summarizing each in a single number. Morettin and Bussab present it at the end of ch. 4: with two samples of the same size \(n\), the values of each one are sorted, and the \(i\)-th smallest observation of A is paired with the \(i\)-th smallest of B. Both are the quantile of order \(p_i = (i - 0.5)/n\) of their sample, and each pair becomes a point.

Reading starts from the line \(y = x\). If the two distributions are equal, the points sit on it. If B is A shifted, the points form a parallel line, above it (B larger) or below it (B smaller). If B is more spread out, the slope is above 1; if less, below 1. When the shapes differ, such as a symmetric against a skewed one, the points bend, and the bend shows mostly at the ends.

Try it: start with shift 0 and ratio 1, and the points stay around the line. Move only the shift and then only the ratio, to see the two signatures separately. Finally, switch the shape of B to skewed.

About: Bivariate analysis

How to describe the relationship between two variables depending on the pair of types: contingency table, chi-squared and contingency coefficients for two categorical variables; scatterplot, covariance, correlation and regression line for two quantitative ones; box plots, R² and Q-Q plot for one of each. The concepts follow Agresti and Finlay (Statistical Methods for the Social Sciences, ch. 3) and Morettin and Bussab (Estatística Básica, ch. 4).

The app runs in R on a server (Shiny Server, hosted on Hugging Face). The data are simulated and nothing you choose here is stored. If the server is asleep, the first load can take up to a minute.

Author: Thiago Meireles | Version 0.1 (November 2026)

See all topics | thiagomeireles.com.br