1  Setting Up Your Computing Environment

1.1 Step 0: Download the Course Materials

The easiest way is to download the course materials as a ZIP file:

  • Go to https://github.com/mlhealthcourse/mlhealthcourse.github.io
  • Click the green “Code” button, then click “Download ZIP.”

If you are familiar with git, you can also open a terminal and run the following command to clone the course repository:

Code
git clone https://github.com/mlhealthcourse/mlhealthcourse.github.io.git

We won’t use git for the rest of the course, but if programming is going to be a part of your career, we strongly recommend the PhD course Git & GitHub at the University of Copenhagen.

1.2 Step 1: Setting Up Your Computing Environment

1.2.1 Why Two Languages?

Throughout this course, you will have the opportunity to work with both R and Python. This is not an accident or an attempt to double your workload. In modern biostatistics, epidemiology, and health data science, both languages appear regularly in published research, collaborative projects, and industry applications. R has deep roots in classical statistics and has an extraordinary ecosystem of packages for survival analysis, Bayesian modelling, and clinical reporting. Python dominates in machine learning, deep learning, and large-scale data engineering. By learning both, you will be able to read and contribute to a wider range of projects, collaborate with more colleagues, and choose the best tool for each task.

You do not need to be an expert programmer to succeed in this course. We will introduce code gradually and explain every line. Think of R and Python as lab instruments: you will learn to use them by doing, and we will always prioritise understanding the statistical ideas over memorising syntax.

TipWhich language should I pick?

If you have little or no programming experience, or prefer sticking to R, use Setup 1: R in RStudio. RStudio is designed for data analysis and is standard in many biostatistics departments. You can always add Python development to your toolkit later on.

If you’d prefer to solely use Python, or are already familiar with it, use Setup 2: Python in VS Code. VS Code is a general-purpose code editor that works well for both R and Python, and it is widely used in industry.

You may combine both setups if you would like to explore both languages.

If you are curious and/or have a little experience with the terminal, use Setup 3: R and Python in VS Code. We will show you how to set up a single development environment that can handle both languages, but it is more advanced and requires comfort with the command line.

1.2.2 Step 1.1: R and RStudio

What Are R and RStudio?

R is a programming language designed for statistical computing and graphics. It runs in a terminal or console, but working with raw R in a terminal is not the most pleasant experience. RStudio is an integrated development environment (IDE) that wraps around R, providing a code editor, a console, a file browser, a plot viewer, and much more in a single window. Think of R as the engine and RStudio as the dashboard and steering wheel.

1.2.3 Step 1.1.1: Download and Install R

  1. Open your web browser and navigate to the Comprehensive R Archive Network (CRAN): https://cran.r-project.org/
  2. You will see links for your operating system near the top of the page:
    • Windows: Click “Download R for Windows,” then click “base,” then click the link that says something like “Download R-4.5.3 for Windows.” Run the downloaded .exe installer and accept all default settings.
    • macOS: Click “Download R for macOS.” Choose the appropriate version for your Mac. If you have an Apple Silicon Mac (M1, M2, M3, or M4 chip), download the arm64 version. If you have an older Intel Mac, download the x86_64 version. If you are unsure, click the Apple icon in the top-left corner of your screen, choose “About This Mac,” and look at the “Chip” or “Processor” field. Open the downloaded .pkg file and follow the installation prompts.
    • Linux: Click “Download R for Linux,” select your distribution (Ubuntu, Fedora, etc.), and follow the instructions. On Ubuntu, you can also install R from the terminal:
Code
# Ubuntu/Debian
sudo apt update
sudo apt install r-base r-base-dev
  1. Verify the installation by opening a terminal (or Command Prompt on Windows) and typing:
Code
R --version

You should see output that includes the R version number (4.5.3 or later is recommended).

1.2.4 Step 1.1.2: Download and Install RStudio

  1. Navigate to the Posit (formerly RStudio) download page: https://posit.co/download/rstudio-desktop/
  2. The page will detect your operating system and suggest the correct installer. Click the download button.
  3. Run the installer:
    • Windows: Run the .exe file and follow the prompts.
    • macOS: Open the .dmg file and drag RStudio to your Applications folder.
    • Linux: Install the .deb or .rpm package using your package manager.
  4. Open RStudio. You should see four panels: the Source editor (top left), the Console (bottom left), the Environment/History pane (top right), and the Files/Plots/Packages/Help pane (bottom right). If R is installed correctly, you will see a welcome message in the Console that includes the R version number.

