MLDataR - A Package for ML datasets

Installing the NHSDataR package

To install the package use the below instructions:

#install.packages(MLDataR)
library(MLDataR)

What datasets are included

The current list of datasets are:

More and more datasets are being added, and it is my mission to have more than 50 example datasets by the end of 2022.

Thyroid Disease dataset

I will first work with the Thyroid disease dataset and inspect the variables in the data:


glimpse(MLDataR::thyroid_disease)
#> Rows: 3,772
#> Columns: 28
#> $ ThryroidClass                  <chr> "negative", "negative", "negative", "ne~
#> $ patient_age                    <int> 41, 23, 46, 70, 70, 18, 59, 80, 66, 68,~
#> $ patient_gender                 <dbl> 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, ~
#> $ presc_thyroxine                <dbl> 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, ~
#> $ queried_why_on_thyroxine       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ~
#> $ presc_anthyroid_meds           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ~
#> $ sick                           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, ~
#> $ pregnant                       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ~
#> $ thyroid_surgery                <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ~
#> $ radioactive_iodine_therapyI131 <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ~
#> $ query_hypothyroid              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ~
#> $ query_hyperthyroid             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, ~
#> $ lithium                        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ~
#> $ goitre                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ~
#> $ tumor                          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, ~
#> $ hypopituitarism                <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ~
#> $ psych_condition                <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ~
#> $ TSH_measured                   <dbl> 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, ~
#> $ TSH_reading                    <dbl> 1.30, 4.10, 0.98, 0.16, 0.72, 0.03, NA,~
#> $ T3_measured                    <dbl> 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, ~
#> $ T3_reading                     <dbl> 2.5, 2.0, NA, 1.9, 1.2, NA, NA, 0.6, 2.~
#> $ T4_measured                    <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ~
#> $ T4_reading                     <dbl> 125, 102, 109, 175, 61, 183, 72, 80, 12~
#> $ thyrox_util_rate_T4U_measured  <dbl> 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, ~
#> $ thyrox_util_rate_T4U_reading   <dbl> 1.14, NA, 0.91, NA, 0.87, 1.30, 0.92, 0~
#> $ FTI_measured                   <dbl> 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, ~
#> $ FTI_reading                    <dbl> 109, NA, 120, NA, 70, 141, 78, 115, 132~
#> $ ref_src                        <chr> "SVHC", "other", "other", "other", "SVI~

As you can see this dataset has 28 columns and 3,772 rows. The dataset is fully documented in the help file of what each one of the items means. The next task is to use this dataset to create a ML model in TidyModels.

Create TidyModels recipe to model the thyroid dataset

This will show how to create and implement the dataset in TidyModels for a supervised ML classification task.

Data preparation

The first step will be to do the data preparation steps:

