Exploring Operations Research with R cover

395 pages · 2024

Catalog title1st Edition

Exploring Operations Research with R

By Jim Duggan

Published
May 21, 2024
Imprint
Chapman and Hall/CRC
Edition
1st Edition
ISBN
9781003293910

Overview

About this book

Exploring Operations Research with R shows how the R programming language can be a valuable tool – and way of thinking – which can be successfully applied to the field of operations research (OR). This approach is centred on the idea of the future OR professional as someone who can combine knowledge of key OR techniques (e.g., simulation, linear programming, data science, and network science) with an understanding of R, including tools for data representation, manipulation, and analysis. The core aim of the book is to provide a self-contained introduction to R (both Base R and the tidyverse) and show how this knowledge can be applied to a range of OR challenges in the domains of public health, infectious disease, and energy generation, and thus provide a platform to develop actionable insights to support decision making.

Features

Can serve as a primary textbook for a comprehensive course in R, with applications in OR Suitable for post-graduate students in OR and data science, with a focus on the computational perspective of OR The text will also be of interest to professional OR practitioners as part of their continuing professional development Linked to a Github repository including code, solutions, data sets, and other ancillary material

Subjects

Computer Science, Engineering & Technology, Mathematics & Statistics

Inside the book

Table of contents

Select a chapter to read its abstract.

Part IBase R

Pages: 132

No chapter abstract available.

Chapter 1Getting Started with R

Pages: 6

This chapter provides a short introduction to R, and presents a number of examples of R in action. The aim is to demonstrate the utility of entering commands via R';s console, in order to create variables, perform simple arithmetic operations, and display the results. A number of R functions are introduced, including the c() function for defining a vector. The mechanism to download libraries from the Comprehensive R Archive Network (CRAN) is shown. The chapter also provides a brief summary of two statistical functions that are used throughout the book, namely simple linear regression using lm(), and correlation with the cor() function.

Chapter 2Vectors

Pages: 26

In R, knowledge of vectors is important. A vector is a one-dimensional data structure, and there are two types of vectors: (1) atomic vectors and (2) lists. An atomic vector is a simpler data structure, in that every element must have the same type. A list is more flexible, and elements can be of different types, and indeed a list can also contain another list, which provides the functionality to capture hierarchical data structures. The chapter describes the four main types of atomic vector, how to create large vectors, and how R deals with missing values. It then introduces the idea of vectorisation, where a function will operate in an element-wise manner on an atomic vector. The second type of vector – a list – is then introduced, and mechanisms to create and name lists are presented. A mini-case is presented showing how atomic vectors can be used to gather outcomes from the simulated rolling of two dice.

Chapter 3Subsetting Vectors

Pages: 24

R provides a range of flexible approaches that can be used to subset data contained in atomic vectors and lists. This chapter presents important subsetting operations, and knowledge of these is key in terms of understanding later chapters on list and data frame processing. Starting with atomic vectors, the four main ways to subset an atomic vector are listed: by positive integer, using the minus sign, with a logical vector, and using the element name. It then summarises the three different operators uses for accessing lists. R's looping structure is introduced, and then used to process list elements contained in the CRAN package repurrrsive.

Chapter 4Functions, Functionals, and the R Pipe

Pages: 24

Functions are building blocks in R, and are small units that can take an input, process it, and return a result. While R already contains many functions, having the knowledge to create functions is essential to getting the most from R. This chapter explains how to write your own function, call it via arguments, and store the result in a variable. It highlights how arguments can be passed to functions in different ways, and how to adopt a “fail fast “approach for error checking. Environments are introduced, which provide the mechanism for R to find variables, and are also important in understanding functions. The chapter also introduces the idea of a function – a function that accepts a function as an argument, and explores the classic R functional known as lapply(). Finally, the chapter presents a recent innovation in base R known as the native pipe operator, as this provides a valuable mechanism to chain operations together in a data pipeline, in order to improve code clarity.

Chapter 5Matrices and Data Frames

Pages: 26

