| Term | β (95% CI) | p-value |
|---|---|---|
| (Intercept) | 20.92 (18.77, 23.08) | <0.001 |
| chew_time | 0.90 (0.84, 0.96) | <0.001 |
We have discussed quantifying the relationship between two continuous variables.
Correlation
Simple linear regression
Recall that the correlation describes the strength and the direction of the relationship.
Pearson’s correlation: describes the linear relationship.
Spearman’s correlation: describes the monotone relationship.
Further, recall that correlation is unitless and bounded to [-1, 1].
Linear regression retains the units on x and y.
| Term | β (95% CI) | p-value |
|---|---|---|
| (Intercept) | 20.92 (18.77, 23.08) | <0.001 |
| chew_time | 0.90 (0.84, 0.96) | <0.001 |
\hat{\text{satisfaction}} = 20.92 + 0.90 \text{ chew\_time}
\hat{\text{satisfaction}} = 20.92 + 0.90 \text{ chew\_time}
What is the interpretation for the slope of time chewed?
What is the interpretation of the y-intercept?
y = \beta_0 + \beta_1 x + \varepsilon
y = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + ... + \beta_k x_k + \varepsilon
We will use the glm() function from base R to construct our regression model.
Multiple linear regression,
tidy() from broom.Pinkie Pie suspects that Gummy enjoys snacks more the longer he chews them. She also suspects that crunch factor plays a role in his satisfaction and now wants to incorporate this into the analysis.
Pinkie Pie records the chew time (chew_time), the crunch factor (snack_crunch) and Gummy’s satisfaction level (satisfaction) of the last 300 snacks given to Gummy (gummy_data).
Let’s now construct the corresponding multiple regression model.
| term | estimate | std.error | statistic | p.value | conf.low | conf.high |
|---|---|---|---|---|---|---|
| (Intercept) | 23.0169700 | 4.9286770 | 4.6700098 | 0.0000046 | 13.3569406 | 32.6769994 |
| chew_time | 0.9183209 | 0.0530165 | 17.3214114 | 0.0000000 | 0.8144105 | 1.0222314 |
| snack_crunch | -0.0406172 | 0.0932403 | -0.4356184 | 0.6634301 | -0.2233649 | 0.1421305 |
\hat{\text{satisfaction}} = 23.02 + 0.92 \text{ chew\_time} - 0.04 \text{ snack\_crunch}
\hat{\text{satisfaction}} = 23.02 + 0.92 \text{ chew\_time} - 0.04 \text{ snack\_crunch}
How do I report regression models in the real world?
In our example,
| Predictor | Estimate (95% CI) | p-Value |
|---|---|---|
| Time Chewed | 0.92 (0.81, 1.02) | < 0.001 |
| Crunch Factor | -0.04 (-0.22, 0.14) | 0.663 |
We can use the likelihood ratio test to test for a “significant regression” line.
Hypotheses
Test Statistic and p-Value
anova() function from base R to test for a significant model.Pinkie Pie suspects that Gummy enjoys snacks more the longer he chews them. She also suspects that crunch factor plays a role in his satisfaction and now wants to incorporate this into the analysis.
Let’s now determine if we have a significant regression line.
R^2 = \frac{\text{SS}_{\text{Reg}}}{\text{SS}_{\text{Tot}}}
Thus, R^2 is the proportion of variation explained by the model (i.e., predictor set).
R^2 \in [0, 1]
R^2 \to 0 indicates that the model fits “poorly.”
R^2 \to 1 indicates that the model fits “well.”
R^2 = 1 indicates that all points fall on the response surface.
Recall that the error term in ANOVA is the “catch all” …
The SSTot is constant for the outcome of interest.
As we add predictors to the model, we are necessarily increasing SSReg
We do not want to arbitrarily increase R^2, so we will use an adjusted version:
R^2_{\text{adj}} = 1 - \frac{\text{MS}_{\text{E}}}{\text{SS}_{\text{Tot}}/\text{df}_{\text{Tot}}}
r_squared() function from library(ssstats) to find the R^2 value.Pinkie Pie suspects that Gummy enjoys snacks more the longer he chews them. She also suspects that crunch factor plays a role in his satisfaction and now wants to incorporate this into the analysis.
Let’s now find the R^2 value for the model we constructed earlier.
y = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + ... \beta_k x_k + \varepsilon
In this model, \varepsilon is the residual error term.
Recall that the residual error (how far away the observation is from the response surface) is defined as
\varepsilon = y - \hat{y}
Note that
y is the observed value
\hat{y} is the predicted value
To ensure the validity of the least squares estimation, we have several assumptions:
Homogeneity of variance
Independence of observations
Linearity
Existence
Gaussian errors
Think: HILE+G
Homogeneity of variance (also called homoscedasticity) means that the variance of the residuals is constant across all levels of the predictors.
If the variance of the residuals changes at different levels of the predictors, we have heteroscedasticity.
We will check this assumption graphically using a scatterplot of residual vs. fitted values.
We do not want to see a pattern.
A “funnel” or “open fan” shape indicates heteroscedasticity.
A general “cloud” shape indicates homoscedasticity.
We also assume that our observations are independent.
Examples of independent data:
Randomly sampled individuals from a large population.
Measurements taken from different individuals with no inherent relationship.
Examples of dependent data:
Measurements taken from the same individual over time (repeated measures).
Measurements taken from individuals within the same group or cluster (e.g., students within the same classroom; people in the same family).
Spatial data where observations are collected from nearby locations.
We assume that the relationship between each predictor and the response is linear in the parameters (i.e., \beta_i).
In this course, all of the models we will construct are linear models.
The language gets tricky because we can have non-linear relationships in a linear model.
For example, we can include polynomial terms (e.g., x^2, x^3) or interaction terms (e.g., x_1 \times x_2) in a linear model.
As long as the model is linear in the parameters, it is considered a linear model.
The existence assumption means that the residuals have a finite mean and variance.
This assumption is almost always satisfied in practical applications.
This is not an assumption we check – we will see estimation errors when this assumption is violated.
Note! The normal distribution is also known as the Gaussian distribution.
For our inference to be valid, we must assume that the residuals are normally distributed.
We will check this assumption using a quantile-quantile (q-q) plot of the residuals.
We want to see points roughly along the line.
Large deviations from the line indicate non-normality.
We can also use a histogram to “back up” our decision from the q-q plot.
We want to see it roughly mound-shaped and symmetric.
Skewed or multi-modal histograms indicate non-normality.
\varepsilon \overset{\text{iid}}{\sim} N(0, \sigma^2)
This notation combines our checkable assumptions:
Residuals are independent and identically distributed (I)
Residuals follow a normal distribution (G), centered at 0 and with some constant variance, \sigma^2 (H).
We can look at a graph with all of these checks at once using the reg_check() function in the ssstats package.
\hat{\text{satisfaction}} = 23.02 + 0.92 \text{ chew\_time} - 0.04 \text{ snack\_crunch}
Note that we will plug in for all x except one, which will vary & is on the x-axis.
Let’s say we have 3 predictors: x1, x2, and x3.
If we want x1 on the x-axis, then
\text{variable on } x = \hat{\beta}_0 + \hat{\beta}_1 x_1 + \hat{\beta}_2 \text{median}(x_2) + \hat{\beta}_3 \text{median}(x_3)
| satisfaction | chew_on_x | snack_on_x |
|---|---|---|
| 50.70824 | 36.32793 | 51.06114 |
| 55.13960 | 57.84485 | 50.72415 |
| 58.10416 | 44.06215 | 51.18664 |
To visualize our model, we will overlay the regression line on a scatter plot of the data.
Here, we take the scatterplot code from last week and add a geom_line() for the regression line.
dataset_name %>% ggplot(aes(x = x_variable, y = y_variable)) + # specify x and y
geom_point(size = 2.5, color = "gray50") + # plot points
geom_line(aes(y = line_variable), size = 1, color = "black") + # plot line
labs(x = "x-axis label", # edit x-axis label
y = "y-axis label") + # edit y-axis label
theme_bw() # change theme of graphFor our example, we will create two visualizations: one with chew_time on the x-axis and one with snack_crunch on the x-axis.
The base code for the visualization is:
gummy_data %>% ggplot(aes(x = x_variable, y = satisfaction)) + # specify x and y
geom_point(size = 2.5, color = "gray50") + # plot points
geom_line(aes(y = line_variable), size = 1, color = "black") + # plot line
labs(x = "x-axis label", # edit x-axis label
y = "Gummy's Satisfaction") + # edit y-axis label
theme_bw() # change theme of graphgummy_data %>% ggplot(aes(x = chew_time, y = satisfaction)) + # specify x and y
geom_point(size = 2.5, color = "gray50") + # plot points
geom_line(aes(y = chew_on_x), size = 1, color = "black") + # plot line
labs(x = "Time Spent Chewing (min)", # edit x-axis label
y = "Gummy's Satisfaction") + # edit y-axis label
theme_bw() # change theme of graphgummy_data %>% ggplot(aes(x = snack_crunch, y = satisfaction)) + # specify x and y
geom_point(size = 2.5, color = "gray50") + # plot points
geom_line(aes(y = snack_on_x), size = 1, color = "black") + # plot line
labs(x = "Crunch Factor of Snack", # edit x-axis label
y = "Gummy's Satisfaction") + # edit y-axis label
theme_bw() # change theme of graphWhoa! It looks like there is not much of a relationship between crunch factor and satisfaction!
Remember that regression is looking at the relationship between y and xi while adjusting for all other predictors in the model.
The variability in satisfaction explained by snack_crunch is being adjusted for chew_time.
This is why we saw a non-significant slope for snack_crunch earlier.
This means that chew_time explains the variance in satisfaction, not snack_crunch.
| var1 | var2 | correlation | test_statistic | p_value |
|---|---|---|---|---|
| snack_crunch | chew_time | 0.817 | 822660 | < 0.001 |
With r_s = 0.82, we see that snack_crunch and chew_time are highly correlated.
This is just scratching the surface for multiple regression.
Other statistics courses go deeper into regression topics.
Categorical predictors.
Interaction terms.
How to handle non-continuous outcomes.
Advanced data visualization for models.