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

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.

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

Study Overview

Research Question

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

Wellbeing data codebook.

Question 1

Produce plots of the marginal distributions (the distributions of each variable in the analysis without reference to the other variables) of the wellbeing, outdoor_time, and social_int variables.

  • You could use, for example, geom_density() for a density plot or geom_histogram() for a histogram.
  • Look at the shape, center and spread of the distribution. Is it symmetric or skewed? Is it unimodal or bimodal?

Solution


Question 2

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

Think about:

  • 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 3

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

A table showing the correlation coefficients - \(r_{(x,y)}=\frac{\mathrm{cov}(x,y)}{s_xs_y}\) - between variables. Each cell in the table shows the relationship between two variables. The diagonals show the correlation of a variable with itself (and are therefore always equal to 1).

In R, we can create a correlation matrix by giving the cor() function a dataframe. However, we only want to give it 3 columns here. Think about how we select specific columns, either using select(), or giving the column numbers inside [].

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


Question 4

The scatterplots we created above show moderate, positive, and linear relationships both between outdoor time and wellbeing, and between numbers of social interactions and wellbeing.

  1. Specify the form of your model, where \(y\) = scores on the WEMWBS, \(x_1\) = weekly number of social interactions, and \(x_2\) = weekly outdoor time.

  2. What are the parameters of the model. How do we denote parameter estimates?

  3. 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 +.

( <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 5

State the research question in the form of a testable hypothesis.

You must define both a null (\(H_0\)) and alternative hypothesis (\(H_1\)).

Solution


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 surface) 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 = 24; Outdoor Time = 3
  • Sean: Social Interactions = 19; Outdoor Time = 26
  • Mike: Social Interactions = 15; Outdoor Time = 20
  • Donna: Social Interactions = 7; Outdoor Time = 2

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

Solution


Question 9

Should we reject or fail to reject \(H_0\)? Why?

Solution


Question 10

Interpret the outdoor time coefficient in the context of the research question.

Solution