Before today, we discussed methods for comparing continuous outcomes across two or more groups.
We now will begin exploring the relationships between two continuous variables.
We will first focus on data visualization and the corresponding correlation.
The we will quantify the relationship using regression analysis.
Scatterplot or scatter diagram:
Each individual in the dataset is represented by a point on the scatterplot.
The explanatory variable is on the x-axis and the response variable is on the y-axis.
It is super important for us to plot the data!
Negative relationship: As x increases, y decreases.
Positive relationship: As x increases, y increases.
No relationship: As x increases, y stays the same.
Non-linear relationship: The relationship between x and y is not linear.
ggplot() function from library(tidyverse) (or library(ggplot2)).Rainbow Dash wants to know whether more flight practice tends to go with a higher score on the Wonderbolts obstacle course.
Our observational unit is a pony. For each pony we have:
practice_hours: hours spent practicingcourse_score: score on the course, from 0 to 100Is there anything unusual?
What kind of relationship is this?
Creating the scatterplot allows us to visualize a potential relationship.
Now, let’s discuss quantifying that relationship.
Initial quantification: correlation.
Further quantification: regression.
Pearson’s Correlation: A unitless measure of the strength and direction of the linear relationship between two quantitative variables.
\rho represents the population correlation coefficient.
r represents the sample correlation coefficient.
Correlation is bounded to [-1, 1].
r=-1 represents perfect negative correlation.
r=1 represents perfect positive correlation.
r=0 represents no correlation.
r = \frac{\sum_{i=1}^n \left( \frac{x_i - \bar{x}}{s_x} \right)\left( \frac{y_i - \bar{y}}{s_y} \right)}{n-1}
We will use the correlation() function from library(ssstats) to examine correlation.
For a single pairwise correlation,
| var1 | var2 | correlation | test_statistic | p_value |
|---|---|---|---|---|
| course_score | practice_hours | 0.995 | 40.7 | < 0.001 |
In Ponyville, Pinkie Pie is curious about how Gummy’s snack satisfaction (0 to 100) relates to the duration of chew time (in seconds) he spends on crunchy treats. She suspects that Gummy enjoys snacks more the longer he chews them.
Pinkie Pie records both the chew time (chew_time) and Gummy’s satisfaction level (satisfaction) of the last 300 snacks given to Gummy (gummy_data).
| chew_time | snack_crunch | nap_time | satisfaction |
|---|---|---|---|
| 38.8 | 67.6 | 27.9 | 49 |
| 13.4 | 63.0 | 16.5 | 32 |
| 8.7 | 53.4 | 16.0 | 44 |
We can determine if the correlation is significantly different from 0 (i.e., a relationship exists)
Hypotheses:
Test Statistic and p-Value
This is default output in our correlation() function.
Pinkie Pie records both the chew time (chew_time) and Gummy’s satisfaction level (satisfaction) of the last 300 snacks given to Gummy (gummy_data).
Looking at correlation results for this specific relationship,
The assumption for Pearson’s correlation is that both x and y are normally distributed.
We will use the correlation_qq() function from library(ssstats) to examine the normality of x and y.
What do we do when we do not meet the normality assumption?
Spearman’s Correlation: A unitless measure of the strength and direction of the monotone relationship between two variables.
Spearman’s correlation only assumes that the data is ordinal.
It does not work for nominal data!
Spearman’s correlation is interpreted the same as Pearson’s correlation.
To find Spearman’s correlation, the following algorithm is followed:
We will use the correlation() function from library(ssstats) to examine correlation.
For a single pairwise correlation,
We can determine if the correlation is significantly different from 0 (i.e., a relationship exists)
Hypotheses:
Test Statistic and p-Value
This is default output in our correlation() function.
Pinkie Pie records both the chew time (chew_time) and Gummy’s satisfaction level (satisfaction) of the last 300 snacks given to Gummy (gummy_data).
Looking at Spearman’s correlation results for this specific relationship,
| var1 | var2 | correlation | test_statistic | p_value |
|---|---|---|---|---|
| chew_time | satisfaction | 0.864 | 29.561 | < 0.001 |
| var1 | var2 | correlation | test_statistic | p_value |
|---|---|---|---|---|
| chew_time | satisfaction | 0.868 | 594784.4 | < 0.001 |
Now we will discuss a different way of representing/quantifying the relationship.
Using simple linear regression, we will model y (the outcome) as a function of x (the predictor).
y = \beta_0 + \beta_1 x + \varepsilon
\beta_0 is the y-intercept.
\beta_1 is the slope describing the relationship between x and y.
\varepsilon (estimated by e) is the error term; remember, from ANOVA (😱):
\varepsilon \overset{\text{iid}}{\sim} N(0, \sigma^2)
y = \beta_0 + \beta_1 x + \varepsilon
\hat{y} = \hat{\beta}_0 + \hat{\beta}_1 x + e
\hat{y} estimates y.
\hat{\beta}_0 estimates \beta_0.
\hat{\beta}_1 estimates \beta_1.
e estimates \varepsilon.
\hat{\beta}_1 = \frac{\sum_{i=1}^n x_i y_i - \frac{\sum_{i=1}^n x_i \sum_{i=1}^n y_i}{n}}{\sum_{i=1}^n x_i^2 - \frac{\left(\sum_{i=1}^n x_i\right)^2}{n}} = r \frac{s_y}{s_x}
\hat{\beta}_0 = \bar{y} - \hat{\beta}_1 \bar{x} where
We will use the glm() function from base R to construct our regression model.
In the case of simple linear regression,
tidy() from broom.| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
| (Intercept) | 40.437594 | 0.6984833 | 57.89343 | 0 |
| practice_hours | 4.419549 | 0.1085893 | 40.69967 | 0 |
\hat{\text{score}} = 40.4 + 4.4 \text{ hours}
| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
| (Intercept) | 21.0922141 | 1.0877450 | 19.39077 | 0 |
| chew_time | 0.8935781 | 0.0302281 | 29.56115 | 0 |
\hat{\text{satisfaction}} = 21.1 + 0.9 \text{ chew time}
Rainbow Dash wants to know whether more flight practice tends to go with a higher score on the Wonderbolts obstacle course.
Recall the regression line we constructed,
\hat{\text{score}} = 40.4 + 4.4 \text{ hours}
Before considering practice time, we expect the average obstacle course score to be 40.4.
For every hour increase in practice time, we expect the obstacle course score to increase by 4.4 points.
Pinkie Pie suspects that Gummy enjoys snacks more the longer he chews them.
Recall the regression line we constructed,
\hat{\text{satisfaction}} = 21.1 + 0.90 \text{ chew time}
Before considering chew time, Gummy’s base level of satisfaction is 21.1.
For a 1 minute increase in chewing time, the satisfaction Gummy feels increases by 0.9 points.
Like all statistics, we can put a confidence interval on \beta_i.
\hat{\beta}_i \pm t_{\alpha/2, n-k-1} \text{ SE}_{\hat{\beta}_i}
tidy(),| term | estimate | std.error | statistic | p.value | conf.low | conf.high |
|---|---|---|---|---|---|---|
| (Intercept) | 40.437594 | 0.6984833 | 57.89343 | 0 | 39.068592 | 41.80660 |
| practice_hours | 4.419549 | 0.1085893 | 40.69967 | 0 | 4.206718 | 4.63238 |
The 95% CI for practice hours is (4.21, 4.63)
| term | estimate | std.error | statistic | p.value | conf.low | conf.high |
|---|---|---|---|---|---|---|
| (Intercept) | 21.0922141 | 1.0877450 | 19.39077 | 0 | 18.960273 | 23.2241551 |
| chew_time | 0.8935781 | 0.0302281 | 29.56115 | 0 | 0.834332 | 0.9528241 |
The 95% CI for chew time is (0.83, 0.95)
Hypotheses
Test Statistic
p-Value
This is default output in tidy().
When do we use either approach?
Remember that correlation tells us the strength of the relationship.
Regression allows us to quantify that relationship on the original scale.
What is the difference between the tests?
Next time:
Multiple regression with more than one predictor
Categorical predictors
Assumptions on the linear model
Visualizations that help explain model results