Multiple Linear Regression

Learning Objectives

At the end of this lab, you will:

  1. Extend the ideas of single linear regression to consider regression models with two or more predictors
  2. Understand and interpret the coefficients in multiple linear regression models

Requirements

  1. Be up to date with lectures
  2. Have completed Week 1 and Week 2 lab exercises

Required R Packages

Remember to load all packages within a code chunk at the start of your RMarkdown file using library(). If you do not have a package and need to install, do so within the console using install.packages(" "). For further guidance on installing/updating packages, see Section C here.

For this lab, you will need to load the following package(s):

  • tidyverse
  • patchwork
  • sjPlot
  • kableExtra

Lab Data

You can download the data required for this lab here or read it in via this link https://uoepsy.github.io/data/wellbeing.csv.

Study Overview

Research Question

Is there an association between wellbeing and time spent outdoors after taking into account the association between wellbeing and social interactions?

Wellbeing/Rurality data codebook.

Setup

Setup
  1. Create a new RMarkdown file
  2. Load the required package(s)
  3. Read the wellbeing dataset into R, assigning it to an object named mwdata

Solution

Exercises

Study & Analysis Plan Overview

Question 1

Provide a brief overview of the study design and data, before detailing your analysis plan to address the research question.

  • Give the reader some background on the context of the study (you might be able to re-use some of the content you wrote for Lab 1 here)
  • State what type of analysis you will conduct in order to address the research question
  • Specify the model to be fitted to address the research question
  • Specify your chosen significance (\(\alpha\)) level
  • State your hypotheses

Solution

Descriptive Statistics

Question 2

Alongside descriptive statistics, visualize the marginal distributions of the wellbeing, outdoor_time, and social_int variables.

Plot interpretation
- The shape, center and spread of the distribution - Whether the distribution is symmetric or skewed - Whether the distribution is unimodal or bimodal

Plotting tips
- Use \n to wrap text in your titles and or axis labels - The patchwork package allows us to arrange multiple plots in two ways - | arranges the plots adjacent to one another, and / arranges the plots on top of one another

Table tips
- The kableExtra package allows us to produce well formatted tables for our descriptive statistics. To do so, you need to specify the kable() and kable_styling() arguments

Solution


Question 3

Produce plots of the associations between the outcome variable (wellbeing) and each of the explanatory variables.

Plot interpretation
- Direction of association - Form of association (can it be summarised well with a straight line?)
- Strength of association (how closely do points fall to a recognizable pattern such as a line?) - Unusual observations that do not fit the pattern of the rest of the observations and which are worth examining in more detail

Plot tips
- use \n to wrap text in your titles and or axis labels - consider using geom_smooth() to superimpose the best-fitting line describing the association of interest

Solution


Question 4

Produce a correlation matrix of the variables which are to be used in the analysis, and write a short paragraph describing the associations.

Correlation Matrix
Review Q2 of your Week 1 lab for guidance on how to produce a correlation matrix.

APA Format
Make sure to round your numbers in-line with APA 7th edition guidelines. The round() function will come in handy here, as might this APA numbers and statistics guide!

Solution

Model Fitting & Interpretation

Question 5

Recall the model specified in Q1, and:

  1. State the parameters of the model. How do we denote parameter estimates?
  2. Fit the linear model in using lm(), assigning the output to an object called mdl1.

As we did for simple linear regression, we can fit our multiple regression model using the lm() function. We can add as many explanatory variables as we like, separating them with a +.

model_name <- lm(<response variable> ~ 1 + <explanatory variable 1> + <explanatory variable 2> + ... , data = <dataframe>)

Solution


Visual

Note that for simple linear regression we talked about our model as a line in 2 dimensions: the systematic part \(\beta_0 + \beta_1 x\) defined a line for \(\mu_y\) across the possible values of \(x\), with \(\epsilon\) as the random deviations from that line. But in multiple regression we have more than two variables making up our model.

In this particular case of three variables (one outcome + two explanatory), we can think of our model as a regression surface (see Figure 3). The systematic part of our model defines the surface across a range of possible values of both \(x_1\) and \(x_2\). Deviations from the surface are determined by the random error component, \(\hat \epsilon\).

Figure 3: Regression surface for wellbeing ~ social_int + outdoor_time, from two different angles

Don’t worry about trying to figure out how to visualise it if we had any more explanatory variables! We can only concieve of 3 spatial dimensions. One could imagine this surface changing over time, which would bring in a 4th dimension, but beyond that, it’s not worth trying!.


Question 6

Using any of:

  • mdl1
  • mdl1$coefficients
  • coef(mdl1)
  • coefficients(mdl1)
  • summary(mdl1)

Write out the estimated parameter values of:

  1. \(\hat \beta_0\), the estimated average wellbeing score associated with zero hours of outdoor time and zero social interactions per week.
  2. \(\hat \beta_1\), the estimated increase in average wellbeing score associated with an additional social interaction per week (an increase of one), holding weekly outdoor time constant.
  3. \(\hat \beta_2\), the estimated increase in average wellbeing score associated with one hour increase in weekly outdoor time, holding the number of social interactions constant
Note

Q: What do we mean by hold constant / controlling for / partialling out / residualizing for?

A: When the remaining explanatory variables are held at the same value or are fixed.

Solution


Question 7

Within what distance from the model predicted values (the regression line) would we expect 95% of WEMWBS wellbeing scores to be?

Either sigma() or part of the output from summary() will help you here.

Solution


Question 8

Based on the model, predict the wellbeing scores for the following individuals:

  • Leah: Social Interactions = 25; Outdoor Time = 3
  • Sean: Social Interactions = 19; Outdoor Time = 36
  • Mike: Social Interactions = 15; Outdoor Time = 20
  • Donna: Social Interactions = 7; Outdoor Time = 1

Who has the highest predicted wellbeing score, and who has the lowest?

Solution

Writing Up & Presenting Results

Question 9

Provide key model results in a formatted table.

Solution


Question 10

Interpret your results in the context of the research question.

Make reference to the your regression table.

Make sure to include a decision in relation to your null hypothesis - based on the evidence, should you reject or fail to reject the null?

Solution