Introduction to aDAV in R

Welcome to the practicals for the course aDAV. In the practicals we will get hands-on experience with the materials in the lectures by doing theoretical exercises, programming in R, and completing assignments. This is the first practical. In this practical, we will start with a exercise on the distinction between supervised and unsupervised learning, and then proceed with R. We will briefly introduce how we are going to work with R and RStudio for the remainder of the course, and get to know (some of) the datasets that we are going to work with.

  • Make sure you have read and completed the preparations.
  • Read through the course schedule. Note that assignments are due in week 5 and 9.

From these practicals, nothing needs to be handed in. You are doing these practicals to get experience with the material from the lectures and to practice for the assignments (which do need to be handed in).

Supervised and unsupervised learning

In the lecture and reading material for this week we have seen that machine learning can be classified on the characteristics of the data and the tasks it aims to solve. A main distinction to be made is that between supervised and unsupervised learning:

In this exercise, we will practice this distinction using 10 examples.

  1. Classify the following examples into Supervised learning tasks and Unsupervised learning. If they are Supervised, indicate whether a classification task or a regression task applies. After 10 minutes, we will discuss the correct answers in class. Hint: always start asking yourself if would expect a response variable in the data!

  1. You work at a consultancy company, and a bank hired your services. Your customer asks you to develop a model that helps them predicting which loan applicants will default (not be able to pay the loan). Final goal: predict good or bad applicants.
  2. Is there a cat in the picture? Recognize cats in pictures of animals and other objects. Final goal: cat or not cat.
  3. A biologist friend of yours asks you to assess the phylogenetic relation between different species of birds using genetic information (traits). The outcome of your analysis is a phylogenetic tree in which species that diverged more recently are closely linked together. Phylogenetic tree of life built using ribosomal RNA sequences, after Karl Woese. Image credit: Modified from Eric Gaba, Wikimedia Commons.

  4. Using pre-existing data and satellite images containing information such as albedo, infrared refraction, colour absorption, etc., and known superficies with solar panels, train a model able of estimating the total area of solar panels per image.
  5. Market segmentation: group customers into segments based on their purchasing behaviours, age, maximal educational degree attained, etc.
  6. Predict whether nodules in a tomography are benign or malignant. You will use a dataset in which the images have been manually annotated by a committee of medic doctors.
  7. Use news and social media analytics to predict changes in the stock market. You will use a small number of stocks as target indicators, and web-scrapped text from social media and the news. You have access to previous instances of this data, and you want to predict the values for your indicators in the near future.
  8. Build a recommendation system for songs. It suggests new artists based on their multidimensional “proximity” to the ones liked by the user: “Because you liked ______ you may also like ______.”
  9. An applied researcher wants to evaluate whether the stress felt by students acts affects the relation between amount of study time and the probability of success on an exam. You will use a data collected in a classroom experiment.
  10. A pharmaceutical company is developing a drug to treat certain type of cancer. The first step is looking for targets: they ask you to analyse data consisting on the differential expression of 10,000 genes in micro-RNA chips from different tissues. You want to uncover groups of genes that display similar expression patterns in each of the tissues.

R projects and Markdown files

We assume that you are already familiar with R and Rstudio, as outlined in the entry requirements of the course. In addition, we assume you are familiar with using Rstudio projects and R Markdown files, as outlined in the course Preparation. If you haven’t completed the course preparation tab on the website yet, please do so before next class. If you feel you still lack some R skills, there are some sources mentioned under Preparation.

In this course we will always work in a project. During this practical, we will work in the project 01_R_intro_students.

  1. Open the file 01_R_intro_students.Rproj in RStudio and run the following code in the console. Do you know where the file “sometext.txt” is located on your disk?


In addition, we will make extensive use of .Rmd files, R Markdown files. With R Markdown files, we can easily create documents which seamlessly combine text, code, and plots. The document you are reading right now was generated from an R Markdown file.

  1. Open the file r_introduction_stu.Rmd from the Files pane in RStudio.

RStudio may ask you to install several packages. You should allow it to!
If these do not install, you should install and load rmarkdown; knitr and the tidyverse.

  1. Make sure you can output the R Markdown file you created to a html using Knit > Knit to HTML on top of the source pane.

The assignments in week 3 and week 8 need to be handed in as an R project folder with data, a .Rmd file, and the .html file generated from it.

From practical 2 onwards, we will always start by opening the R project and do the assignments directly in the R Markdown file. Under each assignment, you can insert an R chunk and input your code there. If you prefer, you may also work directly in a .R file for the practicals. Note that if you do this, you will still have to work with an .Rmd file for the assignments.

Datasets from the ISLR package

The first book we are using in this course is Introduction to Statistical Learning, abbreviated as ISLR. The authors use several datasets throughout the book which are packaged in the R package ISLR. The datasets are: Auto, Caravan, Carseats, College, Credit, Default, Hitters, Khan, NCI60, OJ, Portfolio, Smarket, Wage, Weekly.

  1. Install and load the package in R by running the following in the console


You only need to install packages once. When they are installed on your system, you can always load them in your environment using library(). Let’s have a closer look at some of the datasets we will be working with.

  1. Look at the Default dataset by running the following in the console. What does this dataset contain?


  1. Use the function head() to look at the first few rows of the Hitters dataset. What data does this dataset contain? What are the variable types? Hint: to get more information on what each column represents, this dataset comes with a neat help file that can be accessed through ?Hitters

  1. Do the same for the Boston dataset, contained in the MASS library. What data does this dataset contain? What are the variable types? Hint: also this dataset comes with a neat help file that can be accessed through ?Boston

  1. Use the function summary() to create a summary of the Boston dataset. What is the range and median per capita crime rate by town? And what is the range of the average number of rooms per dwelling?

Code style

Throughout this course, try to maintain a consistent and legible style for your code. This is very important as it will make your collaborators, as well as future you happy. Being able to read and understand your own code after a year of not looking at it is possible if you use consistent style and informative comments where necessary.

  1. Read through the style guide on Hadley Wickham’s website.

Try to adhere to this style for your assignments, too. Tip: in RStudio, you can display a vertical line at 80 characters to know when your code exceeds this. You can do this at Tools > Global Options > Code > Display > Show margin.


If you have followed this practical, you are all set for the remainder of this course! Next week, we will have a closer look at the Visualization using ggplot.