Automatic behavior monitoring technologies are becoming part of our everyday lives thanks to advances in sensors and machine learning. The automatic analysis and understanding of behavior are being applied to solve problems in several fields, including health care, sports, marketing, ecology, security, and psychology, to name a few. This book provides a practical introduction to machine learning methods applied to behavior analysis with the R programming language. No previous knowledge in machine learning is needed. You should be familiar with the basics of R and some knowledge in basic statistics and high school-level mathematics would be beneficial. Even though the exercises focus on behavior analysis tasks, the covered machine learning underlying concepts and methods can be easily applied in any other domain.

Supplemental Material

The supplemental material consists of examples’ code, shiny apps, and datasets. The source code for the examples and the shiny apps can be downloaded from Instructions on how to set up the code, run shiny apps, and get the datasets are in Appendix A. A reference for all the utilized datasets is in Appendix B.


DATASET names are written in uppercase italics. Functions are referred to by their name followed by parenthesis (omitting their arguments), for example: myFunction(). Class labels are written in italics and between single quotes: ‘label1’. The following icons are used to provide additional contextual information:

Provides additional information and notes.
Important information to consider.
Provides tips and good practice recommendations.
Lists the R scripts and files used in the corresponding section.
Interactive shiny app available. Please see Appendix A for instructions on how to run shiny apps.

The folder icon will appear at the beginning of a section (if applicable) to indicate which scripts were used for the corresponding examples.


I want to thank Ketil Stølen and Robert Kabacoff who reviewed the book and gave me valuable suggestions.

I want to thank Michael Riegler, Darlene E., Jaime Mondragon y Ariana, Viviana M., Linda Sicilia, Ania Aguirre, Gagan Chhabra, Annie S., Anton Aguilar, Aleksander Karlsen, 刘爽, Ragnhild Halvorsrud, Tine Nordgreen, Petter Jakobsen, Jim Tørresen, my former master’s and PhD. advisor Ramon F. Brena, and my former colleagues at University of Oslo and SINTEF.

I want to thank Vance Capley who brought to life the front cover and comic illustrations, Francescoozzimo who drew the comic for chapter 10, and Katia Liuntova who animated the online front cover. The examples in this book rely heavily on datasets. I want to thank all the people that made all their datasets used here publicly available. Thanks to Yihui Xie who developed the bookdown R package with which this book was written.

Thanks to Rob Calver, Vaishali Singh, and the CRC Press team who helped me during the publishing process.

Thanks to Simon Greaves for spotting an error in chapter 1.

I want to thank all the music bands I listened to during my writing-breaks: Lionheart, Neaera, Hatebreed, Sworn Enemy, Killswitch Engage, As I Lay Dying, Lamb of God, Himsa, Slipknot, Madball, Fleshgod Apocalypse, Bleeding Through, Caliban, Chimaira, Heaven Shall Burn, Darkest Hour, Demon Hunter, Frente de Ira, Desarmador, Después del Odio, Gatomadre, Rey Chocolate, ill niño, Soulfly, Walls of Jericho, Arrecife, Corcholata, Amon Amarth, Abinchova, Fit for a King, Annisokay, Sylosis, Meshuggah.