Before the course starts you should make sure that you have installed the latest version of:
R. If possible, please install the latest version of R. It will save a ton of problems.
An integrated development environment like R Studio is also highly recommended but is not necessary.
We will be using a few specialized R packages that you need to install prior to arriving. Those are necessary for us to run the exercises. The code below should be run in R (e.g., by opening RStudio and copying them into the console) to install the packages that we will need. You need to be connected to the internet to install the packages.
If you are a Windows user, you may need to run RStudio as an administrator in order to install the packages. This can be done by opening RStudio by right-clicking the RStudio program icon and choosing “Run as administrator”.
Follow this installation guide:
Start by installing the following bunch of R packages:
install.packages(c("tidyverse", "modelr", "broom", "forcats",
"glmnet", "caret", "gglasso", "MESS",
"boot", "corrr", "here", "patchwork",
"patchwork", "equatiomatic",
"selectiveInference",
"doParallel", "pak",
"MASS", "devtools",
"knitr", "rmarkdown",
"ranger", "party", "partykit",
"yardstick",
"gbm", "iml", "rpart",
"stabs", "hal9001",
"pcalg", "SuperLearner"))
as well as these packages as well as Rust and Java
install.packages(c("BiocManager", "devtools", "dagitty"))
# Install libraries from BioConductor
BiocManager::install("RBGL")
pak::pak("disco-coders/causalDisco") # To get the newest version
install.packages("rJavaEnv")Now we need to setup additional dependencies for the
causalDisco package.
causalDisco depends on the package caugi,
which requires Rust to be installed
on your system to build from source. See https://rust-lang.org/tools/install/ for instructions on
how to install Rust.
causalDisco provides an interface to the Java library
Tetrad for causal discovery algorithms. This is done with
rJavaEnv::java_quick_install(version = 25, distribution = "Temurin")
causalDisco::install_tetrad()
To verify everything is set up correctly you can run
verify_tetrad().
To use the deep learning models using the Torch library in R we
will need the scorcher package (and a few helper
packages).
pak::pak("jtleek/scorcher")
install.packages("torch")
install.packages("torchvision")If the installation above succeeds without any errors, you can check that all is working with the following code:
library("scorcher")
library(torch)
install_torch()
library(torchvision)
#- Training Data
train_data <- mnist_dataset(
root = tempdir(),
download = TRUE,
transform = transform_to_tensor)
x_train <- torch_tensor(train_data$data, dtype = torch_float()) |>
torch_unsqueeze(2)
y_train <- torch_tensor(train_data$targets, dtype = torch_long())
#- Create the Dataloader
dl <- scorch_create_dataloader(x_train, y_train, batch_size = 500)
#- Define the Neural Network
scorch_model <- initiate_scorch(dl) |>
scorch_input("x") |>
scorch_layer("conv1", "conv2d", in_channels = 1, out_channels = 32, kernel_size = 3) |>
scorch_layer("act1", "relu") |>
scorch_layer("conv2", "conv2d", in_channels = 32, out_channels = 64, kernel_size = 3) |>
scorch_layer("act2", "relu") |>
scorch_layer("pool1", "max_pool2d", kernel_size = 2) |>
scorch_dropout("drop1", p = 0.25) |>
scorch_flatten("flat1") |>
scorch_layer("fc1", "linear", in_features = 9216, out_features = 128) |>
scorch_layer("act3", "relu") |>
scorch_layer("fc2", "linear", in_features = 128, out_features = 10) |>
scorch_output("fc2")
#- Compile the Neural Network
scorch_model <- scorch_model |>
compile_scorch(
loss_fn = nn_cross_entropy_loss(),
optimizer_fn = optim_adam,
optimizer_params = list(lr = 0.001)
)
#-- Training the Neural Network
scorch_model <- scorch_model |>
fit_scorch(num_epochs = 10, verbose = TRUE)
#- Testing Data
test_data <- mnist_dataset(
root = tempdir(),
train = FALSE,
transform = transform_to_tensor
)
x_test <- torch_tensor(test_data$data, dtype = torch_float()) |>
torch_unsqueeze(2)
y_test <- torch_tensor(test_data$targets, dtype = torch_long())
#- Model Predictions
scorch_model$nn_model$eval()
pred <- scorch_model$nn_model(x_test) |> torch_argmax(dim = 2)
accuracy <- sum(pred == y_test)$item() / length(y_test)
cat(sprintf("Testing Accuracy: %.2f%%\n", accuracy * 100))
#> Testing Accuracy: 98.59%
If your installation was successful, the output should end with the following information (maybe with slightly different numbers):
Testing Accuracy: 98.59%
If not, we will try to fix it in Florence.
Claus Thorn Ekstrøm, Mikkel Meyer Andersen, and Anders Tolver 2026