data("thyroid_disease")
td <- thyroid_disease
# Create a factor of the class label to use in ML model
td$ThryroidClass <- as.factor(td$ThryroidClass)
# Check the structure of the data to make sure factor has been created
str(td)
#> 'data.frame':    3772 obs. of  28 variables:
#>  $ ThryroidClass                 : Factor w/ 2 levels "negative","sick": 1 1 1 1 1 1 1 2 1 1 ...
#>  $ patient_age                   : int  41 23 46 70 70 18 59 80 66 68 ...
#>  $ patient_gender                : num  1 1 0 1 1 1 1 1 1 0 ...
#>  $ presc_thyroxine               : num  0 0 0 1 0 1 0 0 0 0 ...
#>  $ queried_why_on_thyroxine      : num  0 0 0 0 0 0 0 0 0 0 ...
#>  $ presc_anthyroid_meds          : num  0 0 0 0 0 0 0 0 0 0 ...
#>  $ sick                          : num  0 0 0 0 0 0 0 0 0 0 ...
#>  $ pregnant                      : num  0 0 0 0 0 0 0 0 0 0 ...
#>  $ thyroid_surgery               : num  0 0 0 0 0 0 0 0 0 0 ...
#>  $ radioactive_iodine_therapyI131: num  0 0 0 0 0 0 0 0 0 0 ...
#>  $ query_hypothyroid             : num  0 0 0 0 0 0 0 0 0 0 ...
#>  $ query_hyperthyroid            : num  0 0 0 0 0 0 0 0 0 0 ...
#>  $ lithium                       : num  0 0 0 0 0 0 0 0 0 0 ...
#>  $ goitre                        : num  0 0 0 0 0 0 0 0 0 0 ...
#>  $ tumor                         : num  0 0 0 0 0 0 0 0 1 0 ...
#>  $ hypopituitarism               : num  0 0 0 0 0 0 0 0 0 0 ...
#>  $ psych_condition               : num  0 0 0 0 0 0 0 0 0 0 ...
#>  $ TSH_measured                  : num  1 1 1 1 1 1 0 1 1 1 ...
#>  $ TSH_reading                   : num  1.3 4.1 0.98 0.16 0.72 0.03 NA 2.2 0.6 2.4 ...
#>  $ T3_measured                   : num  1 1 0 1 1 0 0 1 1 1 ...
#>  $ T3_reading                    : num  2.5 2 NA 1.9 1.2 NA NA 0.6 2.2 1.6 ...
#>  $ T4_measured                   : num  1 1 1 1 1 1 1 1 1 1 ...
#>  $ T4_reading                    : num  125 102 109 175 61 183 72 80 123 83 ...
#>  $ thyrox_util_rate_T4U_measured : num  1 0 1 0 1 1 1 1 1 1 ...
#>  $ thyrox_util_rate_T4U_reading  : num  1.14 NA 0.91 NA 0.87 1.3 0.92 0.7 0.93 0.89 ...
#>  $ FTI_measured                  : num  1 0 1 0 1 1 1 1 1 1 ...
#>  $ FTI_reading                   : num  109 NA 120 NA 70 141 78 115 132 93 ...
#>  $ ref_src                       : chr  "SVHC" "other" "other" "other" ...

Next I will remove the missing variable, you could try another imputation method here such as MICE, however for speed of development and building vignette, I will leave this for you to look into:

# Remove missing values, or choose more advaced imputation option
td <- td[complete.cases(td),]
#Drop the column for referral source
td <- td %>%
   dplyr::select(-ref_src)

Split the data

Next I will partition the data into a training and testing split, so I can evaluate how well the model performs on the testing set:

#Divide the data into a training test split
set.seed(123)
split <- rsample::initial_split(td, prop=3/4)
train_data <- rsample::training(split)
test_data <- rsample::testing(split)

Create a recipe with preprocessing steps

After I have split the data it is time to prepare a recipe for the preprocessing steps, here I will use the recipes package:

td_recipe <-
   recipe(ThryroidClass ~ ., data=train_data) %>%
   step_normalize(all_predictors()) %>%
   step_zv(all_predictors())

print(td_recipe)
#> Recipe
#> 
#> Inputs:
#> 
#>       role #variables
#>    outcome          1
#>  predictor         26
#> 
#> Operations:
#> 
#> Centering and scaling for all_predictors()
#> Zero variance filter on all_predictors()

This recipe links the outcome variable ThyroidClass and then we use a normalise function to centre and scale all the numerical outcome variables and then we will remove zero variance from the data.

Getting modelling with Parsnip

We come to the modelling step of the exercise. Here I will instantiate a random forest model for the modeeling task at hand:

set.seed(123)
rf_mod <-
  parsnip::rand_forest() %>%
  set_engine("ranger") %>%
  set_mode("classification")

Create the model workflow

Tidymodels uses the concept of workflows to stitch the ML pipeline together, so I will now create the workflow and then fit the model:

td_wf <-
   workflow() %>%
   workflows::add_model(rf_mod) %>%
   workflows::add_recipe(td_recipe)

print(td_wf)
#> == Workflow ====================================================================
#> Preprocessor: Recipe
#> Model: rand_forest()
#> 
#> -- Preprocessor ----------------------------------------------------------------
#> 2 Recipe Steps
#> 
#> * step_normalize()
#> * step_zv()
#> 
#> -- Model -----------------------------------------------------------------------
#> Random Forest Model Specification (classification)
#> 
#> Computational engine: ranger
# Fit the workflow to our training data
set.seed(123)
td_rf_fit <-
   td_wf %>%
   fit(data = train_data)