1.2.5 Step 1.1.3: A Quick Tour of RStudio

  • Console (bottom left): Type R commands here and press Enter to run them immediately. Good for quick exploration.
  • Source Editor (top left): Write and save R scripts (.R files) or Quarto documents (.qmd files). You can run lines or selections by pressing Ctrl+Enter (Cmd+Enter on Mac).
  • Environment (top right): Shows all variables and data objects currently in memory.
  • Files/Plots/Packages/Help (bottom right): Browse files on your computer, view plots, manage installed packages, and read documentation.

1.2.6 Step 1.1.4: Install key course packages for R

R packages extend the language with specialised tools. Think of them as apps you install on your phone — the base R system is the phone itself, and packages add new capabilities.

Open RStudio and run the following command in the Console. This will take several minutes the first time because it downloads and compiles many packages:

Code
source("scripts/install_packages.R")

To verify that the installation worked, you can re-run the same command. If all packages are already installed, R will skip them and print: All packages are already installed.

If you get an error like there is no package called 'xyz', try reinstalling that specific package using: install.packages('xyz'). If compilation errors occur (common on Linux), you may need to install system-level dependencies. RStudio will usually tell you what is missing.

What Each Package Does (Brief Overview)

Key R packages for this course
Package Purpose
tidyverse A collection of packages for data wrangling and visualisation. Includes ggplot2 (plotting), dplyr (data manipulation), tidyr (reshaping), and more.
rms Frank Harrell’s Regression Modeling Strategies package. Provides tools for fitting and validating regression models with a focus on best practices.
glmnet Fits penalised (regularised) generalised linear models, including lasso and ridge regression.
survival The foundational package for survival (time-to-event) analysis in R.
brms An interface to Stan for fitting Bayesian generalised linear mixed models using familiar R formula syntax.
rstanarm Similar to brms but with pre-compiled Stan models for faster startup. Great for common Bayesian regression tasks.
ranger A fast implementation of random forests, useful for both classification and regression.
xgboost Extreme gradient boosting — one of the most powerful and popular machine learning algorithms.
mice Handles missing data through multiple imputation, a principled approach to dealing with incomplete datasets.
dcurves Implements decision curve analysis for evaluating clinical prediction models.
pROC Computes and displays ROC curves and calculates the area under the curve (AUC).
uwot Implements UMAP (Uniform Manifold Approximation and Projection) for dimensionality reduction.
cluster Provides methods for cluster analysis including k-medoids (PAM), hierarchical clustering, and more.
gtsummary Creates publication-quality summary and regression tables.
MatchIt Implements propensity score matching and other matching methods for causal inference.
tidymodels A unified framework for building, tuning, and evaluating machine learning models in R.

1.2.7 Step 1.2: Python and VS Code

What Are Python and VS Code?

Python is a general-purpose programming language widely used in data science and machine learning. Unlike R, Python is not designed exclusively for statistics, but its ecosystem of scientific libraries makes it extremely powerful for data analysis.

There are many tools available for coding in Python. For this course, we recommend VS Code, general-purpose code editor from Microsoft. It supports extensions which make development in Python (and R) easy.

VS Code also allows you to write code in notebooks, an interactive document format that mixes code and narrative text, directly inside VS Code which is convenient for data science purposes.

Other alternatives include Positron, JupyterLab and Spyder. If you are already comfortable with one of these, feel free to use it instead of VS Code.

1.2.9 Step 1.2.2: Install VS Code

  1. Download VS Code from https://code.visualstudio.com/
  2. Install it using the standard process for your operating system.
  3. Open VS Code, click the Extensions icon in the left sidebar (it looks like four small squares), and install the following extensions:
    • Python (by Microsoft) — provides Python language support, debugging, and Jupyter notebook integration.
    • Jupyter (by Microsoft) — adds full Jupyter notebook support inside VS Code.
    • Quarto (by Quarto) — optional, but useful if you want to edit .qmd files in VS Code.
    • R (by REditorSupport) — optional, provides R language support in VS Code.

1.2.10 Step 1.2.3: Verify Python Installation

Code
# Activate the environment
pixi shell

# Run the verification script
python scripts/check_packages.py

1.3 Key Python Packages

Key Python packages for this course
Package Purpose
pandas The primary data manipulation library. DataFrames (tables) are the central data structure.
numpy Numerical computing — arrays, linear algebra, random number generation.
scikit-learn The standard machine learning library. Classification, regression, clustering, preprocessing, model evaluation.
statsmodels Classical statistical models: linear regression, logistic regression, time series, and more. Provides p-values and confidence intervals that scikit-learn does not.
lifelines Survival analysis in Python: Kaplan-Meier curves, Cox proportional hazards models, and more.
xgboost Gradient boosted trees (same algorithm as the R version).
pymc Bayesian statistical modelling and probabilistic programming.
bambi BAyesian Model-Building Interface — a high-level interface to PyMC using R-style formulas.
arviz Visualisation and diagnostics for Bayesian models.
umap-learn UMAP dimensionality reduction for Python.
matplotlib The foundational plotting library for Python.
seaborn Statistical data visualisation built on top of matplotlib. Easier to use for common statistical plots.
miceforest Multiple imputation using random forests in Python.

