Linear regression with outliers and categorical predictors
Lecture 16
Prof. Mine Çetinkaya-Rundel
Duke University STA 199 - Fall 2026
October 26, 2026
Warm-up
While you wait: Participate 📱💻
In the previous class, we used food-insecurity prevalence to predict mental-distress prevalence. Durham County had a residual of -2.60 percentage points.
What should progress on projects look like nowadays?
Schedule meetings (at least one this week and at least two next week)
This week:
Outline your presentation and start drafting slides and discussing who will present which slide (each team member must speak during the presentation)
Start drafting your writeup – you can structure it as the words you will say in your presentation (i.e., your script)
Next week: Make sure at least one meeting is dedicated to practicing your presentation and timing yourself
After next week: Write the reflection portion of your writeup
From last time
Dental visits and health
Application exercise
Go to your ae project in Positron.
If you haven’t yet done so, make sure all of your changes up to this point are committed and pushed, i.e., there’s nothing left in your source control pane.
If you haven’t yet done so, pull to get today’s application exercise file: ae-09-nc-places-model.qmd.
Work through the application exercise in class, and render, commit, and push your edits by the end of class.
Evaluating fit
Is linear model appropriate?
In the residuals vs. fitted (predicted) plot, we look for a horizontal band of points with no clear pattern.
Robeson County is an influential point with high-leverage.
Robeson is horizontally far from the center of the cloud.
It also falls far below the fitted relationship: its residual has a high magnitude compared to other counties in the data.
This combination pulls the right side of the fitted line downward – the slope of the model is 1.199 with all counties and 1.609 without Robeson County.
Working with outliers
Investigate why the observation is unusual:
Is it a data or measurement error?
Is it a genuine but exceptional case?
Does it reveal a subgroup or a missing variable?
Fit the model with and without the observation and compare the results.
Do not remove an outlier solely because it is unusual. Report and justify any decision to exclude data.
Categorical predictor
Development tiers
The North Carolina Department of Commerce ranks all 100 counties by economic well-being each year. The tiers are used in state programs that aim to encourage economic activity in less prosperous areas.
Tier 1: 40 most economically distressed counties
Tier 2: next 40 counties
Tier 3: 20 least economically distressed counties
The ranking combines four equally weighted measures: unemployment, median household income, 36-month population growth, and adjusted property tax base per capita.
A new question
How does estimated frequent mental-distress prevalence vary by county development tier?
How are the variables in this analysis different from those in our model from last time?
predictor: county development tier (categorical; Tier 1 is most economically distressed and Tier 3 is least economically distressed)
Visualize
Determine whether each of the following plot types would be an appropriate choice for visualizing the relationship between mental-distress prevalence and development tier.
Scatterplot ❌
Box plot ✅
Violin plot ✅
Density plot ✅
Bar plot ❌
Stacked bar plot ❌
Boxplot
Code
ggplot( nc_places,aes(x = development_tier, y = mental_distress)) +geom_boxplot() +scale_y_continuous(labels =label_percent(scale =1)) +labs(x ="Development tier",y ="Adults experiencing\nfrequent mental distress (%)",title ="Mental distress by county development tier" )
Violin plot
Code
ggplot( nc_places,aes(x = development_tier, y = mental_distress)) +geom_violin() +scale_y_continuous(labels =label_percent(scale =1)) +labs(x ="Development tier",y ="Adults experiencing\nfrequent mental distress (%)",title ="Mental distress by county development tier" )
The predicted average mental-distress prevalence among Tier 1 counties is 19.4%.
Model - predict
What is the predicted mental-distress prevalence for a Tier 1 county? What are the predicted values for Tier 2 and Tier 3 counties? Where have we seen these values before?
A categorical predictor is encoded using dummy variables. A predictor with \(k\) levels requires \(k - 1\) dummy variables.
The first level of the categorical variable is the baseline level. In a model with one categorical predictor, the intercept is the predicted value of the outcome for the baseline level (when all dummy variables equal 0).
Each slope coefficient describes the difference between the predicted value of the outcome for that level of the categorical variable compared to the baseline level.
Changing the baseline level
By default, R uses the first level of a categorical variable as the baseline level.
We can change the baseline level by reordering the levels of the categorical variable.
Participate 📱💻
What function goes in the blank to make Tier 3 the baseline level in the development_tier variable?