Statistics for
Data Science 1
Association between variables: contingency tables, relative frequencies, scatter plots, covariance, correlation and fitted lines.
Chapters
- 01Contingency TablesLectures 4.1 and 4.2
- 02Relative FrequenciesLecture 4.3
- 03Is There an Association?Lecture 4.3
- 04Stacked Bar ChartsLecture 4.3
- 05Scatter PlotsLecture 4.4
- 06Describing AssociationLecture 4.5
- 07CovarianceLecture 4.6
- 08CorrelationLecture 4.7
- 09Fitting a LineLectures 4.7 and 4.8
- 10Categorical and Numerical VariablesLecture 4.9
- 11Week 4 Summary SheetAll lectures
Based on lectures by Usha Mohan, IIT Madras
Contingency Tables
Weeks 1 to 3 described one variable at a time. Week 4 asks whether two variables are connected. The lecture opens with a review of the course so far.
1.1Where this fits and the three objectives
- What is statistics: descriptive and inferential statistics.
- How data are collected: variables and cases (observations).
- Types of data: categorical or numerical; nominal, ordinal, interval, ratio.
- Categorical data: frequency tables, relative frequency, pie and bar charts, mode, median.
- Numerical data: frequency tables, mean, median, mode, range, variance, SD, percentiles, IQR.
- Two categorical variables: use two-way contingency tables to understand association.
- Two numerical variables: understand association through scatter plots; compute and interpret correlation.
- Mixed variables: understand the relationship between a categorical and a numerical variable.
1.2The contingency table
| Gender \ Own a smartphone | No | Yes | Row total |
|---|---|---|---|
| Female | 10 | 34 | 44 |
| Male | 14 | 42 | 56 |
| Column total | 24 | 76 | 100 |
- 34 = females who own a smartphone
- 44 = all female students
- 76 = all smartphone owners
- 100 = all students surveyed
1.3Lecture examples
Example 1: gender versus use of a smartphone
A market research firm wants to find out whether ownership of a smartphone is associated with the gender of a student: do more females own a smartphone than males, or is owning one independent of gender? A group of 100 college-going children were surveyed.
- There are 44 female and 56 male students.
- 76 students owned a smartphone and 24 did not.
- 34 female students and 42 male students owned a smartphone.
- Identify the variables. Gender (Male, Female): 2 categories, nominal. Own a smartphone (Yes, No): 2 categories, nominal.
- Fill in what is given. Row totals 44 and 56; column totals 24 (No) and 76 (Yes); the Yes cells are 34 and 42.
- Find the missing cells by subtraction. Female No = 44 - 34 = 10; Male No = 56 - 42 = 14.
- Check. Column No: 10 + 14 = 24; column Yes: 34 + 42 = 76; grand total 24 + 76 = 44 + 56 = 100.
| Gender \ Own a smartphone | No | Yes | Row total |
|---|---|---|---|
| Female | 10 | 34 | 44 |
| Male | 14 | 42 | 56 |
| Column total | 24 | 76 | 100 |
Example 2: income versus use of a smartphone
The firm now asks whether ownership of a smartphone is associated with income. A group of 100 randomly picked individuals were surveyed.
- 20 High, 66 Medium and 14 Low income participants.
- 62 owned a smartphone and 38 did not.
- 18 High, 39 Medium and 5 Low income participants owned a smartphone.
- Identify the variables. Income level (Low, Medium, High): 3 categories, ordinal. Own a smartphone (Yes, No): 2 categories, nominal.
- Missing cells. High No = 20 - 18 = 2; Medium No = 66 - 39 = 27; Low No = 14 - 5 = 9.
- Check. Column No: 2 + 27 + 9 = 38; column Yes: 18 + 39 + 5 = 62; 38 + 62 = 20 + 66 + 14 = 100.
| Income level \ Own a smartphone | No | Yes | Row total |
|---|---|---|---|
| High | 2 | 18 | 20 |
| Medium | 27 | 39 | 66 |
| Low | 9 | 5 | 14 |
| Column total | 38 | 62 | 100 |
1.4Building a contingency table in Google Sheets
- Choose the columns of the variables for which you seek an association.
- Go to Data and click the Pivot table option.
- Click Create; this opens the pivot table editor.
- Under Rows, click the first categorical variable.
- Under Columns, click the second categorical variable.
- Under Values, click either of the variables, then choose COUNTA under “Summarize by”.
1.5Going deeper: reading a contingency table
- Every case sits in exactly one cell. That is why the cells add up to the grand total (here 100).
- Totals are the one-variable tables. The row-total column is the frequency table of the row variable alone; the column-total row is the frequency table of the column variable alone. These two are called the marginal totals (an extra term, not from the lecture).
- Subtraction is the shortcut. When a total and one cell are given, the other cell in that row is the difference. Always finish with the column check.
- Which variable goes on the rows? Either works. In these notes the variable whose groups you want to compare (gender, income) goes down the rows.
- Write the totals first, then fill the cells.
- Check every row and every column, and the grand total.
- Do not reorder an ordinal variable.
- Putting the same count in two cells.
- Mixing up a cell with a total.
- Forgetting to keep the order of Low, Medium, High.
Relative Frequencies
Raw counts are hard to compare when the groups have different sizes. Two questions show why: What proportion of all participants own a smartphone? and What proportion of the female participants own a smartphone? The second question divides by the number of females, not by 100.
divide the cell by its row total
divide the cell by its column total
2.1Row relative frequencies: lecture examples
Example 3: gender and smartphone
- Divide every cell by the total of its row (44 for Female, 56 for Male). The column-total row is divided by the grand total, 100.
- Convert to percentages.
| Gender \ Own a smartphone | No | Yes | Row total |
|---|---|---|---|
| Female | 10/44 | 34/44 | 44 |
| Male | 14/56 | 42/56 | 56 |
| Column total | 24/100 | 76/100 | 100 |
| Gender \ Own a smartphone | No | Yes | Row total |
|---|---|---|---|
| Female | 22.73% | 77.27% | 44 |
| Male | 25.00% | 75.00% | 56 |
| Column total | 24.00% | 76.00% | 100 |
77.27% of the female students own a smartphone, and 75.00% of the male students do. Overall 76.00% own one.
Example 4: income and smartphone
- Divide by the row totals 20, 66 and 14 (and the column-total row by 100).
- As percentages.
| Income level \ Own a smartphone | No | Yes | Row total |
|---|---|---|---|
| High | 2/20 | 18/20 | 20 |
| Medium | 27/66 | 39/66 | 66 |
| Low | 9/14 | 5/14 | 14 |
| Column total | 38/100 | 62/100 | 100 |
| Income level \ Own a smartphone | No | Yes | Row total |
|---|---|---|---|
| High | 10.00% | 90.00% | 20 |
| Medium | 40.91% | 59.09% | 66 |
| Low | 64.29% | 35.71% | 14 |
| Column total | 38.00% | 62.00% | 100 |
90.00% of High income participants own a smartphone, 59.09% of Medium and only 35.71% of Low.
2.2Column relative frequencies: lecture examples
Now the questions turn around: What proportion of all participants are female? and What proportion of smartphone owners are female? The second divides by the number of owners.
Example 5: gender and smartphone
- Divide by the column totals 24 (No) and 76 (Yes); the row-total column is divided by 100.
- As percentages.
| Gender \ Own a smartphone | No | Yes | Row total |
|---|---|---|---|
| Female | 10/24 | 34/76 | 44/100 |
| Male | 14/24 | 42/76 | 56/100 |
| Column total | 24 | 76 | 100 |
| Gender \ Own a smartphone | No | Yes | Row total |
|---|---|---|---|
| Female | 41.67% | 44.74% | 44.00% |
| Male | 58.33% | 55.26% | 56.00% |
| Column total | 24 | 76 | 100 |
44.74% of the smartphone owners are female, while 44.00% of all participants are female.
Example 6: income and smartphone
- Divide by the column totals 38 and 62 (and the row totals by 100).
- As percentages.
| Income level \ Own a smartphone | No | Yes | Row total |
|---|---|---|---|
| High | 2/38 | 18/62 | 20/100 |
| Medium | 27/38 | 39/62 | 66/100 |
| Low | 9/38 | 5/62 | 14/100 |
| Column total | 38 | 62 | 100 |
| Income level \ Own a smartphone | No | Yes | Row total |
|---|---|---|---|
| High | 5.26% | 29.03% | 20.00% |
| Medium | 71.05% | 62.90% | 66.00% |
| Low | 23.68% | 8.06% | 14.00% |
| Column total | 38 | 62 | 100 |
Among those with no smartphone, 5.26% are High income; among owners, 29.03% are High income.
2.3Going deeper
- Each row (or column) adds to 100%. In a row-percentage table every row sums to 100% (22.73% + 77.27%). In a column-percentage table every column does (41.67% + 58.33%). Use this as the check.
- Pick the denominator from the question. “Of the females, how many own one?” divides by the females (row). “Of the owners, how many are female?” divides by the owners (column). The word after “of the” tells you what to divide by.
- The two are not the same number. 77.27% of females own a smartphone, but 44.74% of owners are female. Mixing them up reverses the meaning.
- Compare with the overall split. The overall row (24.00% No, 76.00% Yes) is the baseline each group is compared against in the next chapter.
- State in words what you divided by before computing.
- Check that each row (or column) of percentages adds to 100%.
- Dividing by the grand total instead of the row or column total.
- Using row percentages when the question is about columns.
Is There an Association?
To decide whether two categorical variables are associated, we use the row relative frequencies and the column relative frequencies of the previous chapter.
- The row relative frequencies (or the column relative frequencies) are the same for all rows (columns).
- The row relative frequencies (or the column relative frequencies) are different for some rows (some columns).
3.1Lecture examples
Example 7: gender and smartphone, associated?
- Row relative frequencies. Female: 22.73% No, 77.27% Yes. Male: 25.00% No, 75.00% Yes. The two rows are nearly identical, and both are close to the overall split of 24.00% and 76.00%.
- Column relative frequencies. Among non-owners 41.67% are female and 58.33% male; among owners 44.74% female and 55.26% male. These are close to the overall 44.00% and 56.00%.
- Conclusion. Gender and smartphone ownership are not associated: knowing a student's gender tells us almost nothing about whether the student owns a smartphone.
Example 8: income and smartphone, associated?
- Row relative frequencies. High: 10.00% No, 90.00% Yes. Medium: 40.91% No, 59.09% Yes. Low: 64.29% No, 35.71% Yes. The rows are clearly different from one another.
- Column relative frequencies. Among owners 29.03% are High, 62.90% Medium and 8.06% Low; among non-owners the split is 5.26%, 71.05% and 23.68%. The columns also differ.
- Conclusion. Income and smartphone ownership are associated. The higher the income level, the larger the share who own a smartphone.
The dashed line is the overall share of owners. If the variables were not associated, every bar would reach about the same height, near that line (left chart, gender). In the right chart (income) the bars are far apart, so knowing the income level changes what we expect about ownership.
3.2Going deeper
- Two checks, one answer. Rows and columns tell the same story. If the row percentages are the same across rows, the column percentages are the same across columns too, so one of the two is enough.
- “Close enough” is a judgement. 77.27% against 75.00% is a gap of about 2 points, small next to the 55-point gap between High (90%) and Low (35.71%). The lecture gives no numerical cut-off; compare the size of the gaps.
- Association is not cause. Income and smartphone ownership go together, but the table alone does not say that income causes ownership. This remark is an extra, not from the lecture.
- Ordinal order helps. Because income is ordinal we can say “the higher the income, the larger the share”, a direction. For a nominal variable we can only say the groups differ.
- Compare each group with the overall row.
- Say “associated” only when some rows (or columns) clearly differ.
- Quote two or three percentages as evidence in your conclusion.
- Comparing raw counts instead of relative frequencies.
- Calling a 2-point difference an association.
Stacked Bar Charts
4.1Making one in Google Sheets
- Select the data you want to include in the contingency table.
- Click Insert, then Chart, and choose the stacked bar option.
4.2Lecture examples
Example 9: gender and smartphone
Draw the stacked bar chart and the 100% stacked bar chart for the gender and smartphone data.
- Stacked bars of counts. One bar per gender. The bar height is the number of students (44 and 56); it is split into No (10, 14) and Yes (34, 42).
- 100% stacked bars. Rescale every bar to the same height, 100%, so each segment is the row percentage from the table.
- Read the chart. The Yes segment is almost the same fraction of both 100% bars, which matches the earlier conclusion that gender and ownership are not associated.
Example 10: income and smartphone
Draw the stacked bar chart and the 100% stacked bar chart for the income and smartphone data.
- Stacked bars of counts. One bar per income level (14, 66, 20 participants), split into No and Yes.
- 100% stacked bars. Each bar rescaled to 100%.
- Read the chart. The Yes segment grows from 35.71% (Low) to 59.09% (Medium) to 90.00% (High), so the bars differ: income and ownership are associated.
A 100% stacked bar chart is useful for part-to-whole relationships: every bar has the same height, so only the split can differ. Equal splits mean no association; changing splits mean association. The ordinary stacked chart shows the sizes of the groups as well, which the 100% chart hides.
4.3Going deeper: which chart to use
| Stacked bars (counts) | 100% stacked bars | |
|---|---|---|
| Bar height | Number of cases in the group | Always 100% |
| Segments | Counts | Row percentages |
| Shows group sizes | Yes | No |
| Best for | Seeing how big each group is | Comparing the splits to judge association |
- Use the 100% chart when the question is about association.
- Keep the segments in the same order and colour in every bar.
- Comparing segment heights across bars of different sizes in the count chart.
- Forgetting the legend that says which colour is Yes.
Scatter Plots
5.1Which variable goes on which axis
- The variable which explains: it goes on the horizontal axis.
- The variable you would like to explain: it goes on the vertical axis.
5.2Lecture examples
Example 11: age and height of children
The ages and heights of five children are given. Draw the scatter plot.
| Age (years) | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Height (cm) | 75 | 85 | 94 | 101 | 108 |
- Age explains height, so age is the explanatory variable (x) and height the response variable (y).
- Plot the five points (1, 75), (2, 85), (3, 94), (4, 101), (5, 108).
- The points rise from left to right: taller heights go with greater ages.
Example 12: prices of homes
A real estate agent collected the prices of different sizes of homes. He wanted to see the relationship between the price of a home and its size; in particular, whether prices increase linearly with size or in some other way. He collected data on 15 homes: the size in 1000 square feet and the price in lakhs of rupees.
| Home | Size (1000 sq ft) | Price (INR lakhs) |
|---|---|---|
| 1 | 0.8 | 68 |
| 2 | 1 | 81 |
| 3 | 1.1 | 72 |
| 4 | 1.3 | 91 |
| 5 | 1.6 | 87 |
| 6 | 1.8 | 56 |
| 7 | 2.3 | 83 |
| 8 | 2.3 | 112 |
| Home | Size (1000 sq ft) | Price (INR lakhs) |
|---|---|---|
| 9 | 2.5 | 93 |
| 10 | 2.5 | 98 |
| 11 | 2.7 | 136 |
| 12 | 3.1 | 109 |
| 13 | 3.1 | 122 |
| 14 | 3.2 | 159 |
| 15 | 3.4 | 170 |
- Size is the explanatory variable (x) and price the response variable (y).
- Plot the 15 pairs. Two homes of size 2.3 have prices 83 and 112, and two of size 2.5 have prices 93 and 98.
- The cloud climbs from lower left to upper right, so larger homes tend to cost more. The points are scattered around the climb, not exactly on a line.
5.3The visual test for association
5.4Scatter plot in Google Sheets
- Highlight the data you want to plot.
- Click Insert, then Chart, and choose the scatter chart.
- Under the X-axis tab, choose your explanatory variable.
- Under the Series tab, choose the response variable.
- Label the title of the chart and the axes appropriately.
5.5Going deeper
- Which variable is x? Ask: which one would I use to explain or predict the other? Age explains height; size explains price. Swapping them gives a different picture.
- Each point is one case. Home 7 is the point (2.3, 83); home 8 is (2.3, 112). Two points can share an x-value.
- Pattern, not exactness. The homes rise with size but do not lie on a line: the price also depends on other things. Chapters 6 to 9 give ways to describe and measure that.
- Axes need not start at zero. The home plot starts at 0.5 on x so the points fill the picture; check the axis labels before reading.
- Write “x = ..., y = ...” before plotting.
- Label both axes with the variable and its unit.
- Putting the response variable on the x-axis.
- Joining the points with lines (a scatter plot shows points only).
Describing Association
When describing the association between two variables in a scatter plot, four key questions need to be answered:
- Does the pattern trend up, down, or both?
- Does the pattern appear to be linear or does it curve?
- Are the points tightly clustered along the pattern?
- Did you find something unexpected?
6.1Direction
An upward pattern means larger values of x go with larger values of y; a downward pattern means larger values of x go with smaller values of y.
6.2Curvature
Look at whether the points follow a straight line or bend. The lecture shows four plots: linear patterns trending up and down, and two that curve, one bending upward and one levelling off.
6.3Variation
6.4Outliers
Answer the four questions in order. Direction: which way does the pattern go? Curvature: does a straight line describe it, or does it bend? Variation: how tightly do the points hug the pattern? Outliers: is any point far from the rest? A good description of a scatter plot mentions all four.
6.5Going deeper: a sample description
For the 15 homes (Chapter 5): Direction: upward, larger homes tend to cost more. Curvature: roughly linear. Variation: moderate, the points are scattered around the climb. Outliers: none stands far from the cloud. (This is a reading of the plot, not a number from the lecture.)
- Use the four words in the answer: direction, curvature, variation, outliers.
- Say “linear” only if a straight line fits the cloud.
- Ignoring an outlier because it spoils the pattern.
- Calling a curved pattern linear.
Covariance
How do we measure the strength of the association between two variables? The lecture introduces two measures: covariance and correlation.
Covariance quantifies the strength of the linear association between two numerical variables.
7.1Deviations from the means
The lecture builds covariance from two columns of deviations: and .
Example 13: age and height, the deviations
| Age x (years) | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Height y (cm) | 75 | 85 | 94 | 101 | 108 |
- Means. and .
- Deviations. Subtract the means:
| X | Y | X − X̄ | Y − Ȳ |
|---|---|---|---|
| 1 | 75 | -2 | -17.6 |
| 2 | 85 | -1 | -7.6 |
| 3 | 94 | 0 | 1.4 |
| 4 | 101 | 1 | 8.4 |
| 5 | 108 | 2 | 15.4 |
Example 14: age and price of a car, the deviations
| Age x (years) | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Price y (INR lakhs) | 6 | 5 | 4 | 3 | 2 |
- Means. and .
- Deviations.
| X | Y | X − X̄ | Y − Ȳ |
|---|---|---|---|
| 1 | 6 | -2 | 2 |
| 2 | 5 | -1 | 1 |
| 3 | 4 | 0 | 0 |
| 4 | 3 | 1 | -1 |
| 5 | 2 | 2 | -2 |
7.2Multiplying the deviations
Example 15: age and height, products of deviations
Step 3. Multiply for each person and add the products.
| X | Y | X − X̄ | Y − Ȳ | (X − X̄)(Y − Ȳ) |
|---|---|---|---|---|
| 1 | 75 | -2 | -17.6 | 35.2 |
| 2 | 85 | -1 | -7.6 | 7.6 |
| 3 | 94 | 0 | 1.4 | 0 |
| 4 | 101 | 1 | 8.4 | 8.4 |
| 5 | 108 | 2 | 15.4 | 30.8 |
| Sum | 82 |
Every product is zero or positive: when age is above average so is height, and when age is below average so is height.
Example 16: age and price of a car, products of deviations
| X | Y | X − X̄ | Y − Ȳ | (X − X̄)(Y − Ȳ) |
|---|---|---|---|---|
| 1 | 6 | -2 | 2 | -4 |
| 2 | 5 | -1 | 1 | -1 |
| 3 | 4 | 0 | 0 | 0 |
| 4 | 3 | 1 | -1 | -1 |
| 5 | 2 | 2 | -2 | -4 |
| Sum | -10 |
Every product is zero or negative: when age is above average the price is below average, and the other way round.
- When large (small) values of x tend to go with large (small) values of y, the signs of the deviations and tend to be the same.
- When large (small) values of x tend to go with small (large) values of y, the signs of the deviations tend to be different.
The dashed lines are the two means and cut the plane into four quadrants. A point to the right of the vertical line has a positive x deviation, a point above the horizontal line a positive y deviation. In the green quadrants the two signs agree, so the product is positive; in the red quadrants they disagree, so it is negative. Adding the products therefore adds up how many points (and how far) lie in each kind of quadrant.
7.3Definition of covariance
Example 17: covariance of age and height
- From Section 7.2 the sum of the products is 82.
- There are 5 observations.
Example 18: covariance of age and price of a car
- The sum of the products is -10.
- There are 5 observations.
7.4Going deeper
- The sign tells the direction. Positive covariance (16.4, 20.5): upward pattern. Negative (-2, -2.5): downward pattern. Near zero: no linear pattern.
- The size depends on units. Measuring height in metres instead of cm would divide the covariance by 100 without changing the pattern. So 20.5 versus -2.5 cannot be compared directly. This is an extra remark that follows from the definition.
- Same recipe as variance. The variance divides by ; the covariance replaces one of the squares by the other variable's deviation. So the covariance of x with itself is its variance (extra remark).
- Population or sample? Use for the whole population and for a sample, as for the variance.
- Build the table: x, y, deviations, products; then add the products.
- Check that each deviation column adds to 0.
- Keep the sign of the sum: it is the direction.
- Subtracting the mean after multiplying.
- Dividing a sample covariance by n instead of n - 1.
- Quoting a covariance without saying it has units.
Correlation
A more easily interpreted measure of linear association between two numerical variables is correlation. It is derived from covariance.
- The units of the standard deviations cancel out the units of the covariance, so has no units.
- It can be shown that the correlation always lies between and .
8.1Lecture examples
Example 19: correlation of age and height
- Square the deviations and add them:
| X | Y | X − X̄ | Y − Ȳ | (X − X̄)² | (Y − Ȳ)² | (X − X̄)(Y − Ȳ) |
|---|---|---|---|---|---|---|
| 1 | 75 | -2 | -17.6 | 4 | 309.76 | 35.2 |
| 2 | 85 | -1 | -7.6 | 1 | 57.76 | 7.6 |
| 3 | 94 | 0 | 1.4 | 0 | 1.96 | 0 |
| 4 | 101 | 1 | 8.4 | 1 | 70.56 | 8.4 |
| 5 | 108 | 2 | 15.4 | 4 | 237.16 | 30.8 |
| Sum | 10 | 677.2 | 82 |
- Standard deviations. and .
- Correlation.
Example 20: correlation of age and price of a car
- Square the deviations and add them:
| X | Y | X − X̄ | Y − Ȳ | (X − X̄)² | (Y − Ȳ)² | (X − X̄)(Y − Ȳ) |
|---|---|---|---|---|---|---|
| 1 | 6 | -2 | 2 | 4 | 4 | -4 |
| 2 | 5 | -1 | 1 | 1 | 1 | -1 |
| 3 | 4 | 0 | 0 | 0 | 0 | 0 |
| 4 | 3 | 1 | -1 | 1 | 1 | -1 |
| 5 | 2 | 2 | -2 | 4 | 4 | -4 |
| Sum | 10 | 10 | -10 |
- Standard deviations. .
- Correlation.
The green half of the scale is positive correlation (large with large) and the red half is negative correlation (large with small). Age and height give 0.9964, almost +1: very strong upward linear association. Age and the price of a car give exactly -1: a perfect downward line, since the five points lie exactly on a straight line going down.
8.2Correlation in Google Sheets
The function CORREL(series1, series2) returns the value of the correlation. If the x-variable (series1) is in cells A2:A6 and the y-variable (series2) in cells B2:B6, then CORREL(A2:A6,B2:B6) returns the Pearson correlation coefficient.
8.3Going deeper: how to read r
| Value of r | Meaning |
|---|---|
| +1 | perfect upward line |
| close to +1 (0.9964) | very strong upward linear association |
| 0 | no linear association |
| close to -1 | strong downward linear association |
| -1 | perfect downward line (age and price of the 5 cars) |
- Sign and size. The sign gives the direction; the distance from 0 gives the strength of the linear association.
- Only linear. A curved pattern can have near 0 and still be a strong association. That is why Chapter 6 asks about curvature before computing anything.
- Units do not matter. Because has no units, measuring height in metres or in cm gives the same (extra remark).
- Order does not matter. The formula is symmetric in x and y, so is the same if you swap them (extra remark).
- Never beyond . If you get 1.0016 or -1.2, there is a calculation or rounding mistake.
- Compute the sum of products and the two sums of squares; then .
- Round only at the very end.
- Rounding the standard deviations first (0.9973 instead of 0.9964).
- Reading r = 0 as “no association at all”.
Fitting a Line
The strength of linear association was measured with covariance and correlation. The linear association can also be described with the equation of a line. This chapter summarises a scatter plot with a line and introduces .
9.1Equation of the line in Google Sheets
- Open the scatter plot.
- Under the Customize tab, click on Series.
- Click on Trendline.
- Under the Label tab, choose Use equation and click the Show R² button.
9.2Lecture examples
Example 21: size versus price of homes
For the 15 homes of Chapter 5, find the equation of the line, and .
- Fit. Put size on the x-axis, price on the y-axis, and add the trendline with its equation and .
- Reading the line. The slope 30.5 says that the line rises by 30.5 lakh for every extra 1000 square feet.
- Check. and .
Example 22: age versus price of cars
For a data set of the age and price of cars, find the equation of the line, and .
- Fit the trendline in the scatter plot of price against age.
- Reading the line. The slope is negative: the line falls by 0.694 lakh for each extra year of age. The points lie close to the line.
- Check. .
Example 23: size versus price of homes, a weak line
For a second data set of the size and price of homes, find the equation of the line, and .
- Fit the trendline.
- Reading the line. The line is almost flat (a slope of only 7.77) and the points are scattered widely around it.
- Check. .
9.3All the lines side by side
| Example | Equation of the line | R² | r | Tightness of points |
|---|---|---|---|---|
| Size and price (15 homes) | Price = 30.5 × Size + 36 | 0.647 | 0.804 | moderately close |
| Age and price of cars | Price = −0.694 × Age + 9.03 | 0.855 | −0.9247 | close to the line |
| Size and price (second set) | Price = 7.77 × Size + 130 | 0.022 | 0.149 | widely scattered |
The sign of matches the sign of the slope (positive 30.5 with +0.804, negative -0.694 with -0.9247, positive 7.77 with +0.149). The closer is to ±1, the larger and the more tightly the points hug the line; with the line explains almost nothing and is 0.022.
9.4Going deeper
- Slope and intercept. In Price = 30.5 × Size + 36 the slope 30.5 is the change in price for one more unit of size; the intercept 36 is the line's value at size 0 (here just where the line starts, not a real home).
- . For a fitted straight line, is the square of , which is the check used in every example. So is between 0 and 1, and the sign of comes from the slope.
- What says. 0.647 means roughly 65% of the ups and downs in price go along with size, and 0.022 means almost none do. This reading is the usual one, an extra to the lecture, which only says goes with the line.
- Do not predict far outside the data. The homes in the data are 0.8 to 3.4 thousand square feet; the line says little about a 10 000 square foot home (extra remark).
- Check: should equal , and the sign of should match the slope.
- Say the slope with units: lakh per 1000 square feet.
- Reading the slope as the correlation (30.5 is not r).
- Giving r a positive sign when the slope is negative.
Categorical and Numerical Variables
The last lecture asks how a categorical variable is associated with a numerical variable, when the categorical variable has just two categories (it is dichotomous).
10.1Lecture example: gender versus marks
Example 24: gender versus marks
A teacher wants to know whether female students performed better than male students in her class. She collected the marks (out of 100) of twenty students in the subject.
| # | Gender | Marks |
|---|---|---|
| 1 | F | 71 |
| 2 | F | 67 |
| 3 | F | 65 |
| 4 | M | 69 |
| 5 | M | 75 |
| 6 | M | 83 |
| 7 | F | 91 |
| 8 | F | 85 |
| 9 | F | 69 |
| 10 | F | 75 |
| # | Gender | Marks |
|---|---|---|
| 11 | M | 92 |
| 12 | F | 79 |
| 13 | M | 71 |
| 14 | M | 94 |
| 15 | F | 86 |
| 16 | F | 75 |
| 17 | F | 90 |
| 18 | M | 84 |
| 19 | F | 91 |
| 20 | M | 90 |
- Code the categorical variable as 0 and 1. The lecture's scatter plot puts the females at 0 and the males at 1 (the marks at 0 range from 65 to 91, the marks at 1 from 69 to 94).
- Plot marks (numerical) against the gender code.
- Group the same marks by gender to compare them.
Female marks: 12 values, sum 944, so the mean is . Male marks: 8 values, sum 658, mean .
The two vertical stripes of the coded plot overlap heavily: both groups reach from the late sixties to the nineties. In the grouped picture the two means (78.67 and 82.25) are only a few marks apart compared with the spread inside each group. So gender separates the marks only slightly.
10.2Point bi-serial correlation coefficient
Let be a numerical variable and a categorical variable with two categories (a dichotomous variable). To calculate the point bi-serial correlation between them:
- Group the data into two sets based on the value of the dichotomous variable ; assume the value of is either 0 or 1.
- Means. Calculate the mean values of the two groups: and for the groups with and .
- Proportions and spread. Let and be the proportions of observations with and , and the standard deviation of the numerical variable .
Example 25: point bi-serial correlation of gender and marks
- Groups. for the 12 females and for the 8 males (as in the lecture's plot).
- Group means. and .
- Proportions. and , so .
- Standard deviation of all 20 marks. The marks add up to 1602, so the mean is 80.1, and the sample standard deviation is 9.57.
- Substitute.
- Using the population standard deviation (9.33, dividing by 20) instead gives -0.19; either way the size is about 0.18 to 0.19.
- Interpret. The size 0.18 is small, so the association between gender and marks in this class is weak. The negative sign only reflects the coding: the group coded 1 (males) has the higher mean. Swapping the codes would flip the sign and leave the size unchanged.
10.3Going deeper
- The formula in words. is the gap between the two group means, measured in standard deviations of all the marks, and then shrunk by . A big gap compared with the spread gives a big .
- Which group is 0 is your choice. The sign changes, the size does not. Always say which group is coded 1 when you state the sign.
- It is a Pearson correlation. With a 0/1 variable, the ordinary correlation formula of Chapter 8 gives the same number (extra remark; the lecture gives only the formula above).
- Compare means first. The quickest check is the two group means: 78.67 against 82.25, only about 3.6 marks apart.
- State the coding (which group is 0 and which is 1).
- Use the same standard deviation (sample or population) throughout, and say which.
- Using the standard deviation of one group instead of all the marks.
- Reading the negative sign as “females do worse”.
Week 4 Summary Sheet
Everything in Week 4 on one sheet, for a last-minute revision.
Which tool for which pair of variables
| Variables | Summary table | Graph | Measure of association |
|---|---|---|---|
| Two categorical | Contingency table; row and column relative frequencies | Stacked and 100% stacked bar chart | Same relative frequencies in all rows (columns): no association |
| Two numerical | - | Scatter plot (x explanatory, y response) | Covariance, correlation r, fitted line and R² |
| Categorical (2 categories) and numerical | Group means of the two categories | Scatter plot against a 0 / 1 code | Point bi-serial correlation r_pb |
Formulas
Google Sheets steps
| Task | How |
|---|---|
| Contingency table | Data → Pivot table; variables in Rows and Columns; Values with COUNTA |
| Stacked bar chart | Select the table; Insert → Chart → stacked bar |
| Scatter plot | Insert → Chart → scatter; x = explanatory, series = response |
| Correlation | CORREL(series1, series2) |
| Equation of the line and R² | Customize → Series → Trendline → Use equation, Show R² |
Values computed in the lectures
| Example | Result |
|---|---|
| Gender and smartphone: row percentages | Female 22.73% / 77.27%; Male 25.00% / 75.00% (not associated) |
| Income and smartphone: row percentages | High 10.00% / 90.00%; Medium 40.91% / 59.09%; Low 64.29% / 35.71% (associated) |
| Age and height | Sum of products 82; covariance 16.4 (population), 20.5 (sample); r = 0.9964 |
| Age and price of a car | Sum of products -10; covariance -2 (population), -2.5 (sample); r = -1 |
| Size and price of homes | Price = 30.5 × Size + 36; R² = 0.647; r = 0.804 |
| Age and price of cars (second set) | Price = −0.694 × Age + 9.03; R² = 0.855; r = −0.9247 |
| Size and price (weak set) | Price = 7.77 × Size + 130; R² = 0.022; r = 0.149 |
| Gender and marks | means 78.67 and 82.25; r_pb ≈ -0.18 |
Topic by topic
- Contingency table; keep ordinal order.
- Row and column relative frequencies.
- Same percentages: no association.
- Stacked and 100% stacked bars.
- x explanatory, y response.
- Direction, curvature, variation, outliers.
- Cov: mean of (x - x̄)(y - ȳ); N or n - 1; has units.
- r = cov / (sx sy), between -1 and +1, no units.
- Sheets trendline: equation and R².
- Slope sign matches the sign of r.
- Two-category variable: code 0/1, group means, r_pb.
Row and column relative frequencies
Same row (column) percentages: no association
Stacked and 100% stacked bar charts
Describe: direction, curvature, variation, outliers
Population: divide by N; sample: divide by n - 1
Has units: units of x times units of y
Always between -1 and +1
CORREL(series1, series2)
Slope sign matches the sign of r
Point bi-serial correlation r_pb