1.3.1 Verifying Python Package Installation

Code
import pandas as pd
import numpy as np
import sklearn
import statsmodels
import matplotlib.pyplot as plt

print(f"pandas:       {pd.__version__}")
print(f"numpy:        {np.__version__}")
print(f"scikit-learn: {sklearn.__version__}")
print("All key packages loaded successfully!")

1.4 Hello World: Your First Analysis

Let us make sure everything works by loading a built-in dataset and creating a simple plot in both languages. We will use the classic iris dataset — measurements of petal and sepal dimensions for three species of iris flowers. While not a clinical dataset, it is available in both R and Python without any downloads, making it perfect for a quick test.

Code
# Load the tidyverse (includes ggplot2 and dplyr)
library(tidyverse)

# The iris dataset is built into R
data(iris)

# Take a quick look at the first few rows
head(iris)

# Summary statistics
summary(iris)

# Create a scatter plot of Sepal Length vs. Petal Length, colored by Species
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) +
  geom_point(size = 2, alpha = 0.7) +
  labs(
    title = "Iris Dataset: Sepal Length vs. Petal Length",
    x = "Sepal Length (cm)",
    y = "Petal Length (cm)"
  ) +
  theme_minimal()
Code
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris

# Load the iris dataset
iris_data = load_iris()
iris = pd.DataFrame(
    iris_data.data,
    columns=iris_data.feature_names
)
iris["species"] = pd.Categorical.from_codes(
    iris_data.target, iris_data.target_names
)

# Take a quick look
print(iris.head())
print(iris.describe())

# Create a scatter plot
plt.figure(figsize=(8, 5))
sns.scatterplot(
    data=iris,
    x="sepal length (cm)",
    y="petal length (cm)",
    hue="species",
    alpha=0.7,
    s=60
)
plt.title("Iris Dataset: Sepal Length vs. Petal Length")
plt.tight_layout()
plt.show()

If you see a colourful scatter plot with three clusters of points, congratulations — your setup is working.

1.5 How to Use the Course Materials

1.5.2 Code Blocks

Throughout this website, you will encounter code blocks like this:

Code
# This is an R code block
x <- c(1, 2, 3, 4, 5)
mean(x)

Many chapters present code in tabbed panels labelled “R” and “Python.” Click the tab for your preferred language. We encourage you to try both, but if you are short on time, pick the one you are more comfortable with and come back to the other later.

1.5.3 Exercises

Each chapter ends with exercises. They follow this pattern:

  1. A description of the task inside a coloured callout box.
  2. Starter code that sets up the problem and leaves blanks for you to fill in.
  3. A Solution hidden inside a collapsible box — try the exercise yourself before peeking!

1.5.4 Downloading Notebooks

For hands-on practice, you can download the exercises as standalone notebooks:

  • R users: Look for .qmd or .Rmd files in the course repository that you can open in RStudio.
  • Python users: Look for .ipynb (Jupyter notebook) files in the course repository that you can open in JupyterLab or VS Code.

These live in the course repository you downloaded in Step 0, at https://github.com/mlhealthcourse/mlhealthcourse.github.io.

1.5.5 Rendering Quarto Documents

If you want to render .qmd files yourself (to produce HTML or PDF output), you need to install Quarto:

  1. Download Quarto from https://quarto.org/docs/get-started/
  2. Install it following the instructions for your operating system.
  3. In RStudio, you can render a .qmd file by clicking the “Render” button. In VS Code, use the Quarto extension’s render command. From the terminal:
Code
quarto render my_document.qmd

1.6 Troubleshooting Common Setup Issues

1.6.1 R and RStudio Issues

Problem: RStudio cannot find R. Solution: Make sure you installed R before installing RStudio. If you installed them in the wrong order, try reinstalling RStudio. On Windows, RStudio looks for R in standard installation locations. If you installed R to a non-standard path, go to Tools > Global Options > General and set the R version manually.

Problem: Package installation fails with a compilation error. Solution: Some R packages need to compile C++ or Fortran code. On Windows, install Rtools. On macOS, install the Xcode Command Line Tools by running xcode-select --install in Terminal. On Linux, install build-essential and r-base-dev.