Both matrices and data frames provide a structure to store rectangular data, which is the usual format found in spreadsheets or database tables. Typically, in a rectangular dataset, every column represents a variable, and each row contains an observation, for example, a set of values that are related (weather variables at a specific point in time). Matrices are used to store data of the same type, while data frames can store data of different types. This chapter introduces the matrix and the data frame, and shows how we can process data in these structures, using ideas from subsetting atomic vectors and lists. It explains how functionals can be applied to matrices and data frames, including the apply() functional, which can iterate over rows or columns. Two mini-cases are presented: the first shows how a matrix can be used to store information on a social network structure, and that can capture connections between individuals, and the second highlights how a processing pipeline, using R's native pipe operator, can be used to process a data frame.

Chapter 6The S3 Object System in R

Pages: 24

S3 is an object-oriented system in R. It is widely used in base R, and also in other packages. It is built on the idea of a generic functions, which provides a clever mechanism to hide specific implementation details from the user when functions are called. Classic examples of generic functions include summary(), print() and plot(). The chapter introduces the idea of attributes, which are metadata that can be associated with any variable. It explains how attributes are used by the S3 system to implement the generic function approach, using a mechanism known as method dispatch. The approach for writing custom generic functions is shown, and more advanced features such as inheritance are presented. A mini-case showing how to implement a simple queue system concludes the chapter.

Part IIThe tidyverse and Shiny

Pages: 132

No chapter abstract available.

Chapter 7Visualization with ggplot2

Pages: 30

A key element of any data analysis and modelling process is to have the facility to visualise data, and explore relationships between variables. Within R's tidyverse we are fortunate to have access to a power visualisation library ggplot2. There are three important benefits of using ggplot2: (1) plots can be designed in a layered manner; (2) a wide range of informative plots can be generated to support decision analysis (e.g. including scatterplots, histograms and time series charts), and (3) charts can be developed rapidly, and which supports the process of exploratory data analysis. This chapter starts with a simple scatterplot, and shows how this can be extended to provide information on multiple variables. Statistical transforms are introduced, as they are used to generate plots such as bar charts, box plots and histograms. Features such as themes are introduced, as these allow the analyst to control non-data elements of a plot. The chapter concludes with a mini-case showing how ggplot can be used to visualise and format time series data.

Chapter 8Data Transformation with dplyr

Pages: 26

While visualisation of data from a tibble is an important way to explore and gain insights from data, any given tibble may not contain all the data that is required, and for this, further transformation may be needed. The package dplyr provides a set of functions that support tibble transformations. These functions are often called “the five verbs of dplyr”, where each function accepts a tibble and a number of arguments, and then returns a tibble. This provides an elegant architecture that is ideal for the use of tools such as the R and tidyverse pipe operators. This chapter introduces the five verbs of dplyr, which are filter(), arrange(),select(), mutate(), and summarise(). Each of these support specific tasks, for example, filter() selects rows from a tibble, and mutate() allows you to add new columns. The chapter concludes with a mini-case that shows how dplyr functions can be used to compute summary rainfall information from the package aimsir17.

Chapter 9Relational Data with dplyr and Tidying Data with tidyr

Pages: 24

When analysing data, there is often more than one tibble of interest, and also, tibbles may share common information. The challenge then is to find ways to combine information from one or more tibble, and this is known as a join operation. The package dplyr provides a set of functions to allow us to join data from tibbles. There are two types joins: mutating joins, which add new columns to a tibble, and filtering joins, which do not change the tibble columns. This chapter provides an overview of how we can join data from two or more tibbles. It describes the main idea underlying a relational model, and introduces the four main types of mutating joins. Following that, filtering joins are summarises, along with the idea of tidy data, and how the tools of tidyr can be used to lengthen and widen data. The chapter concludes with a case study based on the CRAN package aimsir17, where two tibbles are joined to explore the possible relationship between windspeed and wind energy generation.

Chapter 10Processing Data with purrr

Pages: 30

The package purrr provides a comprehensive set of functions that can be used to iterate over data structures, for example: vectors, lists, data frame and tibbles. Many of these purrr functions are known as functionals, as their arguments include (1) a data structure, and (2) a function (usually anonymous) to operate on the data. This chapter introduces key functions from purrr, with practical examples highlighting their use. The functions include the list processing function map(), extensions of map for specific outputs, for example, map_double() and map_df(). A powerful feature of purrr is that it can be integrated within a workflow that processes tibbles, for example, by using functions such as group_split() from dplyr and nest() from tidyr. An informative mini-case is presented that highlights how purrr can be used to support a statistical modelling workflow, based on two variables from a fuel efficiency dataset.

