HW 5 due tonight and I have office hours 3:30 - 5 pm
Lab 5 in lab tomorrow:
You’ll work on a non-project task, primarily
But you’ll work with your project teams
And you’ll be asked to share plans for next steps for your project as well
Extra credit opportunity: Come to office hours AS A FULL TEAM with me or any of the TAs before the final project deadline, share your project progress, and get feedback on next steps. Each team that does this will get 1 extra credit point on the project.
Project
Getting your presentation on your project website:
Create your presentation wherever you want (PowerPoint, Google Slides, Keynote, etc.)
Export as a PDF
Rename the PDF as presentation.pdf (case sensitive)
Place the PDF in your project folder, overwriting the existing, placeholder presentation.pdf file
From last time: Categorical predictors
Mental-distress prevalence by county development tier
How does estimated frequent mental-distress prevalence vary by county development tier?
predictor: county development tier (categorical; Tier 1 is most economically distressed and Tier 3 is least economically distressed)
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")
Density plot
Code
ggplot(nc_places,aes( x =mental_distress, color =development_tier, fill =development_tier))+geom_density(alpha =0.5)+scale_x_continuous(labels =label_percent(scale =1))+scale_color_viridis_d(end =0.85)+scale_fill_viridis_d(end =0.85)+labs( x ="Adults experiencing frequent mental distress (%)", y ="Density", color ="Development\ntier", fill ="Development\ntier", title ="Mental distress by county development tier")
Multiple geoms
Code
ggplot(nc_places,aes(x =development_tier, y =mental_distress))+geom_boxplot(outlier.shape =NA)+geom_jitter(width =0.12, height =0, size =2, alpha =0.5)+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")
Summarize
Calculate the average mental-distress prevalence for counties in each development tier. How do the averages compare across tiers?
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?
md_tier_lone_aug<-augment(md_tier_lone_fit, new_data =nc_places)ggplot(md_tier_lone_aug,aes(x =loneliness, y =mental_distress, fill =development_tier))+geom_line(aes(y =.pred, color =development_tier), linewidth =1)+geom_point( alpha =0.7, size =3, shape ="circle filled", color ="#674574")+scale_x_continuous(labels =label_percent(scale =1))+scale_y_continuous(labels =label_percent(scale =1))+scale_color_viridis_d(end =0.85, guide ="none")+scale_fill_viridis_d(end =0.85)+labs( x ="Adults experiencing loneliness (%)", y ="Adults experiencing\nfrequent mental distress (%)", fill ="Development tier", title ="Main effects models", subtitle ="Linear model for each tier: same slope, different intercepts")+theme( legend.position ="inside", legend.position.inside =c(0.8, 0.22))
Loneliness: For each additional percentage point in loneliness prevalence, the model predicts mental distress prevalence to be higher, on average, by 0.381 percentage points, all else held constant (i.e., for counties in the same development tier).
Tier 2: For Tier 2 counties, the model predicts mental distress prevalence to be lower, on average, by 0.38 percentage points than for Tier 1 counties, all else held constant (i.e., for counties with the same loneliness prevalence).
Tier 3: For Tier 3 counties, the model predicts mental distress prevalence to be lower, on average, by 1.41 percentage points than for Tier 1 counties, all else held constant (i.e., for counties with the same loneliness prevalence).
Intercept: For Tier 1 counties with 0% loneliness prevalence, the model predicts mental distress prevalence to be 6.04%. However, 0% loneliness is outside the range of the observed data, so the intercept is not meaningful in context.
\(R^2\): Variability explained
How much of the variability in mental-distress prevalence is explained by…
tier alone:
glance(md_tier_fit)|>select(r.squared)
# A tibble: 1 × 1
r.squared
<dbl>
1 0.554
The model with tier alone as predictor explains approximately 55.4% of the variability in mental-distress prevalence among North Carolina counties.
tier + loneliness prevalence:
glance(md_tier_lone_fit)|>select(r.squared)
# A tibble: 1 × 1
r.squared
<dbl>
1 0.691
The model with tier and loneliness prevalence as predictors explains approximately 69.1% of the variability in mental-distress prevalence among North Carolina counties.
What does this say about which model is better for predicting mental distress prevalence?
Adjusted \(R^2\): Fit and complexity
\(R^2\):
measures the proportion of variability explained by the model
increases as more predictors are added to the model, even if the new predictors are not meaningful
Adjusted \(R^2\):
measures the proportion of variability explained by the model, with a penalty for the number of predictor coefficients in the model
penalizes the model for adding unnecessary predictors, therefore does not automatically increase when another predictor is added
Adjusted \(R^2\): Fit and complexity
tier alone:
glance(md_tier_fit)|>select(adj.r.squared)
# A tibble: 1 × 1
adj.r.squared
<dbl>
1 0.545
tier + loneliness prevalence:
glance(md_tier_lone_fit)|>select(adj.r.squared)
# A tibble: 1 × 1
adj.r.squared
<dbl>
1 0.681
What does this say about which model is better for predicting mental distress prevalence?
Other explanations?
Current model: Mental distress prevalence is higher by the same amount for each additional percentage point in loneliness prevalence, regardless of development tier.
Could there be a different reality?
Code
md_tier_lone_aug<-augment(md_tier_lone_fit, new_data =nc_places)ggplot(md_tier_lone_aug,aes(x =loneliness, y =mental_distress, fill =development_tier))+geom_line(aes(y =.pred, color =development_tier), linewidth =1)+geom_point( alpha =0.7, size =3, shape ="circle filled", color ="#674574")+scale_x_continuous(labels =label_percent(scale =1))+scale_y_continuous(labels =label_percent(scale =1))+scale_color_viridis_d(end =0.85, guide ="none")+scale_fill_viridis_d(end =0.85)+labs( x ="Adults experiencing loneliness (%)", y ="Adults experiencing\nfrequent mental distress (%)", fill ="Development tier",)+theme( legend.position ="inside", legend.position.inside =c(0.8, 0.22))
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-10-nc-places-multi-model.qmd.
Work through the application exercise in class, and render, commit, and push your edits by the end of class.