# Extract the fitted data
td_fitted <- td_rf_fit %>%
    extract_fit_parsnip()

Make predictions and evaluate with ConfusionTableR

The final step, before deploying this live, would be to make predictions on the test set and then evaluate with the ConfusionTableR package:

# Predict the test set on the training set to see model performance
class_pred <- predict(td_rf_fit, test_data)
td_preds <- test_data %>%
    bind_cols(class_pred)
# Convert both to factors
td_preds$.pred_class <- as.factor(td_preds$.pred_class)
td_preds$ThryroidClass <- as.factor(td_preds$ThryroidClass)

# Evaluate the data with ConfusionTableR
cm <- ConfusionTableR::binary_class_cm(train_labels = td_preds$ThryroidClass ,
                                       truth_labels = td_preds$.pred_class,
                                       positive="sick")
#> [INFO] Building a record level confusion matrix to store in dataset
#> [INFO] Build finished and to expose record level cm use the record_level_cm list item

Final step is to view the Confusion Matrix and collapse down for storage in a database to model accuracy drift over time:

#View Confusion matrix
cm$confusion_matrix
#> Confusion Matrix and Statistics
#> 
#>           Reference
#> Prediction negative sick
#>   negative      629    3
#>   sick           11   45
#>                                           
#>                Accuracy : 0.9797          
#>                  95% CI : (0.9661, 0.9888)
#>     No Information Rate : 0.9302          
#>     P-Value [Acc > NIR] : 3.207e-09       
#>                                           
#>                   Kappa : 0.8544          
#>                                           
#>  Mcnemar's Test P-Value : 0.06137         
#>                                           
#>             Sensitivity : 0.93750         
#>             Specificity : 0.98281         
#>          Pos Pred Value : 0.80357         
#>          Neg Pred Value : 0.99525         
#>              Prevalence : 0.06977         
#>          Detection Rate : 0.06541         
#>    Detection Prevalence : 0.08140         
#>       Balanced Accuracy : 0.96016         
#>                                           
#>        'Positive' Class : sick            
#> 
#View record level
cm$record_level_cm
#>   Pred_negative_Ref_negative Pred_sick_Ref_negative Pred_negative_Ref_sick
#> 1                        629                     11                      3
#>   Pred_sick_Ref_sick  Accuracy     Kappa AccuracyLower AccuracyUpper
#> 1                 45 0.9796512 0.8544487     0.9660936     0.9888315
#>   AccuracyNull AccuracyPValue McnemarPValue Sensitivity Specificity
#> 1    0.9302326   3.207365e-09    0.06136883      0.9375   0.9828125
#>   Pos.Pred.Value Neg.Pred.Value Precision Recall        F1 Prevalence
#> 1      0.8035714      0.9952532 0.8035714 0.9375 0.8653846 0.06976744
#>   Detection.Rate Detection.Prevalence Balanced.Accuracy               cm_ts
#> 1     0.06540698           0.08139535         0.9601563 2022-01-04 11:01:04

That is an example of how to model the Thyroid dataset, and random forest ensembles are giving us good estimates of the model performance. The Kappa level is also excellent, meaning that the model has a high likelihood of being good in practice.

Diabetes dataset

The diabetes dataset can be loaded from the package with ease also:

