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 1 of 23
The ideaTheory

The same eight steps, whatever the model

Two very different classifiers, one recipe. The class's R docs run it twice, once with logistic regression and once with k-NN, and the shape never changes.

Set a seed. Split into train and test. Fit on the train. Predict on the test. Turn predictions into classes. Compare against the truth. Read the metrics. Decide.

Learn the eight steps and swapping the model is a one-line change. That is the point of the chapter.