Intro to EPI 590R

Why this class?

Hello!

  • Assistant professor at Northeastern University
    • Department of Public Health and Health Sciences and the Roux Institute (Portland, Maine)
  • Started using R during my master’s (so about 10 years of experience)
    • I learned mostly by doing!
    • Twitter (😢), blogs, RStudio::conf videos (now Posit), meet-ups (e.g., RLadies)
  • Basically everything I do is in R!
  • Actual epi research in causal inference, pregnancy, lots of other stuff

About this class

Goal: Learn some best practices to make your life in R easier and your research more reproducible

  • Quick! Intense!
    • It will require practice afterward, and time to sink in
    • The goal is to set you up for success and give you resources to learn more
  • You don’t have to use everything you learn here!
    • Some of these tools I use for every project, some just occasionally
    • Experiment with what works for you, a little at a time

About this class

  • Everything you need is at epi590r.louisahsmith.com
    • Canvas will link you there, but good to bookmark
    • The website will be up indefinitely
  • General format:
    • Some slides (introduce new content and overview of exercises)
    • I’ll demonstrate while you watch
    • Practice on your own/with your classmates
    • Update your GitHub repo (commit and push) as we go – this is in part how I grade participation

Balance

Why this class?

doi:10.1111/1740-9713.01522

Why this class?

In the age of LLMs, it’s more important than ever to be able to reproduce your work and understand what you did

  • Probably less about memorizing specific functions and more about understanding the logic of your analysis and how to document it
  • Unless you are giving full power to your computer (beware!) you still need to run the code, share the output, etc. – we’ll talk about these tools

Note

We’re all figuring this out as we go along!

Why this class?

Errors are everywhere

No one and no field is immune from errors in data analysis. Our goal is to make them as unlikely as possible (and report them when we find them!)

Note

I think we may have a different class of errors with AI, but I know (and have seen) that we will still have errors to deal with.

Not only improves science but also your life!

  • It’s really boring to copy lots of numbers into a table
    • And then change a tiny thing in the analysis and do it all over again
  • It’s really frustrating to lose work when your computer crashes, or completely change an analysis before your advisor forgets what they told you last time and has you change it back
  • It’s fun when things just work! And you get more time for the fun parts of epidemiology

Demonstration

Your final project will be something like this, but a lot smaller and more fun 😉

Questions?

Connecting to GitHub

(We will talk about what Git and GitHub are next, I promise!)

  1. Install the {usethis} package:

    install.packages("usethis")

Installing packages

If you just updated R to a new “major” version, you will need to reinstall packages

  • I tend to do this as I need them rather than try to reinstall them all at once
    • RStudio tries to help!

Possible errors

Spelling the package or function’s name wrong, or not installing or loading the package

Using packages: library vs. ::

If you are writing a script you will save, and will use several functions from this package

library(usethis)
use_git_config(user.name = "Louisa Smith",
               user.email = "louisahsmith@gmail.com")

If you are just running some quick code in the console or only need to use the package a few times in a script

usethis::use_git_config(user.name = "Louisa Smith", user.email = "louisahsmith@gmail.com")

I try to only run library(package) from a script (not the console) so that there’s a “record” of me loading the package, or else I might accidentally write code that doesn’t work later

:: still means you have to install it

Installing and loading are two different things:

  • install.packages("here") – once per computer (you download it)
  • library(here) or here::here() – every session (you use it)

:: skips the library() step, not the install step.

tidycat::tidy_categorical(model)
#> Error in loadNamespace(x) : there is no package called 'tidycat'

Tip

This one trips people up, because there’s no library() line to tell you a package is involved. If you see “there is no package called …”, you need install.packages("...") first. You’ll hit exactly this with {tidycat} tomorrow.

Dependencies

Packages rely on other packages. When you install one, you get some of them but not all:

  • Imports – required for the package to work at all. You get these automatically.
  • Suggests – optional. Needed only for particular functions, examples, or tests. You have to install separately.

{gtsummary} has 10 Imports and 30 Suggests. In the exercises later today I’ll have you run:

install.packages("gtsummary", dependencies = TRUE)

Tip

dependencies = TRUE grabs the Suggests too. It takes longer, but it saves you hitting “there is no package called …” halfway through an example.

Set up git

  1. Introduce yourself:

    usethis::use_git_config(user.name = "Louisa Smith", user.email = "louisahsmith@gmail.com")

When you make changes to your code, they will be associated with this name and email address (this doesn’t really matter for our purposes)

  • You only need to do this once (per computer)

Since I only need to run this once, I would probably run this from the console (bottom) rather than a script (top)

Running from the console is great for install.packages(), quick calculations, fiddling with code until you get it right, or scenarios like this – otherwise save your code in a script!

Connect to GitHub

  1. Create a github token:

    usethis::create_github_token()

Instead of entering your password every time, this is a secure way to connect to GitHub

  • If you are ever asked for your GitHub password in RStudio, you have to give this instead

Connect to GitHub

  1. Copy the token

  2. Back in R, run this code and paste your token in the console where it says “Enter password”:

    gitcreds::gitcreds_set()

(You are not going to see the characters as you type; just hit enter after you paste)

You can do this again whenever your token expires or you are using a different device

Exercises

  • These are also listed on the course site under the slides
  • Refer back to the slides as needed
  • Ask a classmate if you’re stuck
  • Raise your hand for the teaching team
  • Done early? Help a friend! Read the resources section! Play around in R! Check your email!