# Lecture 24 – Correlation¶

## DSC 10, Fall 2022¶

### Announcements¶

• Homework 7 is due tomorrow at 11:59pm.
• Lab 8 is due on Saturday 11/26 at 11:59pm.
• The Final Project is due on Tuesday 11/29 at 11:59pm.
• Suraj's lecture section (C00) is not meeting on Wednesday 11/23 or Monday 11/28.
• Attend and/or watch the podcasts of any other lecture section on those days.
• Try to attend the earlier sections, since they have more space.

### Agenda¶

• Recap: Statistical inference.
• Association.
• Correlation.
• Regression.

## Recap: Statistical inference¶

### Four big ideas in statistical inference¶

Every statistical test and simulation we've run in the second half of the class is related to one of the following four ideas. To solidify your understanding of what we've done, it's a good idea to review past lectures and assignments and see how what we did in each section relates to one of these four ideas.

• To test whether a sample came from a known population distribution, use "standard" hypothesis testing.
• To test whether two samples came from the same unknown population distribution, use permutation testing.
• To estimate a population parameter given a single sample, construct a confidence interval using bootstrapping (for most statistics) or the CLT (for the sample mean).
• To test whether a population parameter is equal to a particular value, $x$, construct a confidence interval using bootstrapping (for most statistics) or the CLT (for the sample mean), and check whether $x$ is in the interval.

### Recent events¶

Questions to think about:

• Is the set of Twitter users that follow Elon Musk a random sample of all Twitter users?
• What does statistical significance mean in this context? How is this related to our "Choosing sample sizes" example from Lecture 23?

## Association¶

### Prediction¶

• Suppose we have a dataset with at least two numerical variables.
• We're interested in predicting one variable based on another:
• Given my education level, what is my income?
• Given my height, how tall will my kid be as an adult?
• Given my age, how many countries have I visited?
• To do this effectively, we need to first observe a pattern between the two numerical variables.
• To see if a pattern exists, we'll need to draw a scatter plot.

### Association¶

• In Lecture 2, we said "association is another term for "any relation" or "link" 🔗."
• In this context, an association is any relationship or link between two variables in a scatter plot. Associations can be linear or non-linear.
• If two variables have a positive association ↗️, then as one variable increases, the other tends to increase.
• If two variables have a negative association ↘️, then as one variable increases, the other tends to decrease.
• As we saw in Lecture 2, association $\neq$ causation!
• However, association is enough to let us make predictions.

### 'acceleration' and 'price'¶

Is there an association between these two variables? If so, what kind?

### 'mpg' and 'price'¶

Is there an association between these two variables? If so, what kind?

Observations:

• There is an association – cars with better fuel economy tended to be cheaper.
• Why do we think that is? 🤔
• The association looks more curved than linear.
• It may roughly follow $y \approx \frac{1}{x}$.

### Linear changes in units¶

• A linear change in units doesn't change the shape of the plot, it only changes the scale of the plot.
• Linear change means adding or subtracting a constant, and multiplying or dividing by a constant.
• In other words, instead of plotting price in dollars and fuel economy in miles per gallon, we can plot price in Yen (🇯🇵) and fuel economy in kilometers per liter and the plot would look the same, just with different axes:

### Converting columns to standard units¶

• Recall: Suppose $x$ is a numerical variable, and $x_i$ is one value of that variable. To convert $x_i$ to standard units, $$x_{i \: \text{(su)}} = \frac{x_i - \text{mean of x}}{\text{SD of x}}$$
• Converting columns to standard units makes different scatter plots comparable, by putting the $x$ and $y$ axes on the same scale.
• Both axes measure the number of standard deviations above the mean.
• Converting columns to standard units doesn't change shape of the scatter plot, because the conversion is linear.

### Standard units for hybrid cars¶

For a given pair of variables:

• Which cars are average in both variables?
• Which cars are well above or well below average in both variables?

### 'acceleration' and 'price'¶

Which cars have 'acceleration's and 'price's that are more than 2 SDs above average?

### 'mpg' and 'price'¶

Which cars have close to average 'mpg's and close to average 'price's?

### Observation on associations in standard units¶

• If two variables are positively associated ↗️,
• their high, positive values in standard units are typically seen together, and
• their low, negative values are typically seen together as well.
• If two variables are negatively associated ↘️,
• high, positive values of one are typically coupled with low, negative values of the other.
• If two variables aren't associated, there should be no such pattern.

When there is a positive association, most data points fall in the lower left and upper right quadrants.

When there is a negative association, most data points fall in the upper left and lower right quadrants.

## Correlation¶

### Definition: Correlation coefficient¶

The correlation coefficient $r$ of two variables $x$ and $y$ is defined as the

• average value of the
• product of $x$ and $y$
• when both are measured in standard units.

If x and y are two Series or arrays,

r = (x_su * y_su).mean()


where x_su and y_su are x and y converted to standard units.

Let's calculate $r$ for 'acceleration' and 'price'.

Note that the correlation is positive, and most data points fall in the lower left and upper right quadrants!

Let's now calculate $r$ for 'mpg' and 'price'.

Note that the correlation is negative, and most data points fall in the upper left and lower right quadrants!

### The correlation coefficient, $r$¶

• $r$ measures how clustered points are around a straight line – it measures linear association.
• If two variables are correlated, it means they are linearly associated.
• $r$ is always between $-1$ and $1$.
• If $r = 1$, the scatter plot is a line of slope 1.
• If $r = -1$, the scatter plot is a line of slope -1.
• If $r = 0$, there is no linear association (uncorrelated).