Tides_Amo
Jones Jones
https://ebookmass.com/product/boneyard-tides_amo-jones-jones/
Plant Pathology 3rd Edition R. S. Mehrotra
https://ebookmass.com/product/plant-pathology-3rd-edition-r-smehrotra/
Ways to be Blameworthy: Rightness, Wrongness, and Responsibility Elinor Mason
https://ebookmass.com/product/ways-to-be-blameworthy-rightnesswrongness-and-responsibility-elinor-mason/
The Oxford Handbook of Positive Psychology 3rd Edition C. R. Snyder
https://ebookmass.com/product/the-oxford-handbook-of-positivepsychology-3rd-edition-c-r-snyder/
Basic Electrical Engineering 3rd Edition Ravish R. Singh
https://ebookmass.com/product/basic-electrical-engineering-3rdedition-ravish-r-singh/
TheRBook
Thisthirdeditionfirstpublished2023
©2023JohnWiley&SonsLtd
EditionHistory:JohnWiley&SonsLtd(1e,2007;2e,2013)
Allrightsreserved.Nopartofthispublicationmaybereproduced,storedinaretrievalsystem,ortransmitted,inanyformorbyany means,electronic,mechanical,photocopying,recordingorotherwise,exceptaspermittedbylaw.Adviceonhowtoobtain permissiontoreusematerialfromthistitleisavailableat http://www.wiley.com/go/permissions
TherightofElinorJones,SimonHardenandMichaelJ.Crawleytobeidentifiedastheauthorsofthisworkhasbeenassertedin accordancewithlaw.
RegisteredOffices
JohnWiley&Sons,Inc.,111RiverStreet,Hoboken,NJ07030,USA
JohnWiley&SonsLtd,TheAtrium,SouthernGate,Chichester,WestSussex,PO198SQ,UK
EditorialOffice
9600GarsingtonRoad,Oxford,OX42DQ,UK
Fordetailsofourglobaleditorialoffices,customerservices,andmoreinformationaboutWileyproductsvisitusat www.wiley.com
Wileyalsopublishesitsbooksinavarietyofelectronicformatsandbyprint-on-demand.Somecontentthatappearsinstandard printversionsofthisbookmaynotbeavailableinotherformats.
LimitofLiability/DisclaimerofWarranty
Inviewofongoingresearch,equipmentmodifications,changesingovernmentalregulations,andtheconstantflowofinformation relatingtotheuseofexperimentalreagents,equipment,anddevices,thereaderisurgedtoreviewandevaluatetheinformation providedinthepackageinsertorinstructionsforeachchemical,pieceofequipment,reagent,ordevicefor,amongotherthings, anychangesintheinstructionsorindicationofusageandforaddedwarningsandprecautions.Whilethepublisherandauthors haveusedtheirbesteffortsinpreparingthiswork,theymakenorepresentationsorwarrantieswithrespecttotheaccuracyor completenessofthecontentsofthisworkandspecificallydisclaimallwarranties,includingwithoutlimitationanyimpliedwarranties ofmerchantabilityorfitnessforaparticularpurpose.Nowarrantymaybecreatedorextendedbysalesrepresentatives,written salesmaterialsorpromotionalstatementsforthiswork.Thefactthatanorganization,website,orproductisreferredtointhiswork asacitationand/orpotentialsourceoffurtherinformationdoesnotmeanthatthepublisherandauthorsendorsetheinformationor servicestheorganization,website,orproductmayprovideorrecommendationsitmaymake.Thisworkissoldwiththe understandingthatthepublisherisnotengagedinrenderingprofessionalservices.Theadviceandstrategiescontainedherein maynotbesuitableforyoursituation.Youshouldconsultwithaspecialistwhereappropriate.Further,readersshouldbeaware thatwebsiteslistedinthisworkmayhavechangedordisappearedbetweenwhenthisworkwaswrittenandwhenitisread. Neitherthepublishernorauthorsshallbeliableforanylossofprofitoranyothercommercialdamages,includingbutnotlimitedto special,incidental,consequential,orotherdamages.
LibraryofCongressCataloging-in-PublicationData
Names:Jones,Elinor(AssociateProfessor),author.|Harden,Simon, author.|Crawley,MichaelJ.,author.
Title:TheRbook/ElinorJones,SimonHarden,andMichaelJ.Crawley.
Description:Thirdedition.|Hoboken,NJ:Wiley,2022.|Includes bibliographicalreferencesandindex.
Identifiers:LCCN2022008352(print)|LCCN2022008353(ebook)|ISBN 9781119634324(cloth)|ISBN9781119634409(adobepdf)|ISBN 9781119634430(epub)
Subjects:LCSH:R(Computerprogramlanguage)|Mathematical statistics–Dataprocessing.
Classification:LCCQA276.45.R3J6622022(print)|LCCQA276.45.R3 (ebook)|DDC005.5/5–dc23/eng20220528
LCrecordavailableathttps://lccn.loc.gov/2022008352
LCebookrecordavailableathttps://lccn.loc.gov/2022008353
Coverdesign:Wiley
Coverimage:CourtesyofSimonHarden;©enjoynz/GettyImages
Setin10/12ptHelveticaLTStdbyStraive,Chennai,India
1 GettingStarted 1
1.1Navigatingthebook1
1.1.1Howtousethisbook1
1.2 R vs.RStudio3
1.3Installing R andRStudio3
1.4UsingRStudio4
1.4.1Using R directlyviatheconsole5
1.4.2Usingtexteditors5
1.5TheComprehensive R ArchiveNetwork7
1.5.1Manuals7
1.5.2Frequentlyaskedquestions8
1.5.3Contributeddocumentation8
1.6Packagesin R 8
1.6.1Contentsofpackages9
1.6.2Findingpackages9
1.6.3Installingpackages9
1.7Gettinghelpin R 11
1.7.1Workedexamplesoffunctions12
1.7.2Demonstrationsof R functions13
1.8Goodhousekeeping13
1.8.1Variabletypes13
1.8.2What’sloadedordefinedinthecurrentsession14
1.8.3Attachinganddetachingobjects14
1.8.4Projects15
1.9Linkingtoothercomputerlanguages15 References 15
2 TechnicalBackground17
2.1Mathematicalfunctions17
2.1.1Logarithmsandexponentials18
2.1.2Trigonometricfunctions19
2.1.3Powerlaws20
2.1.4Polynomialfunctions22
2.1.5Gammafunction24
2.1.6Asymptoticfunctions25
2.1.7Sigmoid(S-shaped)functions27
2.1.8Biexponentialfunction28
2.1.9Transformationsofmodelvariables29 2.2Matrices 30
2.2.1Matrixmultiplication31
2.2.2Diagonalsofmatrices32
2.2.3Determinants33
2.2.4Inverseofamatrix35
2.2.5Eigenvaluesandeigenvectors36
2.2.6Solvingsystemsoflinearequationsusingmatrices39 2.3Calculus 40
2.3.1Differentiation40
2.3.2Integration41
2.3.3Differentialequations42
3 Essentialsofthe R Language55
3.1Calculations56
3.1.1Complexnumbers57
3.1.2Rounding58
3.1.3Arithmetic59
3.1.4Modulararithmetic61
3.1.5Operators62 3.1.6Integers63 3.2Namingobjects64
3.4.2Testingforequalityofrealnumbers69
3.4.3Testingforequalityofnon-numericobjects70
3.4.4Evaluationofcombinationsof TRUE and FALSE 72
3.4.5Logicalarithmetic73
3.5Generatingsequences74
3.5.1Generatingrepeats76
3.5.2Generatingfactorlevels77
3.6Classmembership78
3.7Missingvalues,infinity,andthingsthatarenotnumbers82
3.7.1Missingvalues: NA 83
3.8Vectorsandsubscripts86
3.8.1Extractingelementsofavectorusingsubscripts87
3.8.2Classesofvector89
3.8.3Namingelementswithinvectors90
3.9Workingwithlogicalsubscripts91 3.10Vectorfunctions93
3.10.1Obtainingtablesusing tapply() 95
3.10.2Applyingfunctionstovectorsusing sapply() 97
3.10.3The aggregate() functionforgroupedsummarystatistics99
3.10.4Parallelminimaandmaxima: pmin and pmax 100
3.10.5Findingclosestvalues101
3.10.6Sorting,ranking,andordering102
3.10.7Understandingthedifferencebetween unique() and duplicated() 104
3.10.8Lookingforrunsofnumberswithinvectors106
3.10.9Sets: union(), intersect(),and setdiff() 108
3.11Matricesandarrays109
3.11.1Matrices111
3.11.2Namingtherowsandcolumnsofmatrices112
3.11.3Calculationsonrowsorcolumnsofmatrices113
3.11.4Addingrowsandcolumnstomatrices115
3.11.5The sweep() function117
3.11.6Applyingfunctionstomatrices119
3.11.7Scalingamatrix120
3.11.8Usingthe max.col() function121
3.11.9Restructuringamulti-dimensionalarrayusing aperm() 123
3.12Randomnumbers,sampling,andshuffling126
3.12.1The sample() function127
3.13Loopsandrepeats128
3.13.1Morecomplicated while() loops131
3.13.2Loopavoidance133
3.13.3Theslownessofloops134
3.13.4Donot‘grow’datasetsbyconcatenationorrecursivefunctioncalls135
3.13.5Loopsforproducingtimeseries136
3.14Lists 138
3.14.1Summarisinglistsand lapply() 140
3.14.2Manipulatingandsavinglists142
3.15Text,characterstrings,andpatternmatching147
3.15.1Pastingcharacterstringstogether149
3.15.2Extractingpartsofstrings150
3.15.3Countingthingswithinstrings151
3.15.4Upperandlowercasetext153
3.15.5The match() functionandrelationaldatabases153
3.15.6Patternmatching155
3.15.7Substitutingtextwithincharacterstrings159
3.15.8Locationsofapatternwithinavector160
3.15.9Comparingvectorsusing %in% and which() 162
3.15.10Strippingpatternedtextoutofcomplexstrings163
3.16Datesandtimesin R 164
3.16.1Readingtimedatafromfiles165
3.16.2Calculationswithdatesandtimes168
3.16.3Generatingsequencesofdates170
3.16.4Calculatingtimedifferencesbetweentherowsofadataframe173
3.16.5Regressionusingdatesandtimes175
3.17Environments177
3.17.1Using attach() ornot!178
3.17.2Using attach() inthisbook180
3.18Writing R functions181
3.18.1Arithmeticmeanofasinglesample181
3.18.2Medianofasinglesample182
3.18.3Geometricmean183
3.18.4Harmonicmean184
3.18.5Variance186
3.18.6Varianceratiotest187
3.18.7Usingthevariance189
3.18.8Plotsanddeparsinginfunctions191
3.18.9The switch() function192
3.18.10Argumentsinourfunction193
3.18.11Errorsinourfunctions195
3.18.12Outputsfromourfunction196
3.19Structureof R objects200
3.20Writingfrom R toafile203
3.20.1Savingdataobjects203
3.20.2Savingcommandhistory204
3.20.3Savinggraphicsorplots204
3.20.4Savingdataforaspreadsheet204
3.20.5Savingoutputfromfunctionstoafile205
3.21Tipsforwriting R code206 References 206
4 DataInputandDataframes207 4.1Workingdirectory207
4.2Datainputfromfiles208
4.2.1Datainputusing read.table() and read.csv() 208
4.2.2Inputfromfilesusing scan() 210
4.2.3Readingdatafromafileusing readLines() 213
4.3Datainputdirectlyfromtheweb215
4.4Built-indatafiles215
4.5Dataframes216
4.5.1Subscriptsandindices220
4.5.2Selectingrowsfromthedataframeatrandom222
4.5.3Sortingdataframes223
4.5.4Usinglogicalconditionstoselectrowsfromthedataframe229
4.5.5Omittingrowscontainingmissingvalues, NA 232
4.5.6Adataframewithrownamesinsteadofrownumbers235
4.5.7Creatingadataframefromanotherkindofobject236
4.5.8Eliminatingduplicaterowsfromadataframe239
4.5.9Datesindataframes239
4.6Usingthe match() functionindataframes241
4.6.1Mergingtwodataframes243
4.7Addingmarginstoadataframe245
4.7.1Summarisingthecontentsofdataframes247
5 Graphics 249
5.1Plottingprinciples249
5.1.1Axeslabelsandtitles251
5.1.2Plottingsymbolsandcolours251
5.1.3Savinggraphics254
5.2Plotsforsinglevariables255
5.2.1Histogramsvs.barcharts255
5.2.2Histograms256
5.2.3Densityplots260
5.2.4Boxplots261
5.2.5Dotplots262
5.2.6Barcharts263
5.2.7Piecharts264
5.3Plotsforshowingtwonumericvariables265
5.3.1Scatterplot265
5.3.2Plotswithmanyidenticalvalues270
5.4Plotsfornumericvariablesbygroup272
5.4.1Boxplotsbygroup272
5.4.2Dotplotsbygroup274
5.4.3Aninferior(butpopular)option275
5.5Plotsshowingtwocategoricalvariables277
5.5.1Groupedbarcharts277
5.5.2Mosaicplots277
5.6Plotsforthree(ormore)variables279
5.6.1Plotsofallpairsofvariables279
5.6.2Incorporatingathirdvariableonascatterplot280
5.6.3Basic3Dplots281
5.7Trellisgraphics283
5.7.1Panelboxplots285
5.7.2Panelscatterplots286
5.7.3Panelbarplots289
5.7.4Panelsforconditioningplots290
5.7.5Panelhistograms291
5.7.6Morepanelfunctions292
5.8Plottingfunctions293
5.8.1Two-dimensionalplots293
5.8.2Three-dimensionalplots295
References 295
6 GraphicsinMoreDetail297
6.1Moreoncolour297
6.1.1Colourpaletteswithcategoricaldata297
6.1.2The RColorBrewer package299
6.1.3Foregroundcolours302
6.1.4Backgroundcolours302
6.1.5Backgroundcolourforlegends303
6.1.6Differentcoloursfordifferentpartsofthegraph304
6.1.7Fullcontrolofcoloursinplots305
6.1.8Cross-hatchingandgreyscale307
6.2Changingthelookofgraphics308
6.2.1Shapeandsizeofplot308
6.2.2Multipleplotsononescreen309
6.2.3Tickmarksandassociatedlabels309
6.2.4Fontoftext311
6.3Addingitemstoplots311
6.3.1Addingtext311
6.3.2Addingsmoothparametriccurvestoascatterplot313
6.3.3Fittingnon-parametriccurvesthroughascatterplot314
6.3.4Connectingobservations316
6.3.5Addingshapes321
6.3.6Addingmathematicalandothersymbols322
6.4Thegrammarofgraphicsand ggplot2 326
6.4.1Basicstructure327
6.4.2Examples327
6.5Graphicscheatsheet330
6.5.1Textjustification, adj
6.5.2Annotationofgraphs, ann
6.5.3Delaymovingontothenextinaseriesofplots, ask
6.5.4Controlovertheaxes, axis
6.5.5Backgroundcolourforplots, bg 333
6.5.6Boxesaroundplots, bty 334
6.5.7Sizeofplottingsymbolsusingthecharacterexpansionfunction, cex 334
6.5.8Changingtheshapeoftheplottingregion, plt 335
6.5.9Locatingmultiplegraphsinnon-standardlayoutsusing fig 336
6.5.10Twographswithacommon X scalebutdifferent Y scalesusing fig 336
6.5.11The layout function338
6.5.12Creatingandcontrollingmultiplescreensonasingledevice340
6.5.13Orientationofnumbersonthetickmarks, las 341
6.5.14Shapesfortheendsandjoinsoflines, lend and ljoin 342
6.5.15Linetypes, lty 343
6.5.16Linewidths, lwd 343
6.5.17Severalgraphsonthesamepage, mfrow and mfcol 344
6.5.18Marginsaroundtheplottingarea, mar 345
6.5.19Plottingmorethanonegraphonthesameaxes, new 346
6.5.20Outermargins, oma 347
6.5.21Packinggraphsclosertogether348
6.5.22Squareplottingregion, pty 350
6.5.23Characterrotation, srt 350
6.5.24Rotatingtheaxislabels351
6.5.25Tickmarksontheaxes351
6.5.26Axisstyles353
6.5.27Summary353 References 357
7 Tables 359
7.1Tabulatingcategoricalordiscretedata359
7.1.1Tablesofcounts359
7.1.2Tablesofproportions360
7.2Tabulatingsummariesofnumericdata362
7.2.1Generalsummariesbygroup362
7.2.2Bespokesummariesbygroup364
7.3Convertingbetweentablesanddataframes367
7.3.1Fromatabletoadataframe367
7.3.2Fromadataframetoatable370 Reference 371
8 ProbabilityDistributionsin R 373
8.1Probabilitydistributions:thebasics374
8.1.1Discreteandcontinuousprobabilitydistributions374
8.1.2Describingprobabilitydistributionsmathematically374 8.1.3Independence375
8.2Probabilitydistributionsin R 376
8.3Continuousprobabilitydistributions377
8.3.1TheNormal(orGaussian)distribution377
8.3.2TheUniformdistribution380
8.3.3TheChi-squareddistribution381
8.3.4TheFdistribution382
8.3.5Student’s t distribution383
8.3.6TheGammadistribution385
8.3.7TheExponentialdistribution386
8.3.8TheBetadistribution387
8.3.9TheLognormaldistribution388
8.3.10TheLogisticdistribution389
8.3.11TheWeibulldistribution390
8.3.12MultivariateNormaldistribution390
8.4Discreteprobabilitydistributions392
8.4.1TheBernoullidistribution392
8.4.2TheBinomialdistribution392
8.4.3TheGeometricdistribution395
8.4.4TheHypergeometricdistribution397
8.4.5TheMultinomialdistribution398
8.4.6ThePoissondistribution399
8.4.7TheNegativeBinomialdistribution400
8.5Thecentrallimittheorem402 References 404
9 Testing
9.1.1Definingthequestiontobetested406
9.1.2Assumptions408
9.1.3Interpretingresults408
9.2Continuousdata410
9.2.1Singlepopulationaverage410
9.2.2Twopopulationaverages412
9.2.3Multiplepopulationaverages414
9.2.4Populationdistribution415
9.2.5Checkingandtestingfornormality417
9.2.6Comparingvariances419
9.3Discreteandcategoricaldata421
9.3.1Signtest421
9.3.2Testtocompareproportions423
9.3.3Contingencytables427
9.3.4Testingcontingencytables429
9.4Bootstrapping431
9.5Multipletests433
9.6Powerandsamplesizecalculations434
10 Regression
10.1Thesimplelinearregressionmodel440
10.1.1Modelformatandassumptions440
10.1.2Buildingasimplelinearregressionmodel443
10.2Themultiplelinearregressionmodel446
10.2.1Modelformatandassumptions446
10.2.2Buildingamultiplelinearregressionmodel447
10.2.3Categoricalcovariates449
10.2.4Interactionsbetweencovariates454
10.3Understandingtheoutput458
10.3.1Residuals458
10.3.2Estimatesofcoefficients459
10.3.3Testingindividualcoefficients459
10.3.4Residualstandarderror460
10.3.5 R2 anditsvariants460
10.3.6Theregression F-test460
10.3.7ANOVA:Samemodel,differentoutput461
10.3.8Extractingmodelinformation464 10.4Fittingmodels465
10.4.1Theprincipleofparsimony465 10.4.2Firstplotthedata467
10.4.3Comparingnestedmodels468
10.4.4Comparingnon-nestedmodels470
10.4.5Dealingwithlargenumbersofcovariates471
10.5Checkingmodelassumptions473
10.5.1Residualsandstandardisedresiduals473
10.5.2Checkingforlinearity474
10.5.3Checkingforhomoscedasticityoferrors476
10.5.4Checkingfornormalityoferrors476
10.5.5Checkingforindependenceoferrors478
10.5.6Checkingforinfluentialobservations479
10.5.7Checkingforcollinearity481
10.5.8Improvingfit483
10.6Usingthemodel491
10.6.1Interpretationofmodel491
10.6.2Makingpredictions495
10.7Furthertypesofregressionmodelling497
References 498
11 GeneralisedLinearModels499 11.1HowGLMswork499 11.1.1Errorstructure499
11.1.2Linearpredictor500 11.1.3Linkfunction501 11.1.4Modelchecking502
11.1.5Interpretationandprediction506
11.2CountdataandGLMs507
11.2.1Astraightforwardexample508
11.2.2Dispersion511
11.2.3AnalternativetoPoissoncounts516
11.3CounttabledataandGLMs522
11.3.1Log-linearmodels522
11.3.2Allcovariatesmightbeuseful522 11.3.3Spineplot534
11.4ProportiondataandGLMs537
11.4.1Theoreticalbackground538
11.4.2Logisticregressionwithbinomialerrors541
11.4.3Predicting x from y 544
11.4.4Proportiondatawithcategoricalexplanatoryvariables545
11.4.5BinomialGLMwithorderedcategoricalcovariates550
11.4.6BinomialGLMwithcategoricalandcontinuouscovariates556
11.4.7Revisitinglizards559
11.5BinaryResponseVariablesandGLMs560
11.5.1Astraightforwardexample562
11.5.2Graphicaltestsofthefitofthelogisticcurvetodata564
11.5.3Mixedcovariatetypeswithabinaryresponse567
11.5.4Spineplotandlogisticregression570
11.6BootstrappingaGLM574 References 577
12 GeneralisedAdditiveModels579
12.1Smoothingexample580
12.2StraightforwardexamplesofGAMs583
12.3BackgroundtousingGAMs588
12.3.1Smoothing588
12.3.2Suggestionsforusing gam() 588
12.4MorecomplexGAMexamples589
12.4.1Backto Ozone 590
12.4.2Anexamplewithstronglyhumpeddata592
12.4.3GAMswithbinarydata596
12.4.4Three-dimensionalgraphicoutputfrom gam 598
References 599
13 Mixed-EffectModels601
13.1Regressionwithcategoricalcovariates601
13.2Analternativemethod:randomeffects602
13.3Commondatastructureswhererandomeffectsareuseful603
13.3.1Nested(hierarchical)structures604
13.3.2Non-nestedstructures604
13.3.3Longitudinalstructures605
13.4 R packagestodealwithmixedeffectsmodels605
13.4.1The nlme package605
13.4.2The lme4 package606
13.4.3Methodsforfittingmixedmodels606
13.5Examplesofimplementingrandomeffectmodels607
13.5.1Multileveldata(twolevels)607
13.5.2Multileveldata(threelevels)611
13.5.3Designedexperiment:split-plot614
13.5.4Longitudinaldata617
13.6Generalisedlinearmixedmodels622
13.6.1Logisticmixedmodel622
13.7Alternativestomixedmodels625
References 625
14 Non-linearRegression627
14.1Example:modellingdeerjawbonelength628
14.1.1Anexponentialmodelforthedeerdata629
14.1.2AMichaelis–Mentenmodelforthedeerdata632
14.1.3ComparisonoftheexponentialandtheMichaelis–Mentenmodel634
14.2Example:groupeddata634
14.3Self-startingfunctions638
14.3.1Self-startingMichaelis–Mentenmodel638
14.3.2Self-startingasymptoticexponentialmodel640
14.3.3Self-startinglogistic642
14.3.4Self-startingfour-parameterlogistic643
14.4Furtherconsiderations645
14.4.1Modelchecking645
14.4.2Confidenceintervals647 References 648
15 SurvivalAnalysis649
15.1Handlingsurvivaldata649
15.1.1Structureofasurvivaldataset649
15.1.2Survivaldatain R 652
15.2Thesurvivalandhazardfunctions652
15.2.1Non-parametricestimationofthesurvivalfunction653
15.2.2Parametricestimationofthesurvivalfunction654
15.3Modellingsurvivaldata655
15.3.1Thedata657
15.3.2TheCoxproportionalhazardmodel658
15.3.3Acceleratedfailuretimemodels660
15.3.4Coxproportionalhazardoraparametricmodel?665
16 DesignedExperiments667
16.3.1Contrastcoefficients678 16.3.2Anexampleofcontrastsusing R
16.3.3Modelsimplificationforcontrasts684 16.3.4Helmertcontrasts688 16.3.5Sumcontrasts689 16.3.6Polynomialcontrasts691
17.1Elementsofameta-analysis699
17.1.1Choosingstudiesforameta-analysis700
17.1.2Effectsandeffectsize700
17.1.3Weights701
17.1.4Fixedvs.randomeffectmodels701
17.2Meta-analysisin R 703
17.2.1Formattinginformationfromstudies703
17.2.2Computingtheinputsofameta-analysis703
17.2.3Conductingthemeta-analysis706
17.3Examples 707
17.3.1Meta-analysisOfscaleddifferences707
18 TimeSeries 715
18.1Movingaverage715 18.2Blowflies 717
18.3Seasonaldata723
18.3.1Pointofview724
18.3.2Builtin ts() functions724
18.3.3Cycles726
18.3.4Testingforatimeseriestrend728
18.4Multipletimeseries729
18.5Sometheoreticalbackground730
18.5.1Autocorrelation731
18.5.2Autoregressivemodels732
18.5.3Partialautocorrelation732
18.5.4Movingaveragemodels732
18.5.5Moregeneralmodels:ARMAandARIMA733
18.6ARIMAexample733
18.7Simulationoftimeseries735
19 MultivariateStatistics741
20 ClassificationandRegressionTrees761
20.1HowCARTswork763
20.2Regressiontrees764
20.2.1The tree package764
20.2.2The rpart package765
20.2.3Comparisonwithlinearregression767
20.2.4Modelsimplification769
20.3Classificationtrees771
20.3.1Classificationtreeswithcategoricalexplanatoryvariables771
20.3.2Classificationtreesforreplicateddata773
20.4Lookingforpatterns775 References 777
21 SpatialStatistics 779
21.1Spatialpointprocesses779
21.1.1Howcanwecheckforrandomness?781 21.1.2Models785 21.1.3Marks790
21.2Geospatialstatistics793
21.2.1Models794 References 798
22 BayesianStatistics799
22.1ComponentsofaBayesianAnalysis800
22.1.1Thelikelihood(themodelanddata)800
22.1.2Priors801
22.1.3ThePosterior802
22.1.4MarkovchainMonteCarlo(MCMC)803
22.1.5ConsiderationsforMCMC803
22.1.6Inference805
22.1.7TheProsandConsofgoingBayesian806
22.2Bayesiananalysisin R 806
22.2.1InstallingJAGS807
22.2.2RunningJAGSin R 807
22.2.3WritingBUGSmodels808
22.3Examples 810
22.3.1MCMCforasimplelinearregression810
22.3.2MCMCforlongitudinaldata814
22.4MCMCforamodelwithbinomialerrors818 References 821
23 SimulationModels823
23.1Temporaldynamics823
23.1.1Chaoticdynamicsinpopulationsize823
23.1.2Investigatingtheroutetochaos825
23.2Spatialsimulationmodels826
23.2.1Meta-populationdynamics826
23.2.2Coexistenceresultingfromspatiallyexplicit(local)densitydependence829
23.2.3Patterngenerationresultingfromdynamicinteractions834
ListofTables
Table1.1 Librariesusedinthisbookthatcomesuppliedaspartofthebasepackageof R 8
Table1.2 TaskViewsonCRAN10
Table3.1 Mathematicalfunctions61
Table3.2 Commonoperators62
Table3.3 Logicalandrelationaloperations67
Table3.4 Datatypes80
Table3.5 Vectorfunctions94
Table3.6 Formatcodesfordatesandtimes167
Table3.7 Escapesequencesforusewith cat()
199
Table4.1 Correctlysetoutdatasetforimportingintoadataframe216
Table4.2 Datasetthatwillnotformadataframecorrectly217
Table4.3 Datasetthatwillformadataframecorrectly217
Table4.4 Selectingpartsofadataframecalled df_dummy 223
Table5.1 Plottingsinglevariables255
Table6.1 Orientationandsizesoflabels310
Table6.2 Drawingmathematicalexpressionsintext323
Table6.3 Graphicalparametersandtheirdefaultvalues354
Table8.1 Somecommonlyusedprobabilitydistributionssupportedby R 376
Table9.1 TestsusedinChapter9436
Table10.1 Functionsforvariousregressionmodels497
Table10.2 Frequentlyusedfunctionstoextractinformationaboutregressionmodels498
Table11.1 Commonmembersoftheexponentialfamily501
Table14.1 Usefulnon-linearfunctions628
Table14.2 Usefulnon-linearself-startingfunctions639
Table15.1 Commonparametricformsofthesurvivalandhazardfunctions654
Table17.1 DatafromStudyA711
Preface
R isthemostpowerfultoolintheknownuniverseforcarryingoutstatisticalanalysis,andit’sfree! Thisbookisaimedatthosewhowishtocarryoutsuchwork–exploring,plotting,andmodelling data–butwhodonothavemuchexperiencein R and/orstatistics. R isdescribedfromscratch withinstructionsforloadingandgettinggoingwiththesoftwareinChapter1andadescriptionofits essentialelementsinChapter3.Laterchaptersdiscussstatisticalmethodsandarewrittensothat theycanbeusedeitherasabeginner’sguideorasareferencemanualonparticularproceduresin R.Thetheorybehindtheanalysesiscoveredinenoughdepth,wehope,tomakeitcomprehensible butwithoutoverburdeningthereaderwithtoomuchmathematics.Thedatasetsusedtoillustrate variousanalysesareavailableat https://www.wiley.com/go/jones/therbook3e.
Using R hasbecomefarsimplerwiththeintroductionofRStudio,whichisalsofree(othereditors areavailable).RStudioprovidesafriendlyfrontendandeasyaccesstotools,allofwhichseem alongwayfrom R’soriginalratherforbiddingcommandprompt.Thisbookassumestheuseof RStudioratherthanusing R directly,butthecodepresentedwillworkusingthelattersetuptoo. Whilethereisstilltheusualhurdleofgettingtoknowpowerfulsoftware,thebenefits,particularly ingraphicsandmodelling,faroutweightheeffort.Academicpapersinmanydisciplinesroutinely useandreportresultsusing R.Inaddition,theopen-sourcenatureofthesoftwaremeansthatusers haveaddedextrafunctionalitybywritingpackagestobroaden R’scapabilities.Therearecurrently over18,000packagesthat,togetherwithusefullinksandinformation,canbefoundattheofficial R distributionsite,CRAN: https://cran.r-project.org/
Thisbookiscontingentupontheexistenceof R.Thoseinvolvedaretoonumeroustomention, butwearehugelygratefultoallinvolvedinitscreationandcontinuingevolution.Whenyouuse R, R packages(e.g. spatstat),andRStudio,pleasecitethem.Up-to-datecitationdetailsforeach ofthesecanbefoundbytypingthefollowingin R,respectively:
citation() citation("spatstat") RStudio.Version()
ElinorJones SimonHarden MichaelJ.Crawley August2022
Acknowledgments
Thisbookwouldnotexistwithoutitspreviouseditionssothanks,firstly,totheoriginatingauthor, MichaelJ.Crawley.
IthasbeenapleasuretoreviseTheRBooktocreatethisthirdedition.Weareverygratefulto ProfessorCrawleyforallowingustousematerialsfrompreviousversions,includinghisfantastic arrayofdatasetsthatmakeawelcomereturninthisedition.
Finally,wewouldliketothanktheDepartmentofStatisticalScienceatUniversityCollegeLondon forgivingustimeandspacetocompletethebookduringadifficultperiodforeverybody.
ElinorJones SimonHarden
August2022
Thisbookisaccompaniedbyacompanionwebsite.
www.wiley.com/go/jones/therbook3e
Thiswebsiteinclude:Datasets