That’s weird! Anomaly detection using R

Author
Affiliation

Monash University, Australia

Preface

This book is under development, and the last few chapters are incomplete. If you find any errors, please report them on the GitHub issues page.

This book is about tools and techniques for finding and understanding anomalies. It begins with some simple data sets containing only one variable, and builds up slowly to much more complicated data. It covers popular but inadvisable methods to identify anomalies (pointing out their shortcomings), as well as more reliable and recommended approaches.

The book is written for two audiences: (1) people finding themselves doing data analysis without having had formal training in anomaly detection; and (2) students studying statistics or data science.

You will need a knowledge of statistics and probability at about second-year undergraduate level. So if you’ve done a couple of statistics subjects, and are familiar with multiple regression, hypothesis tests and probability distributions, then you should be prepared for what follows. This book takes a probabilistic approach to anomaly detection, and so probability distributions are particularly important. This is the subject of Chapter 2 and Chapter 3. You will also need some knowledge of matrix algebra; the most important matrix results that we use are provided in Appendix A.

Finally, you will need to know how to use R and be familiar with the tidyverse packages such as dplyr, tidyr and ggplot2. There are many online resources that cover this material, including Wickham et al. (2023) and the StartR self-learning modules developed at Monash University.

All R examples in the book assume that you have loaded the weird package first. You will need to use the development version, currently available on GitHub.

#pak::pak("robjhyndman/weird")
library(weird)
── Attaching core weird packages ─────────────────────────── weird 3.1.0.9000 ──
✔ distributional 0.9.0     ✔ ggplot2        4.0.3
✔ dplyr          1.2.1     
── Conflicts ──────────────────────────────────────────────── weird_conflicts ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()

This loads the relevant data sets, and various other packages needed to reproduce the examples.

 

Rob J Hyndman
October 2026