BIOS 600 - Spring 2026
Exam Corrections due Thursday 4/16 at 11:59pm,but email me if you need extra time.
Lab 8 due this Friday at 11:59pm.
Some R demos, helpful project things
Interpreting categorical variables, plus releveling factors
Interactions & how to interpret interaction terms
Checking for multicollinearity
left_join():write.csv()write.csv()row.names = F so that R does not create an additional column of row numbers.Consider the model
\[Obesity_i = \beta_0 + \beta_1(HDI == Middle)_i + \]
\[ \beta_2(HDI == BottomTen)_i + \epsilon_i\]
The parameter interpretations are below.
\(\beta_0\) represents the expected obesity percentage for a state with 0 for the two dummy variables. That is, in the top ten HDI
\(\beta_1\) represents the expected difference in obesity percentage for a state in the middle HDI category, compared to the top ten
\(\beta_2\) represents the expected difference in obesity percentage for a state in the bottom ten HDI category, compared to the top ten
| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
| (Intercept) | 34.430 | 0.816 | 42.195 | 0 |
| HDIMiddle | -4.797 | 0.942 | -5.091 | 0 |
| HDITop ten | -8.080 | 1.154 | -7.002 | 0 |
Wait a minute! R automatically assigned the lowest level of HDI as the reference level. This is because R assigns factor levels to characters alphabetically, so HDIBottom Ten was before HDIMiddle and HDITop Ten.
Let’s see how we can change the reference level. But before doing that…
Writing out the model
This reorders the factor levels so that “Top ten” becomes the first (reference) level.
Now, when we run the regression, the model will automatically use Top ten as the baseline.
| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
| (Intercept) | 26.350 | 0.816 | 32.293 | 0.000 |
| HDI_relevelBottom ten | 8.080 | 1.154 | 7.002 | 0.000 |
| HDI_relevelMiddle | 3.283 | 0.942 | 3.485 | 0.001 |
Interpretation and Model
Sometimes, the relationship between one predictor and the outcome depends on the value of another predictor variable. For example, the effect of exercise % on obesity may be different for smokers vs. non-smokers. To model such a relationship, we create an interaction term.
This is created simply by multiplying two predictors \(x_1\) and \(x_2\) to create a new predictor, \(x_1x_2\). When interaction terms are in a model, interpretations can become tricky.
Let’s consider a model with main effects of exercise and smoking and an interaction term between them:
\[Obesity_i = \beta_0 + \beta_1 Exercise + \beta_2 Smoking\]
\[+ \beta_3 (Exercise_i \cdot Smoking_i) + \epsilon_i\]
What is the expected change in obesity % given a one percentage point increase in exercise %?
| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
| (Intercept) | 38.577 | 13.867 | 2.782 | 0.008 |
| Exercise | -0.358 | 0.270 | -1.325 | 0.192 |
| Smoking | 0.408 | 0.729 | 0.559 | 0.579 |
| Exercise:Smoking | 0.003 | 0.015 | 0.186 | 0.853 |
Interpretation: If exercise increases by 1 percentage point, the expected change in obesity % depends on smoking %, and is: -.358 + .003(Smoking%)
So,
When Smoking = 0%, the slope of Exercise is -0.358 (exercise is assoc. w/ lower obesity).
When Smoking = 50%, slope = -0.358 + 0.003(50) = -0.208 (still negative association, but smaller in magnitude).
However: The interaction term is not significant. So we should remove the interaction term and just use the model with main effects only.
For a given exercise (ex) and smoking (sm) percentage, our predicted obesity (ob) percentage is
\[\hat {ob}_i = \hat \beta_0 + \hat \beta_1 ex_i + \hat \beta_2 sm_i + \hat \beta_3 ex_i sm_i\]
and for a state at the same smoking % but 1 percentage point higher in exercise %, the predicted obesity percentage is
\[\hat {ob}_{i'} = \hat \beta_0 + \hat \beta_1 (ex_i + 1) + \hat \beta_2 sm_i + \hat \beta_3 (ex_i + 1) sm_i\]
\[ = \hat \beta_0 + \hat \beta_1 ex_i + \hat \beta_1 + \hat \beta_2 sm_i + \hat \beta_3 ex_i sm_i + \hat \beta_3 sm_i\]
Subtracting, we have \(\hat ob_{i'} - \hat ob_i= \hat \beta_1 + \hat \beta_3 sm_i\), which is the expected change in obesity % for a 1% change in exercise %.
Takeaway: In the interactions model, the relationship of exercise and obesity depends on the level of smoking in that state.
Quick review
Take a few minutes to review the math in small groups.
Luckily, interpretation of interaction terms with categorical predictors is easier than with continuous predictors.
Since categorical predictors are based on dummy variables, they can only take on the values of 0 or 1 in the model.
Again, an interaction effect implies that the regression coefficient for an explanatory variable would change depending on the value of another predictor (for instance, the relationship between exercise and obesity might depend on whether a state is in High, Medium, or Low HDI groups).
One common problem in multiple regression is multicollinearity, which occurs when multiple highly correlated variables are used as predictors.
Collinearity simply refers to two variables behind highly correlated.
In this case, the model can become unstable (often seen as standard errors that get huge and lead to huge confidence interval estimates), and it can be difficult to assess the relationships of the predictors.
If nothing is significant when you “expect” something to be, we have some clues:
Individual predictors are significant in simple linear regression models,
but standard errors and interval estimates are huge,
and the overall F test is significant
A significant overall F test with no significant individual variable test is a typical sign of collinearity. We can check out the correlations among the three predictors.
The common way to check for multicollinearity is through VIF values. VIF values indicate the extent of multicollinearity.
A VIF value of 1 suggests no correlation with other predictors.
Values between 1 and 5 generally indicate moderate correlation.
Values above 5 (or sometimes 10, depending on the context) are often considered problematic, indicating severe multicollinearity and potential issues with coefficient stability and interpretation.
Exercise Smoking Over65
1.420894 1.541720 1.123272
Some R demos: merging, dropping NA, write.csv(), re-leveling a factor
Interactions, interpretations, and the math behind it all
Collinearity