Problem: brms or rstanarm fails to install. Solution: These packages depend on Stan, a probabilistic programming language that requires a C++ compiler. Follow the instructions above for installing compilation tools. On Windows, make sure Rtools is on your PATH. Installation can take 10–15 minutes — be patient.

Problem: Package loads but you get warnings about versions. Solution: Warnings (yellow text) are usually harmless — they often say things like “package was built under R version X.Y.Z.” Errors (red text) are the ones that prevent code from running. If you get errors, try updating the package with install.packages("package_name").

Problem: Installing rmarkdown fails with object 'attr' is not exported by 'namespace:xfun', or installing packages fails with undefined symbol: SET_BODY from yaml.so. Solution: These errors occur when system-installed packages (e.g., xfun or yaml in /usr/local/lib/R/site-library/) were compiled against an older version of R and are incompatible with R 4.6+. Reinstall the offending packages from source: install.packages(c("xfun", "yaml"), type = "source"). If the system library is read-only, use sudo R -e 'install.packages("yaml", lib="/usr/local/lib/R/site-library", type="source")'. If rmarkdown still fails after updating xfun, the CRAN version may lag behind; install the development version with remotes::install_github("rstudio/rmarkdown").

1.6.2 Python Issues

Problem: python command not found. Solution: On some systems, Python 3 is accessed via python3 instead of python. Try python3 --version. If using pixi, make sure you are inside the project directory and run commands with pixi run or activate the shell with pixi shell.

Problem: ModuleNotFoundError: No module named 'xyz'. Solution: The package is not installed in your current environment. Make sure you have activated the pixi environment with pixi shell or are running commands via pixi run.

Problem: pip install pymc fails with obscure errors. Solution: PyMC can be tricky to install because it depends on compiled numerical libraries. If you are using pixi, the lock file should handle this automatically — run pixi install and try again.

1.6.3 Common Error Messages

Error: Error: object 'x' not found Solution: You haven’t run the earlier code blocks that create x. Go back and run all preceding code chunks in order. In RStudio, use “Run All Chunks Above” from the Run menu. In Jupyter, use “Run All Above” from the Cell menu.

Error: Error in library(xxx): there is no package called 'xxx' Solution: Install it first with install.packages("xxx") in R, or pip install xxx in Python. Then try loading it again.

1.6.4 General Tips

  • Keep your software updated. At the start of each semester, update R, RStudio, Python, and your packages.
  • Use separate environments. Conda environments (Python) and renv (R) prevent package version conflicts between projects.
  • Read error messages carefully. They usually tell you exactly what went wrong, even if the language is technical. Copy the last line of an error message into a search engine — someone else has almost certainly had the same problem.
  • Ask for help. Post on the course discussion board with the full error message, your operating system, and what you were trying to do. Screenshots are helpful.
TipExercise 1: Verify Your Setup

Confirm that both R and Python are working by completing the following tasks:

  1. In R, load the tidyverse package and use ggplot2 to create a histogram of the Sepal.Width column from the built-in iris dataset.
  2. In Python, use seaborn to create a histogram of the sepal width (cm) column from scikit-learn’s iris dataset.
Code
# Load tidyverse
library(tidyverse)

# Create a histogram of Sepal.Width from the iris dataset
# YOUR CODE HERE
Code
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris

# Load iris and create a DataFrame
iris_data = load_iris()
iris = pd.DataFrame(iris_data.data, columns=iris_data.feature_names)

# Create a histogram of sepal width
# YOUR CODE HERE
Code
# =============================================================================
# Chapter 1 (Setup) - Exercise 1: Verify Your Setup
# =============================================================================

library(tidyverse)

ggplot(iris, aes(x = Sepal.Width)) +
  geom_histogram(binwidth = 0.2, fill = "steelblue", color = "white") +
  labs(
    title = "Distribution of Sepal Width",
    x = "Sepal Width (cm)",
    y = "Count"
  ) +
  theme_minimal()
Code
"""
Chapter 1 (Setup) - Exercise 1: Verify Your Setup
"""

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris

iris_data = load_iris()
iris = pd.DataFrame(iris_data.data, columns=iris_data.feature_names)

plt.figure(figsize=(8, 5))
sns.histplot(iris["sepal width (cm)"], bins=15, kde=True, color="steelblue")
plt.title("Distribution of Sepal Width")
plt.xlabel("Sepal Width (cm)")
plt.ylabel("Count")
plt.tight_layout()
plt.show()
TipExercise 2: Explore a Clinical Dataset

Now let us work with something more relevant to health sciences. Both R and Python include the mtcars dataset (or we can simulate clinical-like data). In this exercise, create a scatter plot relating two variables and add a trend line.