glimpse(MLDataR::diabetes_data)
#> Rows: 520
#> Columns: 17
#> $ Age              <dbl> 40, 58, 41, 45, 60, 55, 57, 66, 67, 70, 44, 38, 35, 6~
#> $ Gender           <chr> "Male", "Male", "Male", "Male", "Male", "Male", "Male~
#> $ ExcessUrination  <chr> "No", "No", "Yes", "No", "Yes", "Yes", "Yes", "Yes", ~
#> $ Polydipsia       <chr> "Yes", "No", "No", "No", "Yes", "Yes", "Yes", "Yes", ~
#> $ WeightLossSudden <chr> "No", "No", "No", "Yes", "Yes", "No", "No", "Yes", "N~
#> $ Fatigue          <chr> "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes~
#> $ Polyphagia       <chr> "No", "No", "Yes", "Yes", "Yes", "Yes", "Yes", "No", ~
#> $ GenitalThrush    <chr> "No", "No", "No", "Yes", "No", "No", "Yes", "No", "Ye~
#> $ BlurredVision    <chr> "No", "Yes", "No", "No", "Yes", "Yes", "No", "Yes", "~
#> $ Itching          <chr> "Yes", "No", "Yes", "Yes", "Yes", "Yes", "No", "Yes",~
#> $ Irritability     <chr> "No", "No", "No", "No", "Yes", "No", "No", "Yes", "Ye~
#> $ DelayHealing     <chr> "Yes", "No", "Yes", "Yes", "Yes", "Yes", "Yes", "No",~
#> $ PartialPsoriasis <chr> "No", "Yes", "No", "No", "Yes", "No", "Yes", "Yes", "~
#> $ MuscleStiffness  <chr> "Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", ~
#> $ Alopecia         <chr> "Yes", "Yes", "Yes", "No", "Yes", "Yes", "No", "No", ~
#> $ Obesity          <chr> "Yes", "No", "No", "No", "Yes", "Yes", "No", "No", "Y~
#> $ DiabeticClass    <chr> "Positive", "Positive", "Positive", "Positive", "Posi~

Has a number of variables that are common with people of diabetes, however some dummy encoding would be needed of the Yes / No variables to make this model work.

This is another example of a dataset that you could build an ML model on.

Heart disease prediction

The final dataset, for now, in the package is the heart disease dataset. To load and work with this dataset you could use the following:

data(heartdisease)
# Convert diabetes data to factor'
hd <- heartdisease %>%
 mutate(HeartDisease = as.factor(HeartDisease))
is.factor(hd$HeartDisease)
#> [1] TRUE

Dummy encode the dataset

The ConfusionTableR package has a dummy_encoder function baked into the package. To code up the dummy variables you could use an approach similar to below:

# Get categorical columns
hd_cat <- hd  %>%
  dplyr::select_if(is.character)
# Dummy encode the categorical variables 
 cols <- c("RestingECG", "Angina", "Sex")
# Dummy encode using dummy_encoder in ConfusionTableR package
coded <- ConfusionTableR::dummy_encoder(hd_cat, cols, remove_original = TRUE)
#> Joining, by = c("Sex", "row")
#> Joining, by = c("Angina", "row", "RestingECG")
coded <- coded %>%
     select(RestingECG_ST, RestingECG_LVH, Angina=Angina_Y,
     Sex=Sex_F)
# Remove column names we have encoded from original data frame
hd_one <- hd[,!names(hd) %in% cols]
# Bind the numerical data on to the categorical data
hd_final <- bind_cols(coded, hd_one)
# Output the final encoded data frame for the ML task
glimpse(hd_final)
#> Rows: 918
#> Columns: 11
#> $ RestingECG_ST    <dbl> 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0,~
#> $ RestingECG_LVH   <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,~
#> $ Angina           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,~
#> $ Sex              <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,~
#> $ Age              <dbl> 40, 49, 37, 48, 54, 39, 45, 54, 37, 48, 37, 58, 39, 4~
#> $ RestingBP        <dbl> 140, 160, 130, 138, 150, 120, 130, 110, 140, 120, 130~
#> $ Cholesterol      <dbl> 289, 180, 283, 214, 195, 339, 237, 208, 207, 284, 211~
#> $ FastingBS        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,~
#> $ MaxHR            <dbl> 172, 156, 98, 108, 122, 170, 170, 142, 130, 120, 142,~
#> $ HeartPeakReading <dbl> 0.0, 1.0, 0.0, 1.5, 0.0, 0.0, 0.0, 0.0, 1.5, 0.0, 0.0~
#> $ HeartDisease     <fct> 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0,~

The data is now ready for modelling in the same fashion as we saw with the thyroid dataset.

What’s on the horizon?

If you have a dataset and it is dying to be included in this package please reach out to me @StatsGary and I would be happy to add you to the list of collaborators.

I will be aiming to add an additional 30+ datasets to this package. All of which are at various stages of documentation, so the first version of this package will be released with the three core datasets, with more being added each additional version of the package.

Please keep watching the package GitHub, and make sure you install the latest updates of the package, when they are available.