Full Ebook of Data Analysis in Medicine and Health Using R Kamarul Imran Musa Online PDF All Chapter

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 69

Data Analysis in Medicine and Health

Using R Kamarul Imran Musa


Visit to download the full and correct content document:
https://ebookmeta.com/product/data-analysis-in-medicine-and-health-using-r-kamarul
-imran-musa/
More products digital (pdf, epub, mobi) instant
download maybe you interests ...

A Primer in Biological Data Analysis and Visualization


Using R 2nd Edition Gregg Hartvigsen

https://ebookmeta.com/product/a-primer-in-biological-data-
analysis-and-visualization-using-r-2nd-edition-gregg-hartvigsen/

Data Science and Predictive Analytics: Biomedical and


Health Applications using R, 2nd 2nd Edition Ivo D.
Dinov

https://ebookmeta.com/product/data-science-and-predictive-
analytics-biomedical-and-health-applications-using-r-2nd-2nd-
edition-ivo-d-dinov/

Imaging Life: Image Acquisition and Analysis in Biology


and Medicine Lawrence R. Griffing

https://ebookmeta.com/product/imaging-life-image-acquisition-and-
analysis-in-biology-and-medicine-lawrence-r-griffing/

Statistics for Ecologists Using R and Excel Data


Collection Exploration Analysis and Presentation 2nd
Edn 2nd Edition Mark Gardener

https://ebookmeta.com/product/statistics-for-ecologists-using-r-
and-excel-data-collection-exploration-analysis-and-
presentation-2nd-edn-2nd-edition-mark-gardener/
Automated Data Analysis Using Excel Brian D. Bissett

https://ebookmeta.com/product/automated-data-analysis-using-
excel-brian-d-bissett/

Data Analysis and Related Applications, Volume 2:


Multivariate, Health and Demographic Data Analysis 1st
Edition Konstantinos N. Zafeiris

https://ebookmeta.com/product/data-analysis-and-related-
applications-volume-2-multivariate-health-and-demographic-data-
analysis-1st-edition-konstantinos-n-zafeiris/

The Shape of Data: Geometry-Based Machine Learning and


Data Analysis in R Colleen M. Farrelly

https://ebookmeta.com/product/the-shape-of-data-geometry-based-
machine-learning-and-data-analysis-in-r-colleen-m-farrelly/

Beginning Data Science in R 4: Data Analysis,


Visualization, and Modelling for the Data Scientist 2nd
Edition Thomas Mailund

https://ebookmeta.com/product/beginning-data-science-in-r-4-data-
analysis-visualization-and-modelling-for-the-data-scientist-2nd-
edition-thomas-mailund/

R for Data Analysis in easy steps 2nd Edition Mike


Mcgrath

https://ebookmeta.com/product/r-for-data-analysis-in-easy-
steps-2nd-edition-mike-mcgrath/
Data Analysis in
Medicine and Health
Using R

In medicine and health, data are analyzed to guide treatment plans, patient care
and control and prevention policies. However, in doing so, researchers in medi-
cine and health often lack the understanding of data and statistical concepts and
the skills in programming. In addition, there is also an increasing demand for data
analyses to be reproducible, along with more complex data that require cutting-
edge analysis. This book provides readers with both the fundamental concepts
of data and statistical analysis and modeling. It also has the skills to perform the
analysis using the R programming language, which is the lingua franca for statis-
ticians. The topics in the book are presented in a sequence to minimize the time
to help readers understand the objectives of data and statistical analysis, learn
the concepts of statistical modeling and acquire the skills to perform the analysis.
The R codes and datasets used in the book will be made available on GitHub for
easy access. The book will also be live on the website bookdown.org, a service
provided by RStudio, PBC, to host books written using the bookdown package in
the R programming language.
Analytics and AI for Healthcare

Series Editor: Nilmini Wickramasinghe, Swinburne University of Technology

About the Series


Artificial Intelligence (AI) and analytics are increasingly being applied to various health-
care settings. AI and analytics are salient to facilitate better understanding and identifying
key insights from healthcare data in many areas of practice and enquiry in healthcare in-
cluding at the genomic, individual, hospital, community and/or population levels.

The Chapman & Hall/CRC Press Analytics and AI in Healthcare Series aims to help pro-
fessionals upskill and leverage the techniques, tools, technologies and tactics of analytics
and AI to achieve better healthcare delivery, access and outcomes.

The series covers all areas of analytics and AI as applied to healthcare. It will look at critical
areas, including prevention, prediction, diagnosis, treatment, monitoring, rehabilitation
and survivorship.

Explainable Artificial Intelligence in Healthcare: Unboxing Machine Learning in Pre-pro-


cessing and Medical Imaging
Edited by Mehul S Raval, Mohendra Roy, Tolga Kaya, and Rupal Kapdi

Data Analysis in Medicine and Health Using R


Kamarul Imran Musa, Wan Nor Arifin Wan Mansor, and Tengku Muhammad Hanis

For more information about this series please visit: https://www.routledge.com/analytics-


and-ai-for-healthcare/book-series/Aforhealth
Data Analysis in
Medicine and Health
Using R

Kamarul Imran Musa


Wan Nor Arifin Wan Mansor
Tengku Muhammad Hanis
First edition published 2024
by CRC Press
6000 Broken Sound Parkway NW, Suite 300, Boca Raton, FL 33487-2742

and by CRC Press


4 Park Square, Milton Park, Abingdon, Oxon, OX14 4RN

CRC Press is an imprint of Taylor & Francis Group, LLC

© 2024 Kamarul Imran Musa, Wan Nor Arifin Wan Mansor, and Tengku Muhammad Hanis

Reasonable efforts have been made to publish reliable data and information, but the author and pub-
lisher cannot assume responsibility for the validity of all materials or the consequences of their use.
The authors and publishers have attempted to trace the copyright holders of all material reproduced
in this publication and apologize to copyright holders if permission to publish in this form has not
been obtained. If any copyright material has not been acknowledged please write and let us know so
we may rectify in any future reprint.

Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced,
transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or
hereafter invented, including photocopying, microfilming, and recording, or in any information stor-
age or retrieval system, without written permission from the publishers.

For permission to photocopy or use material electronically from this work, access www.copyright.
com or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA
01923, 978-750-8400. For works that are not available on CCC please contact mpkbookspermis-
sions@tandf.co.uk

Trademark notice: Product or corporate names may be trademarks or registered trademarks and are
used only for identification and explanation without intent to infringe.

ISBN: 978-1-032-28415-6 (hbk)


ISBN: 978-1-032-28414-9 (pbk)
ISBN: 978-1-003-29677-5 (ebk)

DOI: 10.1201/9781003296775
Typeset in Alegreya font
by KnowledgeWorks Global Ltd

Publisher’s note: This book has been prepared from camera-ready copy provided by the authors.
Kamarul Imran Musa (KIM) would like to dedicate this book to his parents (Arwah
Hj Musa and Napisah Mohamed Nor) and his parents-in-law, his wife (Juhara
Haron), his sons (Afif and Iman) and to all his students.
Wan Nor Arifin Wan Mansor (WNA) would like to dedicate this book to researchers
in medicine, public health and health sciences who are brave enough to learn R.
You are the heroes of the future!
Tengku Muhammad Hanis would like to dedicate this book to his parents (Tengku
Mokhtar and Nor Malaysia), his wife (Nurul Asmaq) and all his fantastic teachers
and lecturers.
Contents

Preface xv

1 R, RStudio and RStudio Cloud 1


1.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 RStudio IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 RStudio Cloud . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4.1 The RStudio Cloud registration . . . . . . . . . . . . . . 3
1.4.2 Register and log in . . . . . . . . . . . . . . . . . . . . 3
1.5 Point and Click Graphical User Interface (GUI) . . . . . . . . . . 4
1.6 RStudio Server . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.7 Installing R and RStudio on Your Local Machine . . . . . . . . . 6
1.7.1 Installing R . . . . . . . . . . . . . . . . . . . . . . . . 6
1.7.2 Installing RStudio IDE . . . . . . . . . . . . . . . . . . 6
1.7.3 Checking R and RStudio installations . . . . . . . . . . . 7
1.7.4 TinyTeX, MiKTeX or MacTeX (for Mac OS) and TeX live . . 8
1.8 Starting Your RStudio . . . . . . . . . . . . . . . . . . . . . . 9
1.8.1 Console tab . . . . . . . . . . . . . . . . . . . . . . . . 10
1.8.2 Files, plots, packages, help and viewer pane . . . . . . . . 11
1.8.3 Environment, history, connection and build pane . . . . . 12
1.8.4 Source pane . . . . . . . . . . . . . . . . . . . . . . . . 12
1.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

2 R Scripts and R Packages 15


2.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.3 Open a New Script . . . . . . . . . . . . . . . . . . . . . . . . 15
2.3.1 Our first R script . . . . . . . . . . . . . . . . . . . . . 16
2.3.2 Function, argument and parameters . . . . . . . . . . . 17
2.3.3 If users require further help . . . . . . . . . . . . . . . . 18
2.4 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.4.1 Packages on CRAN . . . . . . . . . . . . . . . . . . . . 19
2.4.2 Checking availability of R package . . . . . . . . . . . . . 20
2.4.3 Install an R package . . . . . . . . . . . . . . . . . . . . 20
2.5 Working Directory . . . . . . . . . . . . . . . . . . . . . . . . 22
2.5.1 Starting a new R job . . . . . . . . . . . . . . . . . . . . 22

vii
viii Contents

2.5.2 Creating a new R project . . . . . . . . . . . . . . . . . 22


2.5.3 Location for dataset . . . . . . . . . . . . . . . . . . . . 22
2.6 Upload Data to RStudio Cloud . . . . . . . . . . . . . . . . . . 23
2.7 More Resources on RStudio Cloud . . . . . . . . . . . . . . . . 25
2.8 Guidance and Help . . . . . . . . . . . . . . . . . . . . . . . . 25
2.9 Bookdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

