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
install.packages(c("BiocManager", "devtools", "pdftools", "dagitty"))
# Install libraries from BioConductor
BiocManager::install("graph")
BiocManager::install("RBGL")
BiocManager::install("Rgraphviz")
install.packages(c("micd", "pcalg"))
remotes::install_github("annennenne/causalDisco")
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(c("torch", "torchvision"))
If the installation above succeeds without any errors, you can check that all is working with the following code:
library("scorcher")
library(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 <- dl |>
initiate_scorch() |>
scorch_layer("conv2d", in_channels = 1, out_channels = 32, kernel_size = 3) |>
scorch_layer("relu") |>
scorch_layer("conv2d", in_channels = 32, out_channels = 64, kernel_size = 3) |>
scorch_layer("relu") |>
scorch_layer("max_pool2d", kernel_size = 2) |>
scorch_layer("dropout2d", p = 0.25) |>
scorch_function(torch_flatten, start_dim = 2) |>
scorch_layer("linear", 9216, 128) |>
scorch_layer("relu") |>
scorch_layer("linear", 128, 10)
#- Compile the Neural Network
compiled_scorch_model <- compile_scorch(scorch_model)
#-- Training the Neural Network
fitted_scorch_model <- compiled_scorch_model |>
fit_scorch(loss = nn_cross_entropy_loss, num_epochs = 10, verbose = T)
#- 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
fitted_scorch_model$eval()
pred <- fitted_scorch_model(x_test) |> torch_argmax(dim = 2)
accuracy <- sum(pred == y_test)$item() / length(y_test)
cat(sprintf("Testing Accuracy: %.2f%%\n", accuracy * 100))
If your installation was successful, the output should end with the following information (maybe with slightly different numbers):
Testing Accuracy: 98.69%
If not, we will try to fix it in Florence.
We will use a LaTeX backend for some of the plotting. If you do not have LaTeX installed already, you may install it from R using the tinytex package:
install.packages("tinytex")
library(tinytex)
install_tinytex()
Claus Thorn Ekstrøm and Anders Tolver 2025