Preface
Data science begins before the first line of code is written and does not end when a model produces a prediction. It starts with a problem worth understanding and continues through decisions about what data to use, how to explore and prepare them, which methods to apply, how to evaluate the results, and what conclusions can reasonably be drawn. Yet these elements are often learned separately: statistics in one setting, programming in another, and machine learning as a collection of algorithms. The challenge is not simply to master each tool, but to understand how they work together to turn data into meaningful insights and well-reasoned decisions.
Data Science Foundations and Machine Learning with R: From Data to Decisions is built around this integrated view of data science. Designed for readers with little or no prior experience in programming, statistics, or machine learning, the book brings together statistical reasoning, data understanding, data preparation, machine learning, model evaluation, and reproducible analysis in R. Its aim is to help readers understand not only how data science methods are implemented, but also why they are used, how their results should be interpreted, and how analytical choices affect the conclusions that can reasonably be drawn.
Motivation and Scope
Many introductory resources emphasize either statistical theory, programming skills, or machine learning algorithms. In practice, however, effective data science requires these elements to work together. Analysts must be able to formulate meaningful questions, understand the data available to address them, make appropriate analytical choices, evaluate the resulting models or evidence, and interpret the results in context. This book is motivated by the need for an introduction that develops these skills as parts of a coherent analytical process rather than as isolated topics.
The book therefore combines statistical reasoning, data exploration and visualization, data preparation, machine learning, model evaluation, and reproducible analysis in R. The emphasis is not simply on learning how to execute a method, but on understanding why it is appropriate, what choices and assumptions it involves, and how its results should be interpreted. R serves throughout as a practical environment for implementing these ideas and developing transparent and reproducible analyses.
This integrated perspective is organized around the seven-stage Data Science Workflow introduced in Chapter 1 Introduction to Data Science and illustrated in Figure 1.1. The workflow provides a common framework connecting Problem Definition, Data Acquisition, Data Understanding and Exploration, Data Preparation for Modeling, Modeling, Model Evaluation, and Communication and Deployment. It is not intended as a rigid sequence: data science is inherently iterative, and findings at one stage may require returning to earlier stages and reconsidering previous decisions.
The scope of the book is introductory but broad. Particular emphasis is placed on Data Understanding and Exploration, Data Preparation for Modeling, Modeling, and Model Evaluation, together with the statistical foundations needed to reason about uncertainty and interpret results. The modeling chapters cover core approaches to classification, regression, and unsupervised learning, ranging from classical statistical models to modern machine learning methods. Problem Definition, Data Acquisition, and Communication and Deployment are treated as essential parts of the broader workflow, but are covered less extensively.
The goal is not to survey every branch of data science or every available algorithm. Instead, the book aims to provide a coherent foundation from which readers can understand, apply, evaluate, and critically interpret commonly used data science and machine learning methods, and continue confidently to more specialized topics.
Audience and Prerequisites
This book is intended primarily for undergraduate and early graduate students taking an introductory course in data science, machine learning, business analytics, applied statistics, econometrics, or quantitative methods. It is particularly suitable for courses that combine practical data analysis with an understanding of the statistical and computational ideas underlying the methods. The book can also be used by professionals, researchers, and self-study readers seeking a practical foundation in data science and machine learning with R.
No prior experience with R or programming is assumed. Appendix Appendix A: R Foundations for Data Science introduces R, RStudio, fundamental programming concepts, data structures, data import and manipulation, visualization, and reproducible reporting. Readers who are new to R are encouraged to study the relevant parts of the appendix before beginning the main chapters or consult them as needed throughout the book.
A formal background in statistics is not required. Statistical concepts are introduced as they become relevant, with Chapter 7 Statistical Inference for Data Science providing a focused foundation in statistical inference before these ideas are developed further in the regression chapters. Familiarity with basic mathematics is helpful, but the emphasis throughout is on conceptual understanding, interpretation, and application rather than mathematical derivation.
Learning Goals
This book is designed to develop both conceptual understanding and practical skills in data science and machine learning. The learning goals reflect the progression of a data science investigation, from formulating a problem and understanding the data to preparing data for modeling, building and evaluating models, and interpreting the results.
By the end of the book, readers will be able to:
formulate analytical questions and relate them to the stages of the Data Science Workflow, from Problem Definition through Communication and Deployment;
understand and explore data by identifying feature types, examining distributions and relationships, and investigating missingness, unusual observations, and other important data characteristics;
prepare data appropriately for modeling by applying principles of data partitioning, resampling, leakage prevention, missing-value handling, feature preparation, and class imbalance;
apply fundamental ideas from statistical inference to reason from samples to populations, quantify uncertainty, and interpret confidence intervals and hypothesis tests;
build and tune supervised and unsupervised learning models for classification, regression, and clustering, and evaluate their performance using appropriate measures and valid assessment strategies;
interpret analytical results critically and communicate findings, uncertainty, limitations, and practical implications in ways that support well-reasoned decisions;
use R throughout these tasks to implement reproducible data analyses, from data exploration and preparation to modeling, evaluation, and communication.
Together, these goals emphasize not only how data science methods are implemented, but also when and why they should be used, how they should be evaluated, and what conclusions can reasonably be drawn from their results.
Pedagogical Approach
The book is designed to support active learning in self-study, classroom instruction, and professional training. Although individual chapters may be consulted for specific topics, the main chapters are arranged so that concepts and methods build progressively on one another. The book is intended to be used actively rather than read passively.
New ideas are introduced through explanation and examples and then reinforced through annotated R code, in-text Practice tasks, applied analyses, and end-of-chapter exercises. Readers are encouraged to run the code, inspect intermediate results, modify examples, and explore how analytical results change under alternative choices of variables, preprocessing decisions, parameter settings, or models. These activities are intended to strengthen both conceptual understanding and practical fluency and to help readers develop increasing independence in carrying out data analyses.
Throughout the book, data analysis is presented as a process of making and evaluating decisions rather than following a fixed sequence of commands. Readers are encouraged to consider why a particular method is appropriate, what assumptions and limitations accompany it, and how its results should be interpreted. Reproducibility is treated as an integral part of this analytical reasoning: the goal is not simply to obtain a result, but to produce an analysis whose steps can be understood, checked, and reproduced.
Structure of This Book
The main text consists of twelve chapters organized around the Data Science Workflow introduced in Chapter 1 Introduction to Data Science. The sequence moves from the overall analytical process and the understanding and preparation of data to classification, model evaluation, statistical inference, regression, and additional supervised and unsupervised learning methods. R foundations are provided separately in Appendix Appendix A: R Foundations for Data Science, allowing readers who already know R to begin directly with the main chapters while providing beginners with the programming foundation needed throughout the book.
Chapters 1 Introduction to Data Science through 3 Data Preparation for Modeling establish the workflow and the foundations for working with data. Chapter 1 Introduction to Data Science, Introduction to Data Science, presents data science as an iterative process organized around Problem Definition, Data Acquisition, Data Understanding and Exploration, Data Preparation for Modeling, Modeling, Model Evaluation, and Communication and Deployment. It also positions machine learning within this broader process and introduces different analytical goals, including prediction, explanation, and discovering structure in data.
Chapter 2 Data Understanding and Exploration, Data Understanding and Exploratory, focuses on learning what the available data contain before modeling decisions are made. It develops the use of feature types, numerical summaries, visualization, and exploratory analysis to investigate distributions, relationships, missingness, and unusual or implausible observations.
Chapter 3 Data Preparation for Modeling, Data Preparation for Modeling, builds on this understanding by determining how data should be prepared for valid model development and evaluation. It introduces data partitioning and resampling, leakage prevention, the training-only principle, handling missing and unusual values, preparing categorical and numerical features, and addressing class imbalance. The distinction between Chapters 2 Data Understanding and Exploration and 3 Data Preparation for Modeling is deliberate: Data Understanding identifies and investigates important characteristics of the data, whereas Data Preparation determines how those characteristics should be handled for modeling.
Chapters 4 Classification Using k-Nearest Neighbors through 6 Naive Bayes introduce classification and model assessment. Chapter 4 Classification Using k-Nearest Neighbors, Classification Using k-Nearest Neighbors, provides an accessible introduction to supervised machine learning, including feature representation and model tuning. Chapter 5 Model Evaluation, Model Evaluation, develops the principles and measures needed to evaluate predictive models appropriately. Chapter 6 Naive Bayes, Naive Bayes, introduces a probabilistic approach to classification.
Chapter 7 Statistical Inference for Data Science, Statistical Inference for Data Science, introduces reasoning from samples to populations and the quantification of uncertainty. It develops concepts including sampling variability, estimation, confidence intervals, hypothesis testing, and common statistical tests. Its position before the regression chapters provides the inferential foundation needed for understanding uncertainty and drawing population-level conclusions in the methods that follow.
Chapters 8 Regression Analysis and 9 Generalized Linear Models develop regression-based modeling. Chapter 8 Regression Analysis, Regression Analysis, examines relationships between numerical outcomes and explanatory variables using linear and nonlinear regression approaches. Chapter 9 Generalized Linear Models, Generalized Linear Models, extends regression modeling to binary and count outcomes for which ordinary linear regression is not appropriate.
Chapters 10 Decision Trees and Random Forests through 12 Clustering Using K-means broaden the modeling toolkit. Chapter 10 Decision Trees and Random Forests, Decision Trees and Random Forests, develops tree-based approaches for prediction. Chapter 11 Neural Networks, Neural Networks, introduces the principles and application of neural networks for supervised prediction. Chapter 12 Clustering Using K-means, Clustering Using K-means, turns to unsupervised learning and examines how observations can be grouped when predefined outcome labels are unavailable.
Appendix Appendix A: R Foundations for Data Science, R Foundations for Data Science, provides the R knowledge needed throughout the book, including the R environment, fundamental programming concepts, data structures, data import and manipulation, visualization, and reproducible reporting. Readers new to R can study the appendix before beginning the main text, while readers with prior experience can use it as a reference when needed.
For Instructors
The book is designed to support flexible course organization. Instructors may follow the chapters in sequence for a comprehensive introductory course or select particular chapters to match the learning objectives, available teaching time, and background of their students. The book can therefore serve as either a primary text or a supplementary resource in courses involving data science, machine learning, business analytics, applied statistics, or related quantitative subjects.
The placement of R foundations in Appendix Appendix A: R Foundations for Data Science provides additional flexibility. For students with little or no prior R experience, relevant material from the appendix can be incorporated at the beginning of the course or assigned alongside the main chapters. Students who are already comfortable with R can proceed directly to the main material and consult the appendix as needed.
The examples, case studies, Practice tasks, and end-of-chapter exercises can be adapted for lectures, computer practicals, homework, assessment, and project-based learning. Their range allows instructors to combine conceptual understanding, programming practice, and applied analytical reasoning according to the aims of a particular course.
Additional teaching materials, including lecture slides, practical data science projects, and assessment resources, are available to support course preparation and delivery. Information about instructor resources, supplementary materials, and updates is available on the book’s companion website: https://datasciencebook.ai.
Software, Data, and Online Resources
The book uses R for data analysis, visualization, statistical inference, and machine learning, with RStudio providing a convenient environment for working with the code and examples. Instructions for installing and getting started with R and RStudio are provided in Appendix Appendix A: R Foundations for Data Science.
Real-world datasets are used throughout the book to support hands-on learning and reproducible analysis. They provide practical settings for illustrating concepts, developing examples and case studies, and completing exercises across areas such as customer analytics, finance, marketing, healthcare, and other applied domains.
Most datasets used in the book are available through the liver R package. The package allows readers to reproduce the analyses in the book and provides supporting functions for selected data science tasks. Datasets can be loaded directly in R using the data() function, and the package documentation provides descriptions of variables and references to original sources where applicable. Documentation for the liver package is available through CRAN at https://cran.r-project.org/web/packages/liver/index.html.
The book’s companion website, https://datasciencebook.ai, provides supplementary materials, updates, and additional resources for readers and instructors. It also serves as a central location for information related to the book and its supporting materials.
For readers interested in learning the same core concepts using Python, the companion volume Data Science Foundations and Machine Learning with Python: From Data to Decisions provides a Python-based treatment of the material. Further information about both books is available on the companion website.
Acknowledgments
Writing this book has been a demanding and rewarding process, and I am grateful to the many people who supported its development. First, I thank my wife, Pariya, for her patience, encouragement, and constant support throughout this project. I am also deeply grateful to my family, especially my mother and older brother, for their belief in me.
I am especially thankful to Eva Hiripi at Springer for her support and encouragement from the early stages of this book. I also thank Dr. Kevin Burke for his valuable input on the structure of the book and Dr. Jeroen van Raak for his collaboration on the Python edition. I am grateful to Julien Rossi and Dr. Noud van Giersbergen for their thoughtful and constructive feedback on earlier versions of the manuscript, which helped improve its clarity, structure, and overall quality.
I am grateful to my colleagues in the Business Analytics Section at the University of Amsterdam for their feedback, encouragement, and academic support during the writing process. In particular, I thank Prof. Ilker Birbil, Prof. Dick den Hertog, Prof. Marc Salomon, Dr. Marit Schoonhoven, Prof. Joaquim Gromicho, Prof. Stevan Rudinac, Dr. Rob Goedhart, Prof. Jeroen de Mast, Prof. Peter Kroos, Dr. Chintan Amrit, Dr. Inez Zwetsloot, Dr. Alex Kuiper, Dr. Bart Lameijer, Dr. Jannis Kurtz, Dr. Guido van Capelleveen, and Dr. Yeqiu Zheng. I also thank my PhD students, Lucas Vogels and Elias Dubbeldam, for their research insights and continued collaboration.
I further acknowledge my former colleagues and co-authors, Dr. Khodakaram Salimifard, Sara Saadatmand, and Dr. Florian Böing-Messing, for their continued academic partnership. Finally, I am grateful to the students of the courses Data Wrangling and Data Analytics: Machine Learning at the University of Amsterdam. Their questions, feedback, and engagement helped refine the material in meaningful ways. I am particularly thankful to John Gatev for his thoughtful and constructive comments.
Reza Mohammadi,
Amsterdam, Netherlands,
August 2026