3 RStudio Project 27
3.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.3 Dataset Repository on GitHub . . . . . . . . . . . . . . . . . . 28
3.4 RStudio Project on RStudio or Posit Cloud . . . . . . . . . . . . 29
3.5 RStudio Project on Local Machine . . . . . . . . . . . . . . . . . 32
3.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

4 Data Visualization 37
4.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
4.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
4.3 History and Objectives of Data Visualization . . . . . . . . . . . 38
4.4 Ingredients for Good Graphics . . . . . . . . . . . . . . . . . . 38
4.5 Graphics Packages in R . . . . . . . . . . . . . . . . . . . . . . 38
4.6 The ggplot2 Package . . . . . . . . . . . . . . . . . . . . . . . 39
4.7 Preparation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
4.7.1 Create a new RStudio project . . . . . . . . . . . . . . . 40
4.7.2 Important questions before plotting graphs . . . . . . . . 40
4.8 Read Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.9 Load the Packages . . . . . . . . . . . . . . . . . . . . . . . . 41
4.10 Read the Dataset . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.11 Basic Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
4.12 More Complex Plots . . . . . . . . . . . . . . . . . . . . . . . 46
4.12.1 Adding another variable . . . . . . . . . . . . . . . . . . 46
4.12.2 Making subplots . . . . . . . . . . . . . . . . . . . . . 50
4.12.3 Overlaying plots . . . . . . . . . . . . . . . . . . . . . . 51
4.12.4 Combining different plots . . . . . . . . . . . . . . . . . 55
4.12.5 Statistical transformation . . . . . . . . . . . . . . . . . 60
4.12.6 Customizing title . . . . . . . . . . . . . . . . . . . . . 62
4.12.7 Choosing themes . . . . . . . . . . . . . . . . . . . . . 65
4.12.8 Adjusting axes . . . . . . . . . . . . . . . . . . . . . . 66
4.13 Saving Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
4.14 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

5 Data Wrangling 73
5.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
5.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Contents ix

5.2.1 Definition of data wrangling . . . . . . . . . . . . . . . 73


5.3 Data Wrangling with dplyr Package . . . . . . . . . . . . . . . . 74
5.3.1 dplyr package . . . . . . . . . . . . . . . . . . . . . . . 74
5.3.2 Common data wrangling processes . . . . . . . . . . . . 74
5.3.3 Some dplyr functions . . . . . . . . . . . . . . . . . . . 74
5.4 Preparation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
5.4.1 Create a new project or set the working directory . . . . . 75
5.4.2 Load the libraries . . . . . . . . . . . . . . . . . . . . . 75
5.4.3 Datasets . . . . . . . . . . . . . . . . . . . . . . . . . 76
5.5 Select Variables, Generate New Variable and Rename Variable . . . 80
5.5.1 Select variables using dplyr::select() . . . . . . . . . . . 80
5.5.2 Generate new variable using mutate() . . . . . . . . . . . 81
5.5.3 Rename variable using rename() . . . . . . . . . . . . . . 82
5.6 Sorting Data and Selecting Observation . . . . . . . . . . . . . . 82
5.6.1 Sorting data using arrange() . . . . . . . . . . . . . . . . 82
5.6.2 Select observation using filter() . . . . . . . . . . . . . 83
5.7 Group Data and Get Summary Statistics . . . . . . . . . . . . . 84
5.7.1 Group data using group_by() . . . . . . . . . . . . . . . . 84
5.7.2 Summary statistic using summarize() . . . . . . . . . . . 84
5.8 More Complicated dplyr Verbs . . . . . . . . . . . . . . . . . . 85
5.9 Data Transformation for Categorical Variables . . . . . . . . . . 86
5.9.1 forcats package . . . . . . . . . . . . . . . . . . . . . . 86
5.9.2 Conversion from numeric to factor variables . . . . . . . 86
5.9.3 Recoding variables . . . . . . . . . . . . . . . . . . . . 87
5.9.4 Changing the level of categorical variable . . . . . . . . . 87
5.10 Additional Resources . . . . . . . . . . . . . . . . . . . . . . . 88
5.11 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

6 Exploratory Data Analysis 91


6.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
6.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
6.3 EDA Using ggplot2 Package . . . . . . . . . . . . . . . . . . . . 92
6.3.1 Usage of ggplot2 . . . . . . . . . . . . . . . . . . . . . 92
6.4 Preparation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
6.4.1 Load the libraries . . . . . . . . . . . . . . . . . . . . . 92
6.4.2 Read the dataset into R . . . . . . . . . . . . . . . . . . 93
6.5 EDA in Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
6.6 EDA with Plots . . . . . . . . . . . . . . . . . . . . . . . . . . 98
6.6.1 One variable: Distribution of a categorical variable . . . . 98
6.6.2 One variable: Distribution of a numerical variable . . . . . 101
6.6.3 Two variables: Plotting a numerical and a categorical
variable . . . . . . . . . . . . . . . . . . . . . . . . . . 103
6.6.4 Three variables: Plotting a numerical and two categorical
variables . . . . . . . . . . . . . . . . . . . . . . . . . 105
6.6.5 Faceting the plots . . . . . . . . . . . . . . . . . . . . . 105
x Contents

6.6.6 Line plot . . . . . . . . . . . . . . . . . . . . . . . . . 107


6.6.7 Plotting means and error bars . . . . . . . . . . . . . . . 109
6.6.8 Scatterplot with fit line . . . . . . . . . . . . . . . . . . 111
6.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113

7 Linear Regression 115


7.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
7.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
7.3 Linear Regression Models . . . . . . . . . . . . . . . . . . . . . 115
7.4 Prepare R Environment for Analysis . . . . . . . . . . . . . . . 116
7.4.1 Libraries . . . . . . . . . . . . . . . . . . . . . . . . . 116
7.4.2 Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . 116
7.5 Simple Linear Regression . . . . . . . . . . . . . . . . . . . . . 118
7.5.1 About simple linear regression . . . . . . . . . . . . . . 118
7.5.2 Data exploration . . . . . . . . . . . . . . . . . . . . . 118
7.5.3 Univariable analysis . . . . . . . . . . . . . . . . . . . . 119
7.5.4 Model fit assessment . . . . . . . . . . . . . . . . . . . 120
7.5.5 Presentation and interpretation . . . . . . . . . . . . . . 122
7.6 Multiple Linear Regression . . . . . . . . . . . . . . . . . . . . 122
7.6.1 About multiple linear regression . . . . . . . . . . . . . . 122
7.6.2 Data exploration . . . . . . . . . . . . . . . . . . . . . 123
7.6.3 Univariable analysis . . . . . . . . . . . . . . . . . . . . 124
7.6.4 Multivariable analysis . . . . . . . . . . . . . . . . . . . 125
7.6.5 Interaction . . . . . . . . . . . . . . . . . . . . . . . . 130
7.6.6 Model fit assessment . . . . . . . . . . . . . . . . . . . 131
7.6.7 Presentation and interpretation . . . . . . . . . . . . . . 133
7.7 Prediction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
7.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136

8 Binary Logistic Regression 137


8.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
8.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
8.3 Logistic Regression Model . . . . . . . . . . . . . . . . . . . . 138
8.4 Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
8.5 Logit and Logistic Models . . . . . . . . . . . . . . . . . . . . . 138
8.6 Prepare Environment for Analysis . . . . . . . . . . . . . . . . . 139
8.6.1 Creating a RStudio project . . . . . . . . . . . . . . . . 139
8.6.2 Loading libraries . . . . . . . . . . . . . . . . . . . . . 140
8.7 Read Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
8.8 Explore Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
8.9 Estimate the Regression Parameters . . . . . . . . . . . . . . . 143
8.10 Simple Binary Logistic Regression . . . . . . . . . . . . . . . . 143
8.11 Multiple Binary Logistic Regression . . . . . . . . . . . . . . . . 146
8.12 Convert the Log Odds to Odds Ratio . . . . . . . . . . . . . . . 148
8.13 Making Inference . . . . . . . . . . . . . . . . . . . . . . . . . 148
Contents xi

8.14 Models Comparison . . . . . . . . . . . . . . . . . . . . . . . 150


8.15 Adding an Interaction Term . . . . . . . . . . . . . . . . . . . 151
8.16 Prediction from Binary Logistic Regression . . . . . . . . . . . . 152
8.16.1 Predict the log odds . . . . . . . . . . . . . . . . . . . . 153
8.16.2 Predict the probabilities . . . . . . . . . . . . . . . . . . 153
8.17 Model Fitness . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
8.18 Presentation of Logistic Regression Model . . . . . . . . . . . . 156
8.19 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156

9 Multinomial Logistic Regression 159


9.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
9.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
9.3 Examples of Multinomial Outcome Variables . . . . . . . . . . . 160
9.4 Models for Multinomial Outcome Data . . . . . . . . . . . . . . 160
9.5 Estimation for Multinomial Logit Model . . . . . . . . . . . . . 161
9.5.1 Log odds and odds ratios . . . . . . . . . . . . . . . . . 161
9.5.2 Conditional probabilities . . . . . . . . . . . . . . . . . 162
9.6 Prepare Environment . . . . . . . . . . . . . . . . . . . . . . . 162
9.6.1 Load libraries . . . . . . . . . . . . . . . . . . . . . . . 162
9.6.2 Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . 164
9.6.3 Read data . . . . . . . . . . . . . . . . . . . . . . . . . 164
9.6.4 Data wrangling . . . . . . . . . . . . . . . . . . . . . . 165
9.6.5 Create new categorical variable from fbs . . . . . . . . . . 165
9.6.6 Exploratory data analysis . . . . . . . . . . . . . . . . . 166
9.6.7 Confirm the order of cat_fbs . . . . . . . . . . . . . . . 167
9.7 Estimation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
9.7.1 Single independent variable . . . . . . . . . . . . . . . . 168
9.7.2 Multiple independent variables . . . . . . . . . . . . . . 170
9.7.3 Model with interaction term between independent variables 171
9.8 Inferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
9.9 Interpretation . . . . . . . . . . . . . . . . . . . . . . . . . . 174
9.10 Prediction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
9.11 Presentation of Multinomial Regression Model . . . . . . . . . . 175
9.12 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175