Code
library(tidyverse)

# Let's simulate a small clinical dataset
set.seed(42)
n <- 200
clinical <- tibble(
  age = round(rnorm(n, mean = 55, sd = 12)),
  systolic_bp = round(100 + 0.8 * age + rnorm(n, sd = 10)),
  bmi = round(rnorm(n, mean = 27, sd = 5), 1)
)

# Create a scatter plot of age vs. systolic blood pressure with a trend line
# Hint: use geom_point() and geom_smooth(method = "lm")
# YOUR CODE HERE
Code
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

# Simulate a small clinical dataset
np.random.seed(42)
n = 200
clinical = pd.DataFrame({
    "age": np.round(np.random.normal(55, 12, n)).astype(int),
})
clinical["systolic_bp"] = np.round(100 + 0.8 * clinical["age"] + np.random.normal(0, 10, n))
clinical["bmi"] = np.round(np.random.normal(27, 5, n), 1)

# Create a scatter plot of age vs. systolic blood pressure with a trend line
# Hint: use sns.regplot() or sns.lmplot()
# YOUR CODE HERE
Code
# =============================================================================
# Chapter 1 (Setup) - Exercise 2: Explore a Clinical Dataset
# =============================================================================

library(tidyverse)

set.seed(42)
n <- 200
clinical <- tibble(
  age = round(rnorm(n, mean = 55, sd = 12)),
  systolic_bp = round(100 + 0.8 * age + rnorm(n, sd = 10)),
  bmi = round(rnorm(n, mean = 27, sd = 5), 1)
)

ggplot(clinical, aes(x = age, y = systolic_bp)) +
  geom_point(alpha = 0.5, color = "darkblue") +
  geom_smooth(method = "lm", color = "firebrick", se = TRUE) +
  labs(
    title = "Age vs. Systolic Blood Pressure",
    subtitle = "Simulated clinical data (n = 200)",
    x = "Age (years)",
    y = "Systolic Blood Pressure (mmHg)"
  ) +
  theme_minimal()
Code
"""
Chapter 1 (Setup) - Exercise 2: Explore a Clinical Dataset
"""

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

np.random.seed(42)
n = 200
clinical = pd.DataFrame({
    "age": np.round(np.random.normal(55, 12, n)).astype(int),
})
clinical["systolic_bp"] = np.round(100 + 0.8 * clinical["age"] + np.random.normal(0, 10, n))
clinical["bmi"] = np.round(np.random.normal(27, 5, n), 1)

plt.figure(figsize=(8, 5))
sns.regplot(
    data=clinical, x="age", y="systolic_bp",
    scatter_kws={"alpha": 0.5, "color": "darkblue"},
    line_kws={"color": "firebrick"}
)
plt.title("Age vs. Systolic Blood Pressure\nSimulated clinical data (n = 200)")
plt.xlabel("Age (years)")
plt.ylabel("Systolic Blood Pressure (mmHg)")
plt.tight_layout()
plt.show()

1.7 References and Further Reading

  • For R, see Wickham and Grolemund (2017), the definitive introduction to the tidyverse ecosystem.
  • For Python:
    • VanderPlas (2016) covers NumPy, pandas, matplotlib, and scikit-learn in depth.
    • Johnson and Karpathy (2021) offers a concise, practical introduction to NumPy.
    • McKinney (2022) is the authoritative guide to pandas, written by the library’s creator.
  • For Quarto, see the official guide (Posit 2024).
Johnson, Justin, and Andrej Karpathy. 2021. Python Numpy Tutorial (with Jupyter and Colab). https://cs231n.github.io/python-numpy-tutorial/. A concise and practical introduction to NumPy.
McKinney, Wes. 2022. Python for Data Analysis: Data Wrangling with Pandas, NumPy, and Jupyter. 3rd ed. O’Reilly Media. https://wesmckinney.com/book/. The authoritative guide to pandas, written by the creator of the library.
Posit. 2024. Quarto Documentation. https://quarto.org/docs/guide/. The official guide to Quarto, the publishing system used for this course.
VanderPlas, Jake. 2016. Python Data Science Handbook: Essential Tools for Working with Data. O’Reilly Media. https://jakevdp.github.io/PythonDataScienceHandbook/. Covers NumPy, pandas, matplotlib, and scikit-learn in depth.
Wickham, Hadley, and Garrett Grolemund. 2017. R for Data Science: Import, Tidy, Transform, Visualize, and Model Data. O’Reilly Media. https://r4ds.had.co.nz/. The definitive introduction to the tidyverse ecosystem in R.