Skip to content
BI & Data ScienceClassification in R, end to end

Formulas for this chapter

Min-max normalisation

x' = (x - min) / (max - min) R: preProcess(df[, features], method = c("range")) then predict(pre_proc_values, df[, features])

Before every distance-based method. Learn min and max on the training data and reuse them unchanged on the test rows.

min, max
Taken down each feature column of the training data
x'
Rescaled value; the training minimum maps to 0 and the maximum to 1

The logistic classification loop in R

set.seed(123) split <- sample.split(df$y, SplitRatio = 0.7) train <- subset(df, split == TRUE); test <- subset(df, split == FALSE) model <- glm(y ~ x1 + x2, data = train, family = binomial) prob <- predict(model, newdata = test, type = "response") pred <- factor(ifelse(prob > 0.5, 1, 0), levels = c(0, 1))

A binary outcome with predictors on any scale. The 0.5 is a business choice, not a statistical one.

family = binomial
Selects the logistic link; without it glm fits a straight line
type = "response"
Returns probabilities; the default returns log-odds
SplitRatio = 0.7
The training share, so the test set is 30 %

The k-NN loop in R

initial_split(df_norm, prop = 0.7, strata = target) train_data <- training(split); test_data <- testing(split) knn(train = trainX, test = testX, cl = trainY, k = 5)

All features numeric and normalised. Returns predictions directly: there is no model object, because nothing is fitted.

strata
Keeps each class's share the same in both sets
cl
Class labels of the training rows, passed separately from the features
k
Neighbours consulted; odd for two classes, tuned on validation data

caret's confusion matrix

confusionMatrix(pred, actual, positive = "1") Accuracy = (TP + TN) / n Sensitivity (recall)= TP / (TP + FN) Specificity = TN / (TN + FP) Pos Pred Value = TP / (TP + FP) (precision) No Information Rate = largest class count / n

Evaluating on the test set. Predictions first, truth second, and always read accuracy against the No Information Rate.

positive
Which class is the event; every off-diagonal metric is defined relative to it
No Information Rate
Accuracy of always predicting the majority class; the baseline to beat
Step 4 of 23
The real wordsTheory

What stratifying protects

Stratified splitThe share of each class is kept roughly the same in the training and the test set. In rsample that is strata = diabetes.

Suppose 20 % of patients are diabetic. An unstratified random split could easily give a test set that is 30 % diabetic, or 10 %.

Then the accuracy you measure is partly a report on the luck of the draw. Stratifying removes that noise, and on a small or imbalanced dataset it is not optional.