10 Poisson Regression 177


10.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
10.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
10.3 Prepare R Environment for Analysis . . . . . . . . . . . . . . . 179
10.3.1 Libraries . . . . . . . . . . . . . . . . . . . . . . . . . 179
10.4 Poisson Regression for Count . . . . . . . . . . . . . . . . . . . 179
10.4.1 About Poisson regression for count . . . . . . . . . . . . 179
10.4.2 Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . 180
10.4.3 Data exploration . . . . . . . . . . . . . . . . . . . . . 180
10.4.4 Univariable analysis . . . . . . . . . . . . . . . . . . . . 181
xii Contents

10.4.5 Multivariable analysis . . . . . . . . . . . . . . . . . . . 183


10.4.6 Interaction . . . . . . . . . . . . . . . . . . . . . . . . 185
10.4.7 Model fit assessment . . . . . . . . . . . . . . . . . . . 186
10.4.8 Presentation and interpretation . . . . . . . . . . . . . . 188
10.4.9 Prediction . . . . . . . . . . . . . . . . . . . . . . . . . 192
10.5 Poisson Regression for Rate . . . . . . . . . . . . . . . . . . . . 193
10.5.1 About Poisson regression for rate . . . . . . . . . . . . . 193
10.5.2 Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . 193
10.5.3 Data exploration . . . . . . . . . . . . . . . . . . . . . 194
10.5.4 Univariable analysis . . . . . . . . . . . . . . . . . . . . 196
10.5.5 Multivariable analysis . . . . . . . . . . . . . . . . . . . 198
10.5.6 Interaction . . . . . . . . . . . . . . . . . . . . . . . . 199
10.5.7 Model fit assessment . . . . . . . . . . . . . . . . . . . 200
10.5.8 Presentation and interpretation . . . . . . . . . . . . . . 201
10.6 Quasi-Poisson Regression for Overdispersed Data . . . . . . . . 204
10.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205

11 Survival Analysis: Kaplan–Meier and Cox Proportional Hazard (PH)


Regression 207
11.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
11.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
11.3 Types of Survival Analysis . . . . . . . . . . . . . . . . . . . . . 208
11.4 Prepare Environment for Analysis . . . . . . . . . . . . . . . . . 208
11.4.1 RStudio project . . . . . . . . . . . . . . . . . . . . . . 208
11.4.2 Packages . . . . . . . . . . . . . . . . . . . . . . . . . 209
11.5 Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
11.6 Explore Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
11.7 Kaplan–Meier Survival Estimates . . . . . . . . . . . . . . . . . 215
11.8 Plot the Survival Probability . . . . . . . . . . . . . . . . . . . 217
11.9 Comparing Kaplan–Meier Estimates across Groups . . . . . . . . 219
11.9.1 Log-rank test . . . . . . . . . . . . . . . . . . . . . . . 219
11.9.2 Peto-peto test . . . . . . . . . . . . . . . . . . . . . . . 220
11.10 Semi-Parametric Models in Survival Analysis . . . . . . . . . . . 221
11.10.1 Cox proportional hazards regression . . . . . . . . . . . 222
11.10.2 Advantages of the Cox proportional hazards regression . . 223
11.11 Estimation from Cox Proportional Hazards Regression . . . . . . 224
11.11.1 Simple Cox PH regression . . . . . . . . . . . . . . . . . 224
11.11.2 Multiple Cox PH regression . . . . . . . . . . . . . . . . 227
11.12 Adding Interaction in the Model . . . . . . . . . . . . . . . . . 230
11.13 The Proportional Hazard Assumption . . . . . . . . . . . . . . . 232
11.13.1 Risk constant over time . . . . . . . . . . . . . . . . . . 232
11.13.2 Test for PH assumption . . . . . . . . . . . . . . . . . . 232
11.13.3 Plots to assess PH assumption . . . . . . . . . . . . . . . 233
11.14 Model Checking . . . . . . . . . . . . . . . . . . . . . . . . . 236
11.14.1 Prediction from Cox PH model . . . . . . . . . . . . . . 236
Contents xiii

11.14.2 Residuals from Cox PH model . . . . . . . . . . . . . . . 238


11.14.3 Influential observations . . . . . . . . . . . . . . . . . . 239
11.15 Plot the Adjusted Survival . . . . . . . . . . . . . . . . . . . . . 240
11.16 Presentation and Interpretation . . . . . . . . . . . . . . . . . 240
11.17 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241

12 Parametric Survival Analysis 243


12.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
12.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
12.2.1 Advantages of parametric survival analysis models . . . . 244
12.3 Parametric Survival Analysis Model . . . . . . . . . . . . . . . . 244
12.3.1 Proportional hazard parametric models . . . . . . . . . . 245
12.3.2 Accelerated failure time model (AFT) models . . . . . . . . 245
12.4 Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
12.4.1 Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . 247
12.4.2 Set the environment . . . . . . . . . . . . . . . . . . . . 247
12.4.3 Read dataset . . . . . . . . . . . . . . . . . . . . . . . 248
12.4.4 Data wrangling . . . . . . . . . . . . . . . . . . . . . . 248
12.4.5 Exploratory data analysis (EDA) . . . . . . . . . . . . . . 249
12.4.6 Exponential survival model . . . . . . . . . . . . . . . . 250
12.4.7 Weibull (accelerated failure time) . . . . . . . . . . . . . 251
12.4.8 Weibull (proportional hazard) . . . . . . . . . . . . . . . 253
12.4.9 Model adequacy for Weibull distribution . . . . . . . . . 255
12.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256

13 Introduction to Missing Data Analysis 257


13.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
13.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
13.3 Types of Missing Data . . . . . . . . . . . . . . . . . . . . . . 257
13.4 Preliminaries . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
13.4.1 Packages . . . . . . . . . . . . . . . . . . . . . . . . . 259
13.4.2 Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . 260
13.5 Exploring Missing Data . . . . . . . . . . . . . . . . . . . . . . 261
13.6 Handling Missing Data . . . . . . . . . . . . . . . . . . . . . . 264
13.6.1 Listwise deletion . . . . . . . . . . . . . . . . . . . . . 265
13.6.2 Simple imputation . . . . . . . . . . . . . . . . . . . . 265
13.6.3 Single imputation . . . . . . . . . . . . . . . . . . . . . 268
13.6.4 Multiple imputation . . . . . . . . . . . . . . . . . . . . 274
13.7 Presentation . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
13.8 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
13.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

14 Model Building and Variable Selection 283


14.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
14.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
xiv Contents

14.3 Model Building . . . . . . . . . . . . . . . . . . . . . . . . . . 284


14.4 Variable Selection for Prediction . . . . . . . . . . . . . . . . . 285
14.4.1 Backward elimination . . . . . . . . . . . . . . . . . . . 285
14.4.2 Forward selection . . . . . . . . . . . . . . . . . . . . . 286
14.4.3 Stepwise selection . . . . . . . . . . . . . . . . . . . . . 286
14.4.4 All possible subset selection . . . . . . . . . . . . . . . . 286
14.5 Stopping Rule and Selection Criteria in Automatic Variable Selection 287
14.6 Problems with Automatic Variable Selections . . . . . . . . . . . 287
14.7 Purposeful Variable Selection . . . . . . . . . . . . . . . . . . . 288
14.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288

Bibliography 289

Index 291
Preface

We wrote this book to help new R programming users with limited programming
and statistical backgrounds. We understand the struggles they are going through to
move from point-and-click statistical software such as SPSS or MS Excel to more
code-centric software such as R and Python. From our experiences, frustration sets
in early in learning this code-centric software. It often demotivates new users to the
extent that they ditch them and return to using point-and-click statistical software.
This book will minimize these struggles and gently help these excited but fragile new
users to learn quickly and effectively the codes and workflows to perform data and
statistical analysis using the R programming language.
This book’s audience includes postgraduate students, public health researchers, epi-
demiologists and biostatisticians. We designed and wrote this book based on our
experiences teaching students in the public health, epidemiology and biostatistics
programs at the School of Medical Sciences, Universiti Sains Malaysia. Between KIM
and WNA, we have over 30 years of experience teaching and training undergraduate
and postgraduate students. As we mentioned earlier, most of our postgraduate stu-
dents are students in the public health, epidemiology or biostatistics programs at
the School of Medical Sciences, Universiti Sains Malaysia.
The courses we teach include basic and advanced statistics, multivariable data anal-
ysis, structural equation modeling, advanced numerical data analysis and advanced
categorical data analysis. This book, we believe, will achieve its objective. The main
objective is to help new R programming users (such as our undergraduate and post-
graduate students) quickly understand the R programming language, make plots,
explore data, summarize data and perform statistical analyses inside RStudio IDE.
We also provide the interpretation of graphs, tables and statistical models relevant
to our students and us. They do not have strong mathematical and statistical back-
grounds; however, in their career, they are very much involved with collecting, ana-
lyzing and interpreting data. Some will work at medical and health institutions and
organizations. Their applied knowledge and skills in data analysis and epidemiolog-
ical and statistical models will help them draw health policies and make evidence-
based public health interventions.
We used the rmarkdown package1 and the bookdown package2 to write this book
inside RStudio IDE. We are truly grateful to all the people who have developed both
1 https://rmarkdown.rstudio.com/
2 https://github.com/rstudio/bookdown

xv
xvi Preface

