These are the slides from my workshop: Introduction to Machine Learning with R which I gave at the University of Heidelberg, Germany on June 28th 2018. The entire code accompanying the workshop can be found below the video.
The workshop covered the basics of machine learning. With an example dataset I went through a standard machine learning workflow in R with the packages caret and h2o:
reading in data
exploratory data analysis
missingness
feature engineering
training and test split
model training with Random Forests, Gradient Boosting, Neural Nets, etc.
hyperparameter tuning
Workshop - Introduction to Machine Learning with R from Shirin Glander
Setup
All analyses are done in R using RStudio. For detailed session information including R version, operating system and package versions, see the sessionInfo() output at the end of this document.
All figures are produced with ggplot2.
libraries
library(tidyverse) # for tidy data analysis
library(readr) # for fast reading of input files
library(mice) # mice package for Multivariate Imputation by Chained Equations (MICE)
Data preparation
The dataset
The dataset I am using in these example analyses, is the Breast Cancer Wisconsin (Diagnostic) Dataset. The data was downloaded from the UC Irvine Machine Learning Repository.
The first dataset looks at the predictor classes:
malignant or
benign breast mass.
The features characterise cell nucleus properties and were generated from image analysis of fine needle aspirates (FNA) of breast masses:
Sample ID (code number)
Clump thickness
Uniformity of cell size
Uniformity of cell shape
Marginal adhesion
Single epithelial cell size
Number of bare nuclei
Bland chromatin
Number of normal nuclei
Mitosis
Classes, i.e. diagnosis
bc_data %
mutate(bare_nuclei = as.numeric(bare_nuclei),
classes = ifelse(classes == &quot;2&quot;, &quot;benign&quot;,
ifelse(classes == &quot;4&quot;, &quot;malignant&quot;, NA)))
summary(bc_data)
## sample_code_number clump_thickness uniformity_of_cell_size
## Min. : 61634 Min. : 1.000 Min. : 1.000 ## 1st Qu.: 870688 1st Qu.: 2.000 1st Qu.: 1.000 ## Median : 1171710 Median : 4.000 Median : 1.000 ## Mean : 1071704 Mean : 4.418 Mean : 3.134 ## 3rd Qu.: 1238298 3rd Qu.: 6.000 3rd Qu.: 5.000 ## Max. :13454352 Max. :10.000 Max. :10.000 ## ## uniformity_of_cell_shape marginal_adhesion single_epithelial_cell_size
## Min. : 1.000 Min. : 1.000 Min. : 1.000 ## 1st Qu.: 1.000 1st Qu.: 1.000 1st Qu.: 2.000 ## Median : 1.000 Median : 1.000 Median : 2.000 ## Mean : 3.207 Mean : 2.807 Mean : 3.216 ## 3rd Qu.: 5.000 3rd Qu.: 4.000 3rd Qu.: 4.000 ## Max. :10.000 Max. :10.000 Max. :10.000 ## ## bare_nuclei bland_chromatin normal_nucleoli mitosis ## Min. : 1.000 Min. : 1.000 Min. : 1.000 Min. : 1.000 ## 1st Qu.: 1.000 1st Qu.: 2.000 1st Qu.: 1.000 1st Qu.: 1.000 ## Median : 1.000 Median : 3.000 Median : 1.000 Median : 1.000 ## Mean : 3.545 Mean : 3.438 Mean : 2.867 Mean : 1.589 ## 3rd Qu.: 6.000 3rd Qu.: 5.000 3rd Qu.: 4.000 3rd Qu.: 1.000 ## Max. :10.000 Max. :10.000 Max. :10.000 Max. :10.000 ## NA's :16 ## classes ## Length:699 ## Class :character ## Mode :character ## ## ## ## Missing data
# how many NAs are in the data
md.pattern(bc_data, plot = FALSE)
## sample_code_number clump_thickness uniformity_of_cell_size
## 683 1 1 1
## 16 1 1 1
## 0 0 0
## uniformity_of_cell_shape marginal_adhesion single_epithelial_cell_size
## 683 1 1 1
## 16 1 1 1
## 0 0 0
## bland_chromatin normal_nucleoli mitosis classes bare_nuclei ## 683 1 1 1 1 1 0
## 16 1 1 1 1 0 1
## 0 0 0 0 16 16
bc_data %
drop_na() %__%
select(classes, everything(), -sample_code_number)
head(bc_data)
## # A tibble: 6 x 10
## classes clump_thickness uniformity_of_cell_si… uniformity_of_cell_sha…
## ## 1 benign 5 1 1
## 2 benign 5 4 4
## 3 benign 3 1 1
## 4 benign 6 8 8
## 5 benign 4 1 1
## 6 malignant 8 10 10
## # ... with 6 more variables: marginal_adhesion ,
## # single_epithelial_cell_size , bare_nuclei ,
## # bland_chromatin , normal_nucleoli , mitosis Missing values can be imputed with the mice package.
More info and tutorial with code: https://shirinsplayground.netlify.com/2018/04/flu_prediction/
Data exploration
Response variable for classification
ggplot(bc_data, aes(x = classes, fill = classes)) +
geom_bar()
More info on dealing with unbalanced classes: https://shiring.github.io/machine_learning/2017/04/02/unbalanced
Response variable for regression
ggplot(bc_data, aes(x = clump_thickness)) +
geom_histogram(bins = 10)
Features
gather(bc_data, x, y, clump_thickness:mitosis) %__%
ggplot(aes(x = y, color = classes, fill = classes)) +
geom_density(alpha = 0.3) +
facet_wrap( ~ x, scales = &quot;free&quot;, ncol = 3)
Correlation graphs
co_mat_benign %
select(-1) %__%
cor()
co_mat_malignant %
select(-1) %__%
cor()
library(igraph)
g_benign