Chapter 11Shiny

Pages: 20

The Shiny system in R provides an innovative framework where interactive web pages can be created using R functions. These pages can be designed to take inputs from users through a web browser, and then process these in order to produce a range of dynamic outputs, including text summaries, tables and visualisations. Shiny is fully integrated with R code, and enables visualisation of plots and data summaries on a web page. The Shiny system follows a reactive programming approach, which is similar to the way formulas are recalculated in a spreadsheet. A number of input controls can be used, including slider bars, drop-down menus and checkboxes. The chapter presents six examples to highlight how Shiny can be used, starting with a simple web page to print a welcome message, and concluding with an interactive ggplot based on data stored in a tibble.

Part IIIUsing R for Operations Research

Pages: 102

No chapter abstract available.

Chapter 12Exploratory Data Analysis

Pages: 28

Exploratory Data Analysis (EDA) provides a valuable guiding framework analysing a data set. The idea is to review features and characteristics of data with an open mind, and a frequently uses approach is to define a set of questions to guide the process. An attractive feature of EDA is that there are no constraints on the type of question you can ask, and therefore it can be viewed as a creative process. This chapter provides an overview of EDA, and makes use of the tidyverse packages ggplot2, dplyr and tidyr. The five examples used are: exploring whether a plant's measurements can help to uniquely identify a species of iris; the relationship between temperature and electricity demand in Australia; exploring factors that may have impacted pupil-teacher ratios in 1970s Boston; survival analysis of passengers on the M.V. Titanic; and exploring the possible influence of wind direction on temperature in Ireland.

Chapter 13Linear Programming

Pages: 22

Linear programming is a decision making tool that is primarily focused on the general problem of allocating limited resources among several competing activities. Its success as a method arises from its flexibility in solving problems across different domains. The linear programming method uses a mathematical model to describe the problem of interest, and this model is then solved or computational techniques. This chapter presents the R package lpSolve which can be used to solve linear programming problems. It presents an overview of the method, and presents a two decision variable problem to illustrate the problem solving approach. This includes using R to visualise the two-dimensional feasible solution space, and showing how a graphical approach can be used to find an optimal solution to simpler problems. The chapter then shows how to call the lpSolve library, extract the optimal solution, and use the library to perform sensitivity analysis.

Chapter 14Agent-Based Simulation

Pages: 24

Agent based simulation is a valuable simulation method, which allows for the construction of a network of agents that interact through their own decision rules. Agent based simulation is suitable for modelling diffusion processes, which are common feature of many social and biological systems, for example, the spread of a novel pathogen in a susceptible population, or the diffusion of new product adoption in a social network. The focus of this chapter is on product adoption through a network, driven by simulating word of mouth recommendations. The chapter introduces the network graph, a key structure for simulating agents, via the R package igraph. Different types of network topologies are presented, including a random network. The overall design for each agent is specified, including the adoption mechanism, which is based on the Reed-Frost equation from the field of infectious disease modelling. The overall algorithm for conducting the simulation is presented, and a set of scenarios are explored using parallel programming (from the package furrr) to improve performance. The results examine the impact of different initial conditions on overall product adoption.

Chapter 15System Dynamics

Pages: 26

System dynamics is a simulation method used to build dynamic models that can support policy analysis and decision making. It has been successfully deployed across a range of domains, including health care, climate change, project management and manufacturing. The system dynamics approach is grounded in calculus (i.e. a model as a system of ordinary differential equations), and takes the perspective of modelling a system by focusing on its stocks, flows and feedbacks. This chapter provides an overview of system dynamics as a simulation method, and how it is based on solving a system of ordinary differential equations (ODEs). It introduces the R package deSolve which provides a general set of solvers for ODEs. The chapter provides three model examples: limits to growth, an SIR disease model, and an SIR disease model with a hospital stream. The chapter demonstrates how the tools of R's tidyverse can be used to perform a sensitivity analysis of the hospital model, and how this provides a basis for assessing the potential impact of countermeasures on disease progression.