| variable | description |
|---|---|
| age | Age in years of respondent |
| outdoor_time | Self report estimated number of hours per week spent outdoors |
| social_int | Self report estimated number of social interactions per week (both online and in-person) |
| routine | Binary 1=Yes/0=No response to the question 'Do you follow a daily routine throughout the week?' |
| wellbeing | Warwick-Edinburgh Mental Wellbeing Scale (WEMWBS), a self-report measure of mental health and wellbeing. The scale is scored by summing responses to each item, with items answered on a 1 to 5 Likert scale. The minimum scale score is 14 and the maximum is 70 |
| location | Location of primary residence (City, Suburb, Rural) |
| steps_k | Average weekly number of steps in thousands (as given by activity tracker if available) |
04: Model comparison
This week, you’ll practice interpreting the F-test for model fit, as well as comparing different sorts of models to one another using incremental F-tests and AIC/BIC.
- Open RStudio.
- Create a new R Markdown (.Rmd) file for this week’s exercises.
- Save it somewhere you can find it again.
- Give it a clear name (for example,
dapr2_lab04.Rmd). - In the first code chunk, load the packages you’ll need this week (and install them if you don’t have them already):
tidyverse
Data dictionary:
From the Edinburgh & Lothians, 100 city/suburb residences and 100 rural residences were chosen at random and contacted to participate in the study. The Warwick-Edinburgh Mental Wellbeing Scale (WEMWBS) was used to measure mental health and wellbeing.
Participants filled out a questionnaire including items concerning: estimated average number of hours spent outdoors each week, estimated average number of social interactions each week (whether on-line or in-person), whether a daily routine is followed (yes/no). For those respondents who had an activity tracker app or smart watch, they were asked to provide their average weekly number of steps.
Test a model’s fit
Once again, read in the data from https://uoepsy.github.io/data/wellbeing_rural.csv and store it in a variable named mwdata (“mw” stands for “mental wellbeing”).
For this week’s lab, we’ll need to start by deleting all observations that contain any NA values in the following columns:
wellbeingageoutdoor_timesocial_intsteps_k
Using the tidyverse function drop_na(), remove rows containing NAs, and then assign the resulting dataframe back to the variable name mwdata.
You’ll know you’ve done it right if mwdata now contains 134 observations, not 200.
Replace the ...s with each actual variable name.
mwdata <- mwdata |>
drop_na(..., ..., ..., ..., ...)
This linear model should be familiar to you by now:
\[ \text{wellbeing} = \beta_0 + (\beta_1 \cdot \text{outdoor\_time}) + (\beta_2 \cdot \text{social\_int}) + \epsilon \]
Using the function lm(), fit this linear model and name the result m0.
🗂️ See Multiple regression > Fitting model flash card.
Now we want to know: is m0 significantly better at predicting wellbeing than a model with no predictors, i.e., a null model?
This question is addressed by the F-test reported at the very bottom of the model summary for m0.
Write a sentence to report the answer using APA notation.
🗂️ See Assessing model fit > F-ratio > Report in APA format flash card.
Compare nested models
New RQ: Is wellbeing significantly predicted by number of steps (steps_k) and hours outdoors (outdoor_time), over and above the impact of years of age (age) and number of social interactions (social_int)?
To address this RQ, you’ll need to compare two models.
The outcome variable for both models is wellbeing. Write down two lists (the order does not matter):
- the predictors in one of the models
- the predictors in the other model
Bonus challenge: Write out the formulae for these two models using mathematical model notation (that is, the format with the \(\beta\)s).
Using lm(), fit the two models you identified in Q4.
Name the model with fewer predictors m1 and the model with more predictors m2.
🗂️ See Multiple regression > Fitting model flash card.
Compare m1 and m2 using an incremental F-test.
Based on the results of the test, write a sentence that addresses the research question and reports the incremental F-test using APA notation.
🗂️ See Comparing LMs > Incremental F-test flash card.
Compare non-nested models
New RQ: Do number of steps (steps_k) and hours outdoors (outdoor_time) predict wellbeing better than years of age (age) and number of social interactions (social_int)?
To address this RQ, you’ll need to compare two models.
The outcome variable for both models is wellbeing. Write down two lists (the order does not matter):
- the predictors in one of the models
- the predictors in the other model
Bonus challenge: Write out the formulae for these two models using mathematical model notation (that is, the format with the \(\beta\)s).
Using lm(), fit the two models you identified in Q7.
Name one model m3 and the other m4 (it doesn’t matter which is which).
🗂️ See Multiple regression > Fitting model flash card.
Using AIC and BIC, compare these two models.
Based on the results, write a sentence that addresses the research question, and report each model’s AIC and BIC using APA notation.
🗂️ See Comparing LMs > AIC & BIC flash card.
Identify nestedness of models
For each of the following pairs of models, state whether they are nested or not. Assume all models are fitted to the same dataset mwdata.
Pair 1:
wellbeing ~ social_intwellbeing ~ social_int + outdoor_time
Pair 2:
wellbeing ~ social_int + outdoor_timewellbeing ~ social_int + age
Pair 3:
wellbeing ~ social_intwellbeing ~ social_int + outdoor_time + age
Pair 4:
wellbeing ~ social_int + agewellbeing ~ outdoor_time + age + social_int + steps_k
Pair 5:
wellbeing ~ social_intwellbeing ~ outdoor_time
🗂️ See Comparing LMs > Nested vs. non-nested models flash card.
Optional: Knit your report to PDF
Note: During the labs, please spend your time on the exercises above, not on rendering your document. This guidance is here in case you have extra time at the end.
Rmarkdown is a useful format because it can be used as a foundation for generating different kinds of documents. For the group report, you’ll be asked to render your Rmd file to PDF. Why not practice now?
First, make sure you have tinytex installed in R. This is what lets you convert things into PDF format.
Run the following code to install tinytex (you’ll only need to run it once).
install.packages("tinytex")
tinytex::install_tinytex()Then ensure that the “yaml” (rhymes with “mammal”) header—the bit at the top of your Rmd document—looks something like this:
---
title: "this is my report title"
author: "B1234506"
date: "07/09/2025"
output: bookdown::pdf_document2
---
Then follow the instructions from the Rmd bootcamp to knit your Rmd document to PDF.
If you encounter errors, refer to the DAPR1 formatting resources / knitting checklist.
If you are having issues knitting directly to PDF, try the following:
- Knit to HTML file
- Open your HTML in a web-browser (e.g. Chrome, Firefox)
- Print to PDF (Ctrl+P, then choose to save to PDF)
- Open file to check formatting
Review Lesson 5 of the rmd bootcamp for a detailed description/worked examples.
Hiding R Code
To not show the code of an R code chunk, and only show the output, write:
```{r, echo=FALSE}
# code goes here
```Hiding R Output:
To show the code of an R code chunk, but hide the output, write:
```{r, results='hide'}
# code goes here
```Hiding R code AND output
To hide both code and output of an R code chunk, write:
```{r, include=FALSE}
# code goes here
```