packages and to Posit Software3 . Posit Software PBC is an Open Source Data Science
Company, previously known as RStudio PBC. Posit Software PBC continuously sup-
ports open-source data science initiatives and develops and distributes the fantastic
RStudio IDE4 . When writing this physical book, we used R version 4.2.2 and RStudio
version 2022.07.2 Build 576.
The source codes for the book are available on our GitHub repository5 and also pro-
vided the datasets in the data folder at GitHub6 . We are indebted to George Knott,
the statistics editor at Chapman & Hall (CRC Press), who has been very supportive
and understanding when we are chasing the datelines of the book. We also appreci-
ate the help of Nivedita Menon, who has taken over the publishing task from George
after he moved from his role at Chapman & Hall (CRC Press). In addition, Chapman
& Hall (CRC Press) has been very kind to allow us to have the online version of the
book on the bookdown website7 . All in all, we hope everyone enjoys this book!

Kamarul Imran Musa8

Wan Nor Arifin Wan Mansor9

Tengku Muhammad Hanis10

School of Medical Sciences,


Universiti Sains Malaysia
2023-04-22

3 https://posit.co/
4 https://posit.co/products/open-source/rstudio/
5 https://github.com/drkamarul/multivar_data_analysis
6 https://github.com/drkamarul/multivar_data_analysis/tree/main/data
7 https://bookdown.org/drki_musa/dataanalysis/
8 https://github.com/drkamarul
9 https://github.com/wnarifin
10 https://github.com/tengku-hanis
1
R, RStudio and RStudio Cloud

1.1 Objectives
At the end of the chapter, the readers will
• be introduced to the R programming language
• be introduced to RStudio IDE
• be introduced to RStudio Cloud. RStudio Cloud is a platform where users can
run RStudio on the cloud
• be able to install R on their local machine
• be able to install RStudio IDE on their local machine
• understand how to install LaTeX editor (Miktex or Tex Live and MacTex). A LaTeX
editor is optional but is required if users want to render PDF outputs
• understand the structure of R scripts work
• understand R packages and R Taskview

1.2 Introduction
R is a language and environment for statistical computing and graphics. It is a GNU
project similar to the S language and environment developed at Bell Laboratories. R
provides a wide variety of statistical (linear and nonlinear modeling, classical statis-
tical tests, time-series analysis, classification, clustering and others) and graphical
techniques and is highly extensible.
One of R’s strengths is the ease with which well-designed publication-quality plots
can be produced, including mathematical symbols and formulae where needed. R
is available as Free Software under the terms of the Free Software Foundation’s
GNU General Public License in source code form. It compiles and runs on various
UNIX platforms and similar systems (including FreeBSD and Linux), Windows and
macOS.

DOI: 10.1201/9781003296775-1 1
2 1 R, RStudio and RStudio Cloud

1.3 RStudio IDE


RStudio is an integrated development environment (IDE) for R. It includes a con-
sole and syntax-highlighting editor that supports direct code execution and tools
for plotting, history, debugging and workspace management.
RStudio is available in open source and commercial editions and runs on the desktop
(Windows, Mac, and Linux) or in a browser connected to RStudio Server or RStudio
Workbench (Debian/Ubuntu, Red Hat/CentOS and SUSE Linux).

1.4 RStudio Cloud


RStudio Cloud by RStudio facilitates the learning of R. Anyone can sign up and start
using RStudio on the cloud. It is one of the quickest ways to learn R.
Using RStudio Cloud, we do not have to install R on our local machine. RStudio
Cloud allows collaboration between R teachers and students. It also helps colleagues
work together on R projects.
RStudio described it as a lightweight, cloud-based solution that allows anyone to
do, share, teach and learn data science online. And it also adds that by using this
platform, we can
• analyze our data using the RStudio IDE directly from our browser
• share projects with our team, class, workshop or the world
• teach data science with R to our students or colleagues
• learn data science in an instructor-led environment or with interactive tutorials
RStudio Cloud has the free and the commercial version (which is fortunately very
affordable). To start using RStudio Cloud, visit https://rstudio.cloud/. On the page,
click ‘Sign Up’.
With RStudio Cloud, there is almost nothing to configure and you do not need dedi-
cated hardware, installation or annual purchase contract required. Individual users,
instructors and students only need a browser to do, share, teach and learn data
science.
1.4 RStudio Cloud 3

FIGURE 1.1
Sign up page for RStudio Cloud.

FIGURE 1.2
RStudio Cloud webpage.

1.4.1 The RStudio Cloud registration


This is the registration and login webpage for RStudio Cloud.

1.4.2 Register and log in


Proceed with registration. If you have Google account or GitHub account, you can
use either one to quickly register. After you complete the registration, you can log
into RStudio Cloud.
4 1 R, RStudio and RStudio Cloud

FIGURE 1.3
RStudio Cloud registration page.

1.5 Point and Click Graphical User Interface (GUI)


There are a number of GUI versions of R, also known as R GUI. The interface resem-
bles a popular statistical software SPSS. For example, there are
• Bluesky statistics https://www.blueskystatistics.com/
• jamovi https://www.jamovi.org/
BlueSky Statistics can help users to
• migrate from expensive propriety statistical applications to R
• ease the R learning curve
• use the cutting-edge analytics available in R without having to learn program-
ming
• get results in true word processing tables automatically
• quickly add your own menus and dialog boxes to any R functions
And this is jamovi software. jamovi aims to be a neutral platform and takes no posi-
tion with respect to competing statistical philosophies. The project was not founded
to promote a particular statistical ideology, instead wanting to serve as a safe space
where different statistical approaches might be published side-by-side and consider
themselves first-rate members of the jamovi community.
jamovi is an interesting software. It is a new ‘third generation’ statistical spreadsheet.
It is designed from the ground up to be easy to use, it is a compelling alternative
to costly statistical products such as SPSS and SAS. jamovi is built on top of the R
statistical language, giving you access to the best the statistics community has to
1.6 RStudio Server 5

FIGURE 1.4
Bluesky statistics software interface.

FIGURE 1.5
jamovi software.

offer. jamovi will always be free and open because jamovi is made by the scientific
community, for the scientific community.

1.6 RStudio Server


You can run R and RStudio on the server. To do this you have to install RStu-
dio Workbench. Previously, R Studio Workbench was known as RStudio Server.
6 1 R, RStudio and RStudio Cloud

By using RStudio Server, R users can perform analysis on the server. Using RStu-
dio server can give you a taste of cloud data analysis.
There are two versions of RStudio server:
• RStudio Server: This is the Open Source edition
• RStudio Workbench: This is the Professional edition
At our medical school. we have RStudio Server Professional Edition (courtesy of
RStudio, of course) running on our server here https://healthdata.usm.my/rstu
dio/auth-sign-in

1.7 Installing R and RStudio on Your Local Machine


To install R on your local machine, you have to have Admin Right to your machine.
We recommend that you install
• R first,
• then RStudio

1.7.1 Installing R
Though you can use the native R software (that you just installed) to run R codes, we
highly encourage you to use RStudio Integrated Desktop Environment (IDE).
We will show this step by step. First, let us install R on your machine. To install R, go
to cran1 . Then choose the R version that’s correct for your machine OS. For example,
for Windows OS, the link is https://cran.r-project.org/bin/windows/base/R-4.3.1-
win.exe. And for Mac OS, the download link is https://cran.r-project.org/bin/ma
cosx/big-sur-x86_64/base/R-4.3.1-x86_64.pkg. Similarly, if you are using Linux,
follow the steps as listed before.
It is always recommended that users install the latest version of R. During this writ-
ing, the latest version is R version 4.2.1 known as Funny-Looking Kid version that
was released on June 23, 2022. Users can have multiple R versions on the same local
machines. So you do not need to uninstall the old R version to install a new R version.

1.7.2 Installing RStudio IDE


Now, to install RStudio IDE, go to the RStudio download2 page. Choose the sup-
ported platforms correct for your machine OS. The size of the download will be
around 90–110 MB.

1 https://cran.r-project.org/
2 https://www.rstudio.com/products/rstudio/download/#download
1.7 Installing R and RStudio on Your Local Machine 7

FIGURE 1.6
CRAN website.

FIGURE 1.7
RStudio website.

1.7.3 Checking R and RStudio installations


Now, we assume you have installed both R and RStudio. To make sure they work
perfectly (or at least for the first time), check:
• Does your machine can load R? Depending on your OS, go and start R.
• What version of R do you have? When R loads, look for the version of R.
• Do you have RStudio? Depending on your OS, go and start RStudio.
8 1 R, RStudio and RStudio Cloud

FIGURE 1.8
R Markdown.

• What version of RStudio do you have? When RStudio loads, look for the version
of R. If you have multiple R version, you can choose the R version of your choice
by going to Tools then Global Options then General.
• Do you need to update R and RStudio? By knowing the versions of R and RStudio,
now you know if you need to update both or one of them.

1.7.4 TinyTeX, MiKTeX or MacTeX (for Mac OS) and TeX live
Based on experience, as R users develop more R skills, they may find converting their
analysis into PDF documents desirable. It is necessary to install a LaTeX editor if they
want to convert the outputs they generated in R into PDF format. However, if they do
not need to produce a PDF document, they do not have to install it. If they require
more flexibility to edit their LaTeX codes, then they may think of getting common
LaTeX editors such as MiKtex, MacTeX or TeX Live.
We encourage users to first install TinyTeX. If you render your R document into PDF
just by using TinyTeX, then that’s enough. To install TinyTeX, follow the instruction
from https://yihui.org/tinytex/ . For R users, it is simple running these two codes
install.packages('tinytex') and tinytex::install_tinytex()

Install MiKTeX as your LaTeX editor if your machine is using Window OS. If you use
MacOS, then install MacTeX.
1.8 Starting Your RStudio 9

FIGURE 1.9
MikTeX webpage.

FIGURE 1.10
MacTeX webpage.

1.8 Starting Your RStudio


You can either login to RStudio Cloud and automatically see the RStudio interface
OR you can start RStudio on your local machine by loading it. Remember, to login
to RStudio Cloud, go to https://rstudio.cloud. You will be asked for your username
and password.
Click this link3
To start R on your machine, and if you are using Windows, find the RStudio program
in your start bar on your machine. And start it. You will see an interface like the one
3 https://rstudio.cloud/spaces/156361/join?access_code=WtlSxNuTm%2Fz7E%2BLb%2FW2XnOw480

%2BBTmL4B%2FqjYRIg
10 1 R, RStudio and RStudio Cloud

FIGURE 1.11
RStudio Cloud space for this book.

FIGURE 1.12
Rstudio Interface with Vibrant Ink theme.

below. This interface differs from what you see on your screen because I use the Vi-
brant Ink Theme. To choose the theme of your choice, click Global Options, then click
Appearance. There are several themes available for you to choose.
What you see on RStudio now? You should see three panes if you start RStudio for
the first time or four panes if you have used RStudio before.

1.8.1 Console tab


In Console tab, this is where we will see most of the results generated from codes in
RStudio.
1.8 Starting Your RStudio 11

FIGURE 1.13
RStudio Panes.

FIGURE 1.14
Console tab.

1.8.2 Files, plots, packages, help and viewer pane


In this console, you will see
• List of objects (Remember, R is an object-oriented-programming or oop)
• R files, datasets, tables, list, etc.
12 1 R, RStudio and RStudio Cloud

FIGURE 1.15
File, Plots, Packages, Help and Viewer pane.

FIGURE 1.16
Environment, History, Connections and Build pane.

1.8.3 Environment, history, connection and build pane


In the environment, history, connection and build pane, you will see this

1.8.4 Source pane


In the Source pane, you can create R files and write your R codes
1.9 Summary 13

FIGURE 1.17
Source pane.

1.9 Summary
In this chapter, we learn about R, RStudio IDE and RStudio Cloud. We have also
introduced two point and click R GUIs Bluesky Statistics and jamovi. To use RStudio
without any hassle during installation, we recommend using RStudio Cloud.
2
R Scripts and R Packages

2.1 Objectives
At the end of this chapter, readers will be able
• to write simple R scripts
• to understand R packages
• to install R packages
• to create a new RStudio project
• to be able to use RStudio Cloud

2.2 Introduction
An R script is simply a text file containing (almost) the same commands that you
would enter on the command line of R. ( almost) refers to the fact that if you are
using sink() to send the output to a file, you will have to enclose some commands in
print() to get the same output as on the command line.
R packages are extensions to the R statistical programming language. R packages
contain code, data and documentation in a standardized collection format that can
be installed by users of R, typically via a centralized software repository such as
CRAN (the Comprehensive R Archive Network).

2.3 Open a New Script


For beginners, you may start by writing some simple codes. Since these codes are
written in R language, we call these codes as R scripts. To do this, go to File, then
click R Script
• File -> R Script
• In Window OS, users can use this shortcut CTRL-SHIFT-N

DOI: 10.1201/9781003296775-2 15
16 2 R Scripts and R Packages

FIGURE 2.1
New R script.

2.3.1 Our first R script


Let us write our very first R codes inside an R script.
• In Line 1, type 2 + 3
• click CTRL-ENTER or CMD-ENTER
• see the outputs in the Console Pane

2 + 3

## [1] 5

After writing your codes inside the R script, you can save the R script file. This will
allow you to open it up again to continue your work.
And to save R script, go to
• File ->
• Save As ->
• Choose folder ->

• Name the file


Now, types these codes to check the version of your R software

version[6:7]

## _
## status
## major 4
2.3 Open a New Script 17

The current version for R is 4.2.1


By they way if you are using lower version of R, then we recommend you to upgrade.
To upgrade your R software
• and if you are using Windows, you can use installr package
• but if you use macOS, you may need to download R again and manually install
You may find more information from this link1 .

2.3.2 Function, argument and parameters


R codes contain
• function
• argument
• parameters
f <- function(<arguments>) {
## Do something interesting
}

For example, to list all the arguments for a function, you may use args(). Let’s exam-
ine the arguments for the function lm(), a function to estimate parameters for linear
regression model.

args(lm)

## function (formula, data, subset, weights, na.action, method = "qr",


## model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE,
## contrasts = NULL, offset, ...)
## NULL

Once you understand the required arguments, you may use the parameters so the
function can perform the desired task. For example:

lm(weight ~ Time, data = ChickWeight)

##
## Call:
## lm(formula = weight ~ Time, data = ChickWeight)
##
## Coefficients:
## (Intercept) Time
## 27.467 8.803

1 https://www.linkedin.com/pulse/3- methods- update- r- rstudio- windows- mac- woratana-

ngarmtrakulchol/
18 2 R Scripts and R Packages

FIGURE 2.2
Help pane.

2.3.3 If users require further help


If users would like to see more extensive guides on certain functions, they may type
the ? before the function. For example, if users want to know more about the func-
tion lm, then they may type the R codes below. Following that, R will open a help page
with a more detailed description, usage of the function and the relevant arguments.

?lm

## starting httpd help server ... done

Here, we provide an example how a Help Pane will look like.

2.4 Packages
R is a programming language. Furthermore, R software runs on packages. R pack-
ages are collections of functions and data sets developed by the community. They
2.4 Packages 19

FIGURE 2.3
CRAN task views.

increase the power of R by improving existing base R codes and functions or by


adding new ones.
A package is a suitable way to organize users’ work and share it with others if users
want to. Typically, a package will include
• code (sometimes not just R codes but codes in other programming languages),
• documentation for the package and the functions inside,
• some tests to check that everything works as it should, and
• data sets.
Users can read more about R packages here2 .

2.4.1 Packages on CRAN


At the time of writing, the CRAN package repository features 12784 packages. Avail-
able R packages are listed on the Cran Task Views website.
CRAN task views aim to provide some guidance which packages on CRAN are rel-
evant for tasks related to a certain topic. They give a brief overview of the included
packages and can be automatically installed using the ctv package.

2 https://www.datacamp.com/community/tutorials/r-packages-guide
20 2 R Scripts and R Packages

The views are intended to have a sharp focus so that it is sufficiently clear which pack-
ages should be included (or excluded), and they are not meant to endorse the “best”
packages for a given task.

2.4.2 Checking availability of R package


To check if the desired package is available on users’ machine, users can this inside
their R console:

library(tidyverse)

## -- Attaching core tidyverse packages ------------------------ tidyverse 2.0.0 -


-
## v dplyr 1.1.1 v readr 2.1.4
## v forcats 1.0.0 v stringr 1.5.0
## v ggplot2 3.4.2 v tibble 3.2.1
## v lubridate 1.9.2 v tidyr 1.3.0
## v purrr 1.0.1
## -- Conflicts ------------------------------------------
tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## i Use the conflicted package (<http://conflicted.r-
lib.org/>) to force all conflicts to become errors

Users should not receive any error messages. Users who have not installed the pack-
age will receive an error message. Furthermore, it tells them that the package is not
available in their R. By default, the package is stored in the R folder in their My Doc-
ument or HOME directory

.libPaths()

## [1] "C:/Users/drkim/AppData/Local/R/win-library/4.2"
## [2] "C:/Program Files/R/R-4.2.3/library"

2.4.3 Install an R package


To install an R package, there are two ways:

1. users can type the R codes like below (without the # tag)
2.4 Packages 21

FIGURE 2.4
Install packages pane.

FIGURE 2.5
The name of the package to be installed.

# install.packages(tidyverse, dependencies = TRUE)

2. users can use the GUI in the RStudio IDE

Now, type the package you want to install. For example, if you want to install the
tidyverse package then click the Install button. Please make sure that you need to
have internet access to do this. You can also install packages from:
• a zip file (from your machine or USB)
• from github repository
• other repository
22 2 R Scripts and R Packages

2.5 Working Directory


Setting and knowing the R working directory is very important. Our working direc-
tory will contain the R codes, the R outputs, datasets or even resources or tutorials
that can help us during in R project or during our R analysis/
The working directory is just a folder. Moreover, the folder can contain many sub-
folders. We recommend that the folder contain the dataset (if you want to analyze
your data locally) and other R objects. R will store many other R objects created dur-
ing each R session.
Type this to locate the working directory:

getwd()

## [1] "C:/Users/drkim/Downloads/multivar_data_analysis/multivar_data_analysis"

2.5.1 Starting a new R job


There are two ways to start a new R job:
• create a new R project from RStudio IDE. This is the method that we recommend.
• setting your working directory using the setwd() function.

2.5.2 Creating a new R project


We highly encourage users to create a new R project. To do this, users can
• go to File -> New Project

When you see project type, click New Project

2.5.3 Location for dataset


Many data analysts use data stored on their local machines. R will read data and
usually store this data in data frame format or class. When you read your data into
RStudio, you will see the dataset in the environment pane. RStudio reads the original
dataset and saves it to the RAM (random access memory). So you must know the size
of your computer RAM. How much is your RAM for your machine? The bigger the
RAM, the larger R can read and store your data in the computer’s memory.
The data read (in memory) will disappear once you close RStudio. But the source
dataset will stay in its original location, so there will be no change to your original
data (be happy!) unless you save the data frame in the memory and replace the orig-
inal file. However, we do not recommend you do this.
2.6 Upload Data to RStudio Cloud 23

FIGURE 2.6
New Project.

FIGURE 2.7
Project type.

2.6 Upload Data to RStudio Cloud


If users want to use data in the RStuio Cloud, they may have to upload the data to
the RStudio Cloud directory. They may also use RStudio Cloud to read data from the
Dropbox folder or Google Drive folder.
24 2 R Scripts and R Packages

FIGURE 2.8
Environment pane lists data in the memory. This RStudio IDE interface uses the Vi-
brant Ink theme which users can choose from the Appearance tab in Global Option
menu inside the Tools tab.

FIGURE 2.9
Upload tab on RStudio Cloud.
2.9 Bookdown 25

FIGURE 2.10
Examples of two books that are freely accessible on the Bookdown website and also
available as physical books.

2.7 More Resources on RStudio Cloud


There are a number of resources on RStudio Cloud. For example, on YouTube chan-
nel, there is RStudio Cloud for Education https://www.youtube.com/watch?v
=PviVimazpz8. Another good resource on YouTube is Working with R in Cloud
https://www.youtube.com/watch?v=SFpzr21Pavg

2.8 Guidance and Help


To see further guidance and help, users may register and join RStudio Community
at RStudio Community3 . Users can also ask questions on Stack Overflow4 . There are
also mailing list groups on specific topics but users have to subscribe to it.

2.9 Bookdown
RStudio has provided a website to host online books, the Bookdown5 . The books at
Bookdown are freely accessible online. There are some of the books that are available
on Amazon or other book depository as physical books such as ours.

3 https://community.rstudio.com/
4 https://stackoverflow.com/
5 https://bookdown.org/
26 2 R Scripts and R Packages

2.10 Summary
In this chapter, we describe R scripts and R packages. We also show how to write
simple R scripts and how to check if any specific R package is available on your ma-
chine and how to install it if it is not available. We recommend using RStudio Cloud
if you are very new to R. Working directory sometimes confuses new R users, hence
we also recommend all R users to create new RStudio Project for new analysis task.
There are resources available offline and online and many of them are freely accessi-
ble especially at the Bookdown6 website.

6 https://bookdown.org/
3
RStudio Project

3.1 Objectives
At the end of the chapter, we expect readers to be able to:
• link their RStudio with our datasets. The datasets are on our GitHub repository
• create an RStudio Cloud project using our GitHub repository
• create an RStudio project on a local machine using our GitHub repository

3.2 Introduction
In this chapter, we will guide you to have a similar folder and file structure to our
project for this book. This folder and file structure will help you run the codes with
minimal error risk. Furthermore, to achieve this, we will use the RStudio Project.
On the RStudio Project webpage1 , it says that RStudio projects make it straightfor-
ward to divide your work into multiple contexts, each with their working directory,
workspace, history and source documents.RStudio projects are associated with R
working directories. You can create an RStudio project:
• on RStudio Cloud or RStudio on your local machine
• In a brand new directory
• In an existing directory where you already have R code and data
• By cloning a version control (Git or Subversion) repository

1 https://support.rstudio.com/hc/en-us/articles/200526207-Using-RStudio-Projects

DOI: 10.1201/9781003296775-3 27
28 3 RStudio Project

FIGURE 3.1
The dataset repository on GitHub.

FIGURE 3.2
RStudio Cloud.

3.3 Dataset Repository on GitHub


We will use the our GitHub repository that contains the book’s datasets. The name
of the repository is data-for-RMed. To go to the repository, click on this link2

2 https://github.com/drkamarul/multivar_data_analysis.git
Another random document with
no related content on Scribd:
"It's such a common thing for trains to break down! Just as
likely as not!"

"Now, Giles! I do wish though, that I had gone to the


station to meet her. If it had not been for the rain—"

"Why didn't you ask me to go?"

"You!!"

Giles Cuthbert laughed. He was a rather largely-built man,


with long loose limbs, and a sunburnt face, not good-
looking as to feature, yet attractive from its humorous
geniality. His movements were deliberate and gentlemanly;
and he had a curious voice, soft, low, somewhat drawling,
but without affectation. The man altogether was entirely
natural. In age he might have been almost anywhere
between thirty and forty-five.

"How old is this little girl?"

"Jean! I don't exactly know. I have been trying to


remember; but the years do go so fast—and one doesn't
exactly like to inquire, because it seems as if one had not
cared. I could ask Jem, to be sure, but I always forget. She
must be—thirteen—fourteen—nearly fourteen! It is years
since I have seen any of those Trevelyans. Jean's mother
was a sweet woman but her father—No, I never did care for
Stewart."

"Hasn't she been here before?"

"I'm afraid I have never asked her. Things have come about
so, you know. One can't always be seeing all one's
relations, and Dulveriford is out of the way—so far north. I
shouldn't have thought of asking the child now, only Jem
put the idea into my head; he said she wanted a change, he
thought. She had had a fright or something, and somebody
wrote word to Jem that she was not looking well. O I
remember—there was a poor old gentleman found frozen to
death at night on the marshes; and Jean happened to be
there."

"Odd place for a child to happen to be in."

"Yes: I don't know how it was. Jem didn't say much, and I
have forgotten what he did say. He will tell us more when
he comes. It will not do to tease her with questions."

"And Jem is coming home himself when she is here?"

"Yes; so nice, isn't it? I don't know when he can manage it;
but as soon as possible. Jean has always been rather a
favourite of his. Dear Jem is so fond of children."

"Here comes the infant, aunt!"

"Now, my dear Giles, she isn't an infant I assure you. Why,


girls of thirteen and fourteen are nearly grown-up in these
days. They know so much more than I do, that I am half
afraid of them. All about atoms and cheese-mites, and laws
of nature, and gases, and how things grow—it quite
frightens one. Yes, there's the cab. I ordered the cab to be
at the station, and I sent word to the station-master to see
after Jean. I did want to send Maria, but cook doesn't like
Maria to go out when it rains. You see cook is rather
rheumatic. No tea yet! What can Maria be after? I almost
think I shall have to ring? O no, wait, please—she will come
to answer the front bell, and then I can speak."

"I don't see the precise connection between cook's


rheumatism and Maria's staying indoors. You seem to have
a pair of dragonesses in the kitchen."
"No, indeed—pray don't say such things, Giles! Somebody
might hear! It is only that I do like to keep people good-
tempered. Cook is such a nice worthy creature—I am sure,
if I were dying, she would do anything for me! But she does
get rather old, and if anything puts her out, the dinner is
spoilt."

"I should be disposed to prefer a cook who would do


anything for me while I was alive and well. What's that for?"

Mrs. Trevelyan had produced a neat square of white


knitting.

"Why—nothing, dear; only a very nice clergyman said last


year in a sermon that women ought to make a point of
doing needlework—at least, I think he meant that! So I
have always kept this at hand since, for emergencies. I am
afraid I don't do much—" regretfully—"but it is nicer to be
found busy. Jean might think she had such an idle cousin."

"Ah! A hint for me!" Giles lugged out a big volume of


natural history, seated himself, and opened at random.
"Wouldn't it be more effective to be found reading aloud?—
Surprised in the act of improving our minds?"

In a monotonous sing-song, Giles started off—"'The


Chlamyphores, which have ten teeth on each side of both
jaws, five toes on each foot, the anterior claws very large,
crooked, compressed, and furnishing, as in the Cabassous,
a very powerful cutting instrument adapted for digging. The
back is covered—' Hallo!"

Mrs. Trevelyan never could resist Giles' manner, and she


was in a paroxysm of laughter when the door opened to
admit, not the expected "little girl," but a tall damsel, with
pale oval face, and combination of shyness and reserve
crystallised into a resolute stillness.
Giles did not start up quickly, yet he was standing, before
Jean could actually enter; and his twinkling eyes took her in
at a glance, twinkling more than usual at his aunt's
mistake. The subdued "Hallo!" meant some measure of
surprise on his own part. "So—ho!" thought he. "That is
what Jem is after! Little girl, indeed!"

Mrs. Trevelyan's greeting was no less warm than if the


supposititious "infant" had really appeared. She took Jean
into her arms with repeated kisses, while Jean, unused to
be hugged by one virtual stranger before another and
absolute stranger, was conscious of an inward glow of
response, but outwardly shrank rather more into herself.

Giles Cuthbert came forward, as a member of the


household, to offer his meed of welcome, and thereby
recalled Mrs. Trevelyan to her duty.

"How stupid of me! I'm forgetting to introduce—my nephew,


Mr. Cuthbert, Jean—Miss Trevelyan, Giles. Of course you
have heard of Giles, dear, hundreds of times. He belongs to
us, you know, in a way. Now do sit down by the fire, and
warm yourself. You must be cold after travelling all day.
Dulveriford always seems so far north—almost out of the
world."

Jean was amused, for Dulveriford seemed to her the world's


centre.

"Come and sit down! The tea—O, that girl is gone."

"Too late, aunt!" Giles' hand was on the bell, unbidden, and
a sharp tinkle sounded.

"Now, Giles!" reproachfully. "And there really was not the


least need. I can hear her coming now along the passage.
After all, she was only waiting till Jean arrived—keeping the
tea nice and hot. My dear!—" to Jean. "I am astonished to
see such a grown-up young lady. Jem never gave me that
impression. Is it possible that you are only thirteen?"

"Sixteen last birthday!"

"Ah, that makes all the difference. I shall tell Jem, he really
must leave off speaking of you as a 'little girl,' because it
gives such a wrong impression. Why, I have actually been
telling Prince not to bark, or frighten you. Yes: this is Prince
—such a dear clever dog. I assure you, he understands
every single thing that is said to him."

"One has to avoid talking secrets before Prince. He is apt to


repeat them, on the sly."

"Now, Giles! What will Jean think of you? But really, the
dear dog is wonderfully clever. He reasons like any human
being. It is all nonsense to say that dogs can't reason,
because I know Prince does. Now I am wondering whom
Jean is supposed to be like. You are such a curious mixture,
my dear. A little of your father, certainly, and a little of your
mother! She was such a sweet woman! Ah, you have your
grandmother Trevelyan's chin! And your grandmother
Ingram's mouth! Only the way you shut your lips is like a
Trevelyan. Your eyes are nobody's—"

"Unless Miss Trevelyan's own," murmured Giles.

"I mean, nobody that I have seen. Of course, one could


trace them back, if one had lived long enough. Everything
comes from somewhere," said Mrs. Trevelyan, with gentle
positiveness and profundity. "But the way you use your
eyes, dear, is quite like an Ingram—like one branch of the
Ingram family, I mean. And you have the long Trevelyan
hand—"
"Don't be astonished," Giles put in softly. "My aunt always
appraises her guests in this fashion. Eyes, nose, mouth,
worth so much apiece; total, so much: samples of doctrine
of heredity."

"Now, Giles! But Jean understands, don't you, dear? Of


course I like to know how much of you belongs to your own
family. As for heredity—" the speaker folded dreamy hands,
preparing for a gentle canter on her pet hobby-horse—"I do
not see how anybody who knows anything of life can doubt
it. Why, look at handwriting alone! Jean makes the tails of
her g's and z's exactly like her old great-uncle Thomas—I
mean, like his writing—and yet she never even saw him,
and learnt from quite a different person. So you know that
must have come down by inheritance."

"To be sure, there is Mrs. Wiggins—she is our great lady


here, Jean, the wife of our squire—Mrs. Wiggins thinks a
mother can do anything in the world with her children, can
turn them out whatever shape she chooses. But that is a
great deal more easily said than done: and Mrs. Wiggins
only has one little girl. I always notice that it is the mothers
with only one child who think that they can do exactly
whatever they like with their children. If she had a dozen,
she would have found by this time, that all the training in
the world wouldn't make them all grow up alike. I have only
had one as it happens, but I never could model Jem. He
took his own shape, all I could do, dear fellow! And I am
sure I don't want him to be different, for he is all one could
wish. But still, you see what I mean. And I was one of
thirteen, so I do know something about it. Not two of us
turned out the same; and yet I'm sure there was not a
grain of difference made in the way we were treated."

Giles rose, and laid hands on the teapot.


"My dear Giles! What am I thinking about? Now that is the
worst of me. If I begin to talk, I forget everything. And poor
Jean half starved! No? But I am sure you must be. Have
you had a proper lunch? Only sandwiches! Could you not
eat an egg? Well, at all events you must take plenty of
bread-and-butter."

Giles relinquished the teapot, handed eatables, and found


his way to a seat nearer Jean, where he could study her at
leisure—for Jem's sake. He was at pains to draw her out,
endeavouring to bring about a relaxation of her too serious
face. Weather and journey were discussed for a while with
no particular result, and Prince was called up to be caressed
by Jean.

"Fond of reading," Giles asked presently, noting a glance


towards the bookcase. "You won't find dissertations here on
cheese-mites or atoms. That is the correct thing for a young
lady, I am told."

Jean's grave lips unbent.

"Wufflestone lags behind the age. But if you want to begin


studies in Phrenology, and all that sort of thing, you have
come to the right place. My aunt has a perfect library of
such literature."

"I should like to know more about Phrenology."

"The science of bumps! I am not sure that I shall not


recommend Lavater. Rather big volumes, but you can soon
skim them. Plenty about features—noses, chins, and so on.
You've no conception what an amount of character exists in
your neighbours' noses."

"Not really?" said Jean.


"Not a doubt of it. There's the sensible nose, and the stupid
nose; the cogitative nose and the unintellectual nose; the
ill-tempered nose and the sweet-tempered nose; the strong
nose and the weak nose."

"Is the weak nose always short?"

"By no means. You may have a strong short nose, or a


weak long one."

"Suppose I had a weak nose and a firm chin?" questioned


Jean.

"Inherited from opposite sides? No doubt you would be


weak in one direction, strong in another. It would mean a
perpetual contest between nose and chin—sometimes the
one victorious, sometimes the other."

Jean could not make out from his face whether he did or did
not mean what he said. He was politely serious, only his
eyes never ceased to twinkle.

"But that is too funny," she said, laughing.

"Immense amount of truth in it. Of come you must take old


Lavater cum grano salis. I shall have to conduct you
through something more modern while you are here. He
and the older phrenologists could only assign a particular
characteristic to a particular bump, and leave it there to
tyrannise over the individual. Modern wisdom begins to
suspect that the mind secretes the body, so to speak, like a
mollusc secreting its shell. According to which notion, a
man's character is not weak because his chin retreats, but
his chin retreats because his character is weak."

"Do you really believe that?"


Giles made a comical movement of his shoulder. "Why not?
How can one tell? Seems odd, when you come to think of it
—but everything is odd when you come to think of it. One
can't tell why a weak nature should secrete a retreating
chin, for instance; but one cannot get out of the puzzle,
because there is not the least doubt that retreating chins
and feeble characters do very often go together."

Jean drew a long breath of interest, and her eyes lighted up


with the fascination of a new idea. "How nice!" thought Mrs.
Trevelyan. "Dear Giles is quite lively—taking real trouble
with Jean. He must like her. She is not at all a bad-looking
girl—such regular features, and such a nice figure; and she
holds herself well, and is so very ladylike. I can't bear
fidgety people, and Jean doesn't fidget in the least. It will
save a great deal of trouble if Giles should take a fancy to
her. I can send them out to walk together, and get him to
show her the country round. Yes, she certainly is good-
looking, almost handsome, when her eyes grow so bright;
and she has a soft pleasant laugh. I am glad she has come.
I think I shall grow fond of Jean."

CHAPTER XII.

THE WIGGINSES AND MAIDENHAIR.


"The happy find
Equality of beauty everywhere
To feed on."
JEAN INGELOW.

MRS. TREVELYAN carried out her intention, and made


abundant use of Giles Cuthbert for the amusing of Jean.
Usually he was not amenable to such efforts, having a great
objection to needless trouble, but for once, he offered no
objection. If he had taken a fancy to the girl, that made all
the difference. The laziest people living will take trouble to
please themselves.

Giles liked ease; and his life had been such as to foster this
inclination. He had been born, not to wealth, but to
sufficient means; and he had been brought up from
childhood with the notion, that while it was correct and
gentlemanly to have some stated occupation, there was no
necessity for him to work hard. So he never had worked
hard. He had passed schooldays with tolerable credit; he
had gone through college with no particular reproach; and
he had consented to the law as a profession. There advance
was stayed. Some amount of preparatory reading was
accomplished; and Giles, finding law by no means to his
taste, threw it up. Months of dallying followed, and he
began to talk of a career in politics; but an opening had to
be waited for, and somebody suggested a lengthy tour on
the Continent, by way of preparation. Giles seized on this
notion, his indulgent father always consenting; and the tour
proved to be a very lengthy one indeed.

At twenty-five, his future mode of life was still uncertain,


when the sudden death of that father left him in easy
circumstances, unfettered, depending on nobody, while
nobody depended on him.

Thenceforward, he drifted away from any definite line of


life, simply pleasing his own fancies. He had no lands or
tenants to occupy him; nothing beyond a clear fifteen
hundred a year, to spend as he would. Since Giles had
expensive tastes, he found no difficulty in getting rid of that
amount yearly; indeed, at times, he outran it, though not
seriously, for debt was a matter which disturbed his peace
of mind, and Giles preferred to be comfortable, mentally as
well as bodily.

He lived a good deal in Town, travelled a good deal on the


Continent, saw a good deal of society, skimmed a good deal
of literature, and in a lazy fashion, found a good deal to do.
When money ran short, he would betake himself to Mrs.
Trevelyan's Cottage for a few weeks, and would "vegetate"
there in contented idleness. Mrs. Trevelyan had always been
his favourite aunt, and her home was always open to him.

There were capabilities in Giles, which, rightly trained and


used, might have resulted in a fine character; but it is
through resistance, not compliance, through hardship, not
ease, that harmony and beauty of character are evolved.
For the bare idea of grandeur, opposition overcome is a
necessity.

A boat swept downward by the current may be pretty: but


there is nothing "fine" about it. Our higher admiration is
reserved for the same boat manfully fighting its way up-
stream, overcoming difficulty inch by inch through resolute
effort. A tree, reared in a hothouse, with soil, warmth, food,
amply provided, may be a most successful specimen of its
kind; but it suggests no thought of victory; it is only the
pampered darling of scientific care. ᴵ The gnarled and
twisted oak, which has fought its way through countless
storms, has mastered unnumbered hardships, stands out as
a type of the truly great, of the morally grand.

ᴵ I must acknowledge here a debt to Mr. Ruskin.

Giles Cuthbert had fought through few difficulties, had


mastered few oppositions. His had been a smooth and
leisurely existence; hard duties had not come to him as a
necessity; and he had not turned aside to seek them for the
sake of others. The consequent growth of character was
easy, self-satisfying, pleasant in many respects, but by no
means great.

Jean had been surrounded from babyhood by a far more


embracing environment, had been compelled habitually to
march in the face of her own inclinations, had had to
wrestle through countless oppositions. Instead of finding all
difficulties cleared away from her path, she had been
trained to meet and grapple with them. The resisting vigour
of her growth was gradually shaping a woman's character of
fine outlines, pure, straightforward, self-denying—such a
growth as could never have resulted from a spoilt and
cosseted existence.

Perhaps the very contrast between the two drew them


together. Jean liked Cuthbert despite his laziness, which
was, after all, more mental than bodily. He was ready
enough for physical exertion, if an adequate object lay
ahead. It was steady work from which he turned with
loathing; work which had to be done, whatever his passing
mood might be. He was quaint, amusing, and full of fun.
Jean had never before laughed to such an extent; indeed,
when once it becomes an admitted fact that somebody is
funny, very little wit is needed to set people's risible
muscles in motion.

She liked Mr. Cuthbert, certainly. Not as she liked Jem; not
as she liked Cyril; not with liking to be named beside her
love for Oswald; still, she found him a pleasant companion.
Of course he was ages older than herself; why, he actually
had a grey hair or two visible. Jean set him down loosely in
her girlish mind as "somewhere about forty or fifty," which
to a girl not yet seventeen, sounds venerable. On the
whole, she was inclined to regard him as a species of
adopted uncle, much nearer to her father than to herself in
standing.

Mrs. Trevelyan, knowing Giles to lack about fifteen years of


the fifty, did not view matters in precisely the same light.

"To be sure he is almost twenty years older than Jean," she


thought; "and that is a great deal even on the right side.
Jean is such a child too. It would be absurd to think of
anything at present. But some day, three or four years
hence—if Giles is not married then, I don't see why it should
be impossible. Such a wife as Jean, might be the making of
Giles. If anybody could ever get him to work, it would be
some one like Jean—always busy. If not, there is no reason
why they should not live comfortably on fifteen hundred a
year. It sounds like wealth to me! Only of course, Giles
could not get quite so many waist-coats, or have such very
expensive cigars. He seems taken with the child; and Jean
is friendly with him." Mrs. Trevelyan smiled contentedly over
her little castle in the air. "I shall write and tell Stewart how
well she is looking. He really must let me keep her six
weeks or two months now she is here."
Five weeks at the Cottage! Jean could better have believed
in five months. The novel ideas and events of each day in
Wufflestone lengthened it out to her mental sensations,
while each hour flew with speed.

Everything here was different from home. The little dainty


house and garden; the dilettante manner of living; the slow
meals; the placid tempers; the abundant leisure; the dearth
of needlework; the absence of fault-finding; the supply of
new books—all these were unwonted and for the time
charming.

Dearly as Jean loved Dulveriford Rectory, and the


associations of her childhood, she was of too vivid and
malleable a nature not to rejoice in the new knowledge
springing from now surroundings. Jean learnt more of ease,
of freedom, of frank girlish simplicity in those weeks, than
in all her previous life. She learnt to hold her own in good-
humoured argument; to stand being laughed at, and
sometimes even to make others laugh; to be mistress of
herself, not alone in action but also in manner. Rigid reserve
was fast thawing, and she allowed herself to be affectionate
to Mrs. Trevelyan.

This easy mode of existence did not spoil Jean. The reign of
duty in her life was too strong to be so soon disturbed; and
while her nature, like all sensitive natures—by "sensitive" I
do not mean ill-tempered—was peculiarly open to new
impressions, her principles were firm. She had no wish to
live always such a life; but there was a wondrous charm in
its freshness.

Perhaps the most curious part of these weeks to Jean, was


the finding herself so completely cut off from home
interests. Neither Mr. Trevelyan nor Madame Collier were
good correspondents, and Jean seldom had a line from
either; nor did Evelyn write. Mrs. Trevelyan inquired duly at
intervals after Jean's home-people; but she knew nothing of
Dulveriford, nothing of the Brow, the Gorge, or Dutton,
nothing of Miss Devereux, Cyril, Evelyn, or the General.

Jean had been at first glad to escape questioning about late


events, yet it seemed singular that Mrs. Trevelyan should
not have heard more. Jean knew that Jem liked and
admired Evelyn—as who did not?—and she wondered that
he had not more fully related to his mother the tragical end
of Evelyn's married life.

Mrs. Trevelyan did indeed once allude to—"that poor old


gentleman, Jean, who was frozen to death—General What-
was-his-name? And you were actually there yourself and
saw it all, poor child!"

But Jean's constrained answer drew no further show of


interest, and the matter was not again spoken of.

Jem's proposed appearance had been repeatedly deferred,


through pressure of work.

"Don't let Jean go home, until I have seen her," he wrote


more than once to his mother, and at length a day was fixed
early in April.

"The very day of the party at the Hall," said Mrs. Trevelyan,
after reading the letter. "Well, that cannot be helped. If Jem
arrives in time, he must go with us; but the evening train is
more likely, he says."

"What party?" asked Giles.

"Mrs. Wiggins—to afternoon tea. I suppose it is partly in


honour of Jean; so of course we must go. She made a great
point of my taking Jean; and she wants you too, Giles. I
told her I would try: but I was not hopeful. If you could
make up your mind once—"

"Thanks, ma'am!"

Giles' tone was sufficient, and Mrs. Trevelyan sank into


acquiescence.

Jean would gladly have escaped the ordeal, had freedom of


choice been permitted; but Mrs. Trevelyan was so placidly
sure of her delight in the treat, that nobody could have the
heart to undeceive her. Jean disliked strangers, and was not
captivated by Mrs. Wiggins. Still, the thing had to be
endured; and she was prepared to endure heroically.

The Wigginses were so excruciatingly correct that they


introduced nobody to anybody. This was the rule of the
house. Indiscriminate introductions are not "the thing;" but
neither is it to be accounted "the thing" to plant an absolute
stranger amid a group of people, all mutually acquainted,
who will leave her socially out in the cold.

Those who meet under the roof of a common friend ought,


no doubt, to be able to exchange ideas without being
formally named one to another; and the absence of
introductions presupposes this possibility. English people
are not yet cured of shyness, however; and even while
tabooing introduction, they too often wait rigidly for it.

Jean found this to her cost. She was welcomed cordially,


separated from Mrs. Trevelyan, placed among a group of
strangers, and there left to her own devices. She had seen
a good many Wufflestone people in the last few weeks, but
not a single acquaintance happened to be "within hailing
distance." Around were grouped ladies, old and young, all
ladylike in appearance, and all seemingly well acquainted.
They chatted freely together, and most of them used their
eyes upon Jean, but none spoke to her.

For nearly an hour she sat thus, wondering how long the
condition of things was to last. Tea brought round formed
her one diversion. She could not see Mrs. Trevelyan. Mrs.
Wiggins once flitted up for a remark, and flitted away again,
taking no further trouble.

Internally, Jean grow indignant. It seemed unfair and


uncourteous that she should be pressed to come to the
house, and that not an effort should be made for her
entertainment. She thought with longing of the cosy cottage
drawing-room, and of Giles' amusing conversation. Then
she wandered to recollections of home and of Evelyn—the
gentle and gracious Evelyn, who would never so neglect her
guests.

"Not even if she disliked them," thought Jean.

A few yards distant was a closed conservatory door. Jean


growing tired of her position, rose at length and made her
way thither. The movement drew general attention, for
which Jean cared little. She glanced across the room,
caught a glimpse of Mrs. Trevelyan on a distant sofa,
exchanged a smile with her, then wandered off alone into
the deserted conservatory. To close the glass door was her
first care, with a sense of satisfaction in shutting off those
unsociable people. Then she stole happily from plant to
plant, studying the fair make of one, inhaling the sweetness
of another, entirely content.

From the long conservatory, a door led into a fernery, where


green things clustered in a bower, with ceaseless trickle of
water. Jean was charmed. She sat down on a jutting corner
of rock-work, opposite a great mass of large-leaved
Maidenhair, the delicate new fronds of which were salmon-
tinted, shading into green; and there she gave herself up to
enjoyment.

Somebody coming in did not at once arouse her. Not till a


shadow fell across the salmon gleam of the Maidenhair, did
she look up.

"Jem!"

"I managed to get off pretty early. How do you do, Jean?
Giles sent me after you both."

"Cousin Chrissie is in the drawing-room."

"I have seen her, and paid my respects to Mrs. Wiggins."

"Was I wrong to come out? I did get so tired of doing


nothing; and nobody spoke to me."

"Why?"

"I don't know. Because I am a stranger, I suppose. But


should you not have thought that a reason the other way?
People ought to be kind. And I am only a girl, so it was
hardly my place to begin upon them. I don't mind now. It is
delicious among the ferns. Did cousin Chrissie send you to
me?"

"She said I might find you here. Shall we go into the


garden?"

"May we? Would it be polite?"

"Somebody is going to drive my mother home, and she has


undertaken to make your apologies to Mrs. Wiggins. I told
her I should carry you off. It is all right. We know Mrs.
Wiggins well."

He opened the outer door, and Jean questioned no further.


One of their old free talks together would be delightful. She
never felt so much at her ease with anybody as with Jem,
because never so sure of being understood.

Conversation did not at once flow. Jem led away from the
house, through the garden, into a lane beyond, which Jean
knew to mean a considerable round, before they could get
home. She was ready for any amount of exercise—only,
would not his mother want him? And what made Jem so
grave? Had he something on his mind?

The lane curved hither and thither, in aimless fashion:


having on either side a hedge, with trees at intervals.
Slanting rays from the sun lighted up the right hand hedge
with a dim glory, and fell upon the patches of dull lichen
which decorated an aged elm trunk, smitten into
ruggedness by prolonged hardships of wind and weather.

"Things are beautiful everywhere," said Jean. "Even in a flat


country like this."

"It is not Dulveriford," responded Jem. And as if this


supplied the opening for which he had waited, he went on—

"How is your father? And—" in a lower voice, "Mrs. Villiers?"

Jean had much to tell, for she found that he knew little.
Home interests never far distant came on her in a rush, as
he listened, drawing out further details by a murmured
syllable now and then. After long silence on Dulveriford
subjects, it was natural to pour out; and she gave abundant
particulars of the evening which had left Mrs. Villiers a
widow. Only, about the little boudoir scene between
husband and wife, a scene of which she had been an
unwilling witness, and which had been rendered by after-
events doubly sacred, Jean said not a word.

"It was an ordeal for you," Jem remarked.

"One could not think of oneself at such a time. For Evelyn it


was terrible. To be there on the spot, and to see him! But
even my father could not keep her back."

"And since then she has been—"

"Very, very sad. I don't think I should have expected—But


nobody ever knows beforehand. She seems to feel that
everything is gone, and that her life is at an end."

"You would not have expected what?"

"Ought I to say it? One feels some things that one does not
like to speak out. Only—to you—may I not? He was so good
and kind—but still so very much older, and different in every
way. Yet now one might fancy that her life had been
perfect; even though one knows how things really were."

"The manner of her loss must have intensified it. The shock
and suddenness—"

"I dare say that had something to do with her feeling. It


was all very dreadful. It couldn't help being so. And besides
—" Jean hesitated, falling into a slower walk. "I suppose it is
sometimes worse to lose anybody, if—Worse in one way, I
mean—"

"If there has not been perfect oneness? If there has been
friction?"

You might also like