Tidy Models with R
Dr. Ashok Kumar
Department of Statistics
University of Lucknow
Email: kumar_ashok@[Link]
Training Programme on
Data Analysis and Modelling using R
Organized by
Department of Statistics, University of Lucknow
in collaboration with
National Statistics Office, Coordination and Quality Division , Zonal Office Lucknow
March 23-27, 2026
What is Tidymodels?
The term 'tidy models' refers to a collection of principles and tools that aim to
create a consistent, modular, and human-friendly framework for machine
learning and statistical modeling.
Tidymodels is a “meta-package” for modeling and statistical analysis that shares
the underlying design philosophy, grammar, and data structures of
the tidyverse.
Understanding the Tidyverse means understanding how to approach data
manipulation, visualization, and modeling in a consistent and intuitive way.
The core idea is to make the entire modeling process—from data preprocessing
to model training, evaluation, and deployment—more organized, transparent,
and reproducible.
It builds upon the concept of 'tidy data' (where each variable is a column, each
observation is a row, and each type of observational unit is a table) and extends
it to the modeling workflow.
Why use Tidymodels?
Consistency
Whether you're running a simple linear regression or a complex advanced
model, the functions and arguments stay largely the same.
Flexibility
It separates the definition of a model (e.g., "I want a Random Forest") from the
engine used to run it (e.g., the ranger or randomForest packages).
Safety
It includes "guardrails" that help prevent common mistakes, like data leakage
(where information from your test set accidentally "leaks" into your training process).
Readability
The code follows a pipe-friendly (%>% or |>) structure, making it much easier
for others to read.
Key Principles and Components:
1. Unified API (Application Programming Interface)
Instead of learning a different set of functions for every model type (e.g., one way to fit a linear
regression, another for a random forest, and yet another for an SVM), 'tidy models' provides a
consistent interface.
This means the functions you use to specify, fit, tune, and evaluate models look similar regardless of
the underlying algorithm.
2. Modularity and Pipelines
The modeling process is broken down into distinct, interchangeable steps. These steps can then be
chained together into a 'pipeline' or 'workflow'.
Preprocessing: Tools for data cleaning, transformation, feature engineering (e.g., scaling, one-hot
encoding, imputation).
Model Specification: Defining the type of model (e.g., linear regression, decision tree) and its
hyperparameters (e.g., number of trees, regularization strength).
Resampling: Techniques like cross-validation or bootstrapping for robust model evaluation and
hyperparameter tuning.
Fitting: Training the model on data.
Prediction: Using the trained model to make predictions on new data.
Key Principles and Components…
3. Emphasis on Resampling and Hyperparameter Tuning
'Tidy models' frameworks often integrate robust methods for resampling (like k-fold
cross-validation) and hyperparameter tuning (like grid search or Bayesian
optimization) as first-class citizens.
This helps in building models that generalize well to unseen data and in selecting
optimal model configurations.
4. Consistent Output Structures
• The results of various modeling steps (e.g., model fits, performance metrics,
predictions) are typically returned in a 'tidy' data format (e.g., data frames/tibbles).
This makes it easier to inspect, combine, and visualize the outputs.
5. Extensibility
• The framework is designed to be extensible, allowing users to easily incorporate new
models, preprocessing steps, or evaluation metrics.
Benefits of the 'Tidy Models' Approach
Clarity and Readability
The code often reads like a recipe, clearly outlining each step of the modeling process.
Consistency
Reduces the cognitive load of learning different syntaxes for different models.
Reproducibility
The structured approach makes it easier to reproduce results and share modeling
workflows.
Robustness
Encourages best practices like robust evaluation through resampling.
Efficiency
Can streamline complex modeling tasks, especially when comparing multiple models or
tuning many hyperparameters.
Tidy Models: The whole game
Example (Conceptual) Workflow
Imagine you want to predict house prices:
Define a Recipe/ Preprocessing
Handle missing values in some columns.
One-hot encode categorical features like 'neighborhood' or 'house_type'.
Normalize numerical features like 'square_footage' or 'number_of_rooms'.
Specify a Model
Choose a Random Forest Regressor.
Declare tunable hyperparameters, e.g., trees = tune(), min_n = tune().
Create a Workflow
Combine the preprocessing recipe and the model specification into a single workflow object.
Resample Data:
Generate cross-validation folds from your training data.
Example (Conceptual) Workflow…
Tune Hyperparameters
Fit the workflow to each fold, trying different combinations of hyperparameters for the Random
Forest model.
Evaluate performance (e.g., RMSE) for each combination.
Select Best Model
Identify the best-performing set of hyperparameters.
Finalize the workflow with these optimal parameters.
Final Fit
Train the finalized workflow on the entire training dataset.
Evaluate
Make predictions on the unseen test set.
Calculate final performance metrics.
Tidyverse design Principles
The tidyverse has four guiding principles:
It is human centered, i.e. the tidyverse is designed specifically to support the
activities of a human data analyst.
It is consistent, so that what you learn about one function or package can be applied
to another, and the number of special cases that you need to remember is as small as
possible.
It is composable, allowing you to solve complex problems by breaking them down
into small pieces, supporting a rapid cycle of exploratory iteration to find the best
solution.
It is inclusive, because the tidyverse is not just the collection of packages, but it is also
the community of people who use them.
Packages of Tidyverse?
Package Purpose Analogy
rsample Data splitting and resampling (e.g., train/test sets, cross- Dividing your ingredients.
validation).
recipes Data preprocessing and feature engineering (scaling, centering, Preparing/chopping the
dummy variables). ingredients.
parsnip A unified interface for different models (Random Forest, Linear Choosing the appliance (oven,
Regression, etc.). stove).
workflows Bundles a recipe and a model together into a single object. Following a specific recipe card.
tune Optimizes model hyperparameters to find the best settings. Fine-tuning the cooking
time/temp.
yardstick Measures model performance (accuracy, RMSE, ROC curves). Tasting the final dish.
broom Converts messy model outputs into clean, "tidy" data frames. Cleaning up the kitchen.
Packages of Tidyverse?
Machine Learning
“Learning is any process by which a system improves performance from
experience. Herbert Simon
Machine Learning is an idea to learn from examples and experience, without
being explicitly programmed. Instead of writing code, you feed data to the
generic algorithm, and it builds logic based on the data given.
Data science, machine learning and artificial intelligence are some of the top
trending topics in the today’s tech world.
Data mining and Bayesian analysis are also in trending and this is adding the
demand for machine learning.
Mahine Learning…
The main purpose of machine learning is to explore and construct algorithms
that can learn from the previous data and make predictions on new input data.
ML is an automated learning with little or no human intervention.
It involves programming computers so that they learn from the available
inputs.
It is very difficult to consider all the decisions based on all possible inputs. To
solve this problem, algorithms are developed that build knowledge from a
specific data and past experience by applying the principles of statistical
science, probability, logic, mathematical optimization, reinforcement learning
and control theory.
Traditional Learning vs. Machine Learning
Traditional programming relies on hard- Machine Learning relies on learning
coded rules patterns based on sample data
Types of Learning
Supervised (inductive) learning
Data and corresponding labels are given
Unsupervised learning
Only data is given, no labels provided
Semi-supervised learning
Mix of supervised and unsupervised learning
Reinforcement learning
Rewards from sequence of actions
Supervised Learning
Task to arrange collection of fruits Supervised Learning:
• From previous work we know the physical characters of fruits
• In data mining terminology, the earlier work is called as training
the data.
• You already learn the things from your train data.
• This is because of response variable which is a decision variable.
Categories of Supervised learning
Supervised learning problems can be further divided into two parts, namely
classification, and regression.
Classification: A classification problem is when the output variable is a
category or a group, such as “black” or “white” and “spam” and “no spam”.
Regression: A regression problem is when the output variable is a real
value, such as “Rupees” or “height.
27-03-2026 Dr. Ashok Kumar 18
Supervised Learning Algorithm
• There are several algorithms available for supervised learning.
• Some of the widely used algorithms of supervised learning are as shown
below:
Logistic Regression
Naive Bayes Classifiers
K-NN (K-Nearest Neighbours)
Decision Trees
Support Vector Machine
27-03-2026 Dr. Ashok Kumar 19
Unsupervised Learning
In Unsupervised Learning, the machine uses unlabeled data and learns on itself
without any supervision.
the algorithms are left to themselves to discover interesting structures in the data.
Mathematically, unsupervised learning is when you only have input data (X)
and no corresponding output variables.
This is called unsupervised learning because unlike supervised learning above,
there are no given correct answers and the machine itself finds the answers.
Unsupervised Learning
Suppose it is given an image having both dogs and cats which have not seen ever.
• The machine has no idea about the features of dogs and cats.
• Therefore, we can’t categorize it as ‘dogs and cats’.
• These can be categorized according to their similarities, patterns, and
differences, i.e., we can easily categorize the above picture into two parts.
• The first may contain all pics having dogs in them and the second part may
contain all pics having cats in them.
• Here you didn’t learn anything before, which means no training data or
examples.
• It allows the model to work on its own to discover patterns and information that
was previously undetected. It mainly deals with unlabeled data.
Categories of Unsupervised learning
Unsupervised learning problems can be further divided into aossciation
and clustering problems.
Association: An association rule learning problem is where you want to
discover rules that describe large portions of your data, such as “people that
buy X also tend to buy Y”.
Clustering: A clustering problem is where you want to discover the
inherent groupings in the data, such as grouping customers by purchasing
behaviour.
27-03-2026 Dr. Ashok Kumar 22
Unsupervised Learning Algorithm
• There are several algorithms available for unsupervised learning.
• Some of the widely used algorithms or techniques of unsupervised
learning are given below:
✓ K-means clustering
✓ Hierarchical clustering
✓ Principal Component Analysis
✓ Singular Value Decomposition
✓ Independent Component Analysis
27-03-2026 Dr. Ashok Kumar 23
Performance Metrics for Regression Models
Regression Metrics
In the regression task, we are supposed to predict the target variable which is in the
form of continuous values.
To evaluate the performance of such a model below metrics are used:
Mean Absolute Error (MAE)
1
𝑀𝐴𝐸 = σ𝑛𝑖=1 𝑦 − 𝑦ො𝑖
𝑛
Mean Squared Error (MSE)
1
𝑀𝑆𝐸 = σ𝑛𝑖=1 𝑦 − 𝑦ො𝑖 2
𝑛
R² (R-squared)
σ𝑛
𝑖=1 𝑦−𝑦ො𝑖 2
𝑅2 = 1 − σ𝑛 ത 2
𝑖=1 𝑦−𝑦
Confusion Matrix
Confusion Matrix is used to know the performance of a Machine learning
classification.
It is represented in a matrix form, N x N matrix, where N is the number of
classes or outputs.
Confusion Matrix gives a comparison between actual and predicted values. It
contains the count of observations that fall in each category.
Performance Metrics for Classification Models
Performance Evaluation Measures for Classification Models
Accuracy
Precision
Recall/ Sensitivity
Specificity
F1-Score
AUC & ROC Curve
Accuracy
Ratio of correct predictions to total predictions.
Accuracy = (TP + TN) / (TP + FP +TN + FN)
The best accuracy is 100% indicating that all the predictions are correct.
For an imbalanced dataset, accuracy is not a valid measure of model performance.
Sensitivity/Recall
The ratio of true positives to total (actual) positives in the data.
Sensitivity or Recall = TP/(TP+FN)
Important when: identifying the positives is crucial.
Recall or sensitivity gives us information about a model’s performance on false
negatives (incorrect prediction of customers who will default).
27 March 2026 Dr. Ashok Kumar 28
Precision
The ratio of true positives to total predicted positives.
Precision = TP/(TP+FP)
Important when: you want to be more confident of your predicted positives.
Specificity
• The ratio of true negatives to total negatives in the data.
Specificity = TN/(TN+FP)
• Important when: you want to cover all true negatives.
Thank you
☺☺☺