Summary and Schedule
ATTENTION This is an experimental test of The Carpentries Workbench lesson infrastructure. It was automatically converted from the source lesson via the lesson transition script.
If anything seems off, please contact Zhian Kamvar zkamvar@carpentries.org
Data Carpentry’s aim is to teach researchers basic concepts, skills, and tools for working with data so that they can get more done in less time, and with less pain. This lesson was designed for researchers interested in working with public health data in R, but may be of interest to researchers in other fields as well.
This lesson provides an introduction to simple linear regression. The episodes cover the concept of simple linear regression, the use of simple linear regression with various types of predictor variables (single continuous variable, single factor variable with two groups and single factor variable with more than two groups), predictions of the mean and the assessment of model fit and assumptions.
Getting started
To get started, see the instructions in the Setup page. There you will learn how to obtain the data and packages used in this lesson.
Prerequisites
This lesson does not require a formal background in statistics.
This lesson requires:
- Working copies of R and RStudio. See here for installation instructions.
- An understanding of how to use the Tidyverse packages to summarise and manipulate data in RStudio. See these episodes on data handling and data manipulation.
- An understanding of how to use the ggplot2 package to plot data in RStudio. See this episode on data visualisation.
- An understanding of the concepts covered in the Statistical thinking for public health lesson.
Setup Instructions | Download files required for the lesson | |
Duration: 00h 00m | 1. An introduction to linear regression |
What type of variables are required for simple linear
regression? What do each of the components in the equation of a simple linear regression model represent? |
Duration: 00h 20m | 2. Linear regression with one continuous explanatory variable |
How can we assess whether simple linear regression is a suitable way to
model the relationship between two continuous variables? How can we fit a simple linear regression model with one continuous explanatory variable in R? How can the parameters of this model be interpreted in R? How can this model be visualised in R? |
Duration: 01h 00m | 3. Linear regression with a two-level factor explanatory variable |
How can we explore the relationship between one continuous variable and
one categorical variable with two groups prior to fitting a simple
linear regression? How can we fit a simple linear regression model with one two-level categorical explanatory variable in R? How does the use of the simple linear regression equation differ between the continuous and categorical explanatory variable cases? How can the parameters of this model be interpreted in R? How can this model be visualised in R? |
Duration: 01h 40m | 4. Making predictions from a simple linear regression model |
How can predictions be manually obtained from a simple linear regression
model? How can R be used to obtain predictions from a simple linear regression model? |
Duration: 02h 00m | 5. Assessing simple linear regression model fit and assumptions |
What does it mean to assess model fit? What does \(R^2\) quantify and how is it interpreted? What are the six assumptions of simple linear regression? How do I check if any of these assumptions are violated? |
Duration: 04h 00m | 6. Optional: linear regression with a multi-level factor explanatory variable |
How can we explore the relationship between one continuous variable and
one multi-level categorical variable prior to fitting a simple linear
regression? How can we fit a simple linear regression model with one multi-level categorical explanatory variable in R? How can the parameters of this model be interpreted in R? How can this model be visualised in R? |
Duration: 04h 25m | Finish |
The actual schedule may vary slightly depending on the topics and exercises chosen by the instructor.
To get set up for this lesson, there are a few steps that you will need to follow, as explained below:
- Install R and RStudio, or update these if you already have the programs installed.
- Download the RStudio Project environment for this lesson.
- Set up the packages used in this lesson using
renv
.
Installing or updating R and RStudio
If you do not have R and RStudio installed on your computer, follow the installation instructions here. If you have R and RStudio installed, please make sure that your computer is running the latest versions by following the instructions here.
Downloading the RStudio Project environment
To ensure that you have the data and packages used in this lesson, you can use a dedicated RStudio Project environment. Please follow the following steps:
- Create a folder on your computer for the lesson,
e.g.
simple-linear-reg-lesson
in your documents folder. - Download the environment as a zipped folder by left clicking here.
- Unzip the downloaded folder into the
simple-linear-reg-lesson
folder.
Setting up the packages used in the lesson
To ensure that everyone is using the same versions of packages used
in this lesson, as well as to avoid clashes with packages installed on
your computer, the RStudio Project environment has a pre-defined package
library using renv
. To install the required packages,
please follow the following steps:
- Launch the RStudio Project by double-clicking on the
workshop-project.RProj
file insimple-linear-reg-lesson
. - Allow
renv
to install the required packages locally by typingrenv::restore()
in the console. - When the prompt comes up, answer it with
Y
. - After installation has completed, load the required packages by
typing
source("scripts/load_packages.R")
into the console. - You are now set up for the lesson!
Note: the lesson data should be in your Environment under the name
dat
. If you would like more information about the data, see
the [data][lesson-data] page.