Book Image

Simulation for Data Science with R

By : Matthias Templ
Book Image

Simulation for Data Science with R

By: Matthias Templ

Overview of this book

Data Science with R aims to teach you how to begin performing data science tasks by taking advantage of Rs powerful ecosystem of packages. R being the most widely used programming language when used with data science can be a powerful combination to solve complexities involved with varied data sets in the real world. The book will provide a computational and methodological framework for statistical simulation to the users. Through this book, you will get in grips with the software environment R. After getting to know the background of popular methods in the area of computational statistics, you will see some applications in R to better understand the methods as well as gaining experience of working with real-world data and real-world problems. This book helps uncover the large-scale patterns in complex systems where interdependencies and variation are critical. An effective simulation is driven by data generating processes that accurately reflect real physical populations. You will learn how to plan and structure a simulation project to aid in the decision-making process as well as the presentation of results. By the end of this book, you reader will get in touch with the software environment R. After getting background on popular methods in the area, you will see applications in R to better understand the methods as well as to gain experience when working on real-world data and real-world problems.
Table of Contents (18 chapters)
Simulation for Data Science with R
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Preface

"Everybody seems to think I'm lazy

I don't mind, I think they're crazy

Running everywhere at such a speed

Till they find there's no need (There's no need)"

The Beatles in their song "I'm only sleeping"

The Monte Carlo way and simulation approach are ways to stay lazy and efficient at the same time. "Lazy", since a simulation approach is generally much easier to carry out as compared to an analytical approach—there is mostly no need for analytical approaches, and one might be crazy to neglect the whole world of statistical simulation. "Efficient", since it costs minimal efforts to get reliable results, and often simulation is the only approach to get results. The simulation approach in data science and statistics is generally a more intuitive approach compared to analytical solutions. It is not hidden behind a wall of mathematics, and using a simulation approach is often the only way to solve complex problems.

Statistical simulation has thus become an essential area in data science and statistics. It can be seen as a data-driven approach to many practical problems in data science and statistics.

In this book, theory is also explained with illustrative examples using the software environment R, for which advanced data processing features are shown in the book.

This book will thus provide a computational and methodological framework for statistical simulation to users with a computational statistics and/or data science background.

More precisely, the aim of this book is to lay into the hands of the readers a book that explains methods, give advice on the usage of the methods, and provide computational tools to solve common problems in statistical simulation and computer-intense methods.

The core issues are on simulating distributions and datasets, Monte Carlo methods for inference statistics, microsimulation and dynamical systems, and presenting solutions using computer-intense approaches. You will see applications in R not only to better understand the methods but also to gain experience when working on real-world data and real-world problems.

The author of the book has tried to make humorous and amusing examples in certain chapters in order to increase interest, staying catchy and memorable. Next to serious text on methods, curious examples on individual mortality and fertility rates of the author of the book are also present as is the system dynamics from the love/hate story of Prince Henry and Chelsy Davy, the Australian guy in the Austrian mountain trying to reach the highest mountain through an optimization problem, or the weak law of winning the lottery are presented as well.

What this book covers

Chapter 1, Introduction, discusses the general aim of simulation experiments in data science and statistics, why and where simulation is used, and the special case of dealing with big data.

Chapter 2, R and High-Performance Computing, consists of comprehensive text on advanced computing, data manipulation, and visualization with R.

Chapter 3, The Discrepancy between Pencil-Driven Theory and Data-Driven Computational Solutions, reports problems on numerical precision, rounding, and convergence in a deterministic setting.

Chapter 4, Simulation of Random Numbers, starts with the simulation of uniform random numbers and transformation methods to obtain other kinds of distributions. It includes a discussion of various types of Markov chain Monte Carlo (MCMC) methods.

Chapter 5, Monte Carlo Methods for Optimization Problems, introduces deterministic and stochastic optimization methods.

Chapter 6, Probability Theory Shown by Simulation, has a strong focus on basic theorems in statistics; for example, the concept of the weak law of large numbers and the central limit theorem are shown by simulation.

Chapter 7, Resampling Methods, is a comprehensive view on the bootstrap, the jackknife and cross-validation.

Chapter 8, Applications of Resampling Methods and Monte Carlo Tests, shows applications in various fields such as regression, imputation, and time series analysis. In addition, Monte Carlo tests and their variants such as permutation tests and bootstrap tests are presented.

Chapter 9, The EM Algorithm, introduces the expectation maximum method to iteratively obtain an optima. Applications in clustering and imputation of missing values are given.

Chapter 10, Simulation with Complex Data, shows how to simulate synthetic data as well as population data that can be used for the comparison of methods in general or also serve as input for agent-based microsimulation models.

Chapter 11, System Dynamics and Agent-Based Models, discusses agent-based microsimulation models and shows basic models in system dynamics to study complex dynamical systems.

What you need for this book

This book heavily depends on the software environment R, version 3.2 or newer (https://cran.r-project.org/). In most chapters, independent and standalone code is written to show methods and execute examples, and no additional packages of R are needed. For a few chapters, additional R packages such as deSolve, cvTools, laeken, VIM, and few others must be installed within R. The packages dplyr and ggplot2 are used throughout the book.

Optionally, the use of a script editor for R, such as RStudio (https://www.rstudio.com/) or Architect + Eclipse (https://www.openanalytics.eu/architect), is recommended.

Who this book is for

This book is for users who are familiar with computational methods and R. If you want to learn about the advanced features of R, along with computer-intense Monte Carlo methods and tools for statistical simulation, and if you prefer data-driven solutions, then this book is for you.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

R code words in text, filenames, file extensions, pathnames, and dummy URLs are shown as follows:

A block of code is set as follows:

[default]
love <- function(t, x, parms){
  with(as.list(c(parms, x)), {
    dPrince_Harry <- a * Chelsy_Davy
    dChelsy_Davy <- -b * Prince_Harry
    res <- c(dPrince_Harry, dChelsy_Davy)
    list(res)
  })
}

Any command-line input or output is written as follows:

dat <- matrix(c(104,11037,189,11034),2,2, byrow=TRUE)
## Loading required package: grid
> confint(oddsratio(dat, log=FALSE))
##     2.5 %    97.5 %
##  0.4324132 0.6998549

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Simulation-for-Data-Science-with-R. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/SimulationforDataSciencewithR_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.