# 1. Install the installr package from CRAN
install.packages("installr")
# 2. Load the package into your session
library(installr)
# 3. Run the automated Rtools installation
install.Rtools(check = TRUE, check_r_update = TRUE, GUI = TRUE)1 Getting Started
1.1 Your R System
In this course, we work with the combination of R + RStudio.
- [R](https://www.r-project.org) is the calculation engine that performs the computations.
- [RStudio](https://posit.co/download/rstudio-desktop/) is the editor that helps you sending inputs to R and collect outputs.
Make sure you have a recent version of R + RStudio installed on your computer. If you have never used RStudio, here is a good video introducing the basic system and how R and RStudio interact.
1.2 Windows users - install Rtools
As we may need to use R libraries that require a C++ compiler, Windows users should install Rtools (the C++ compiler for R). Either download and install Rtools directly from here or run the following commands in RStudio
1.3 Libraries that you will need
The R engine comes with a number of base functions, but one of the great things about R is that you can extend these base functions by libraries that can be programmed by anyone. In principle, you can install libraries from any website or file. In practice, however, most commonly used libraries are distributed via two major repositories. For statistical methods, this is CRAN, and for bioinformatics, this is Bioconductor.
To install a package from a library, use the command
install.packages(LIBRARY)Exchange “LIBRARY” with the name of the library you want to install. The default is to search the package in CRAN, but you can specify other repositories or file locations in the function. For Windows / Mac, R should work out of the box. For other UNIX based systems, may also need to install
build-essential
gfortran
libmagick++-dev
r-base-dev
cmake
If you are new to installing packages on Debian / Ubuntu, etc., type the following:
sudo apt update && sudo apt install -y --install-recommends build-essential gfortran libmagick++-dev r-base-dev cmake
In this book, we will often use data sets from the EcoData package, which is not on CRAN, but on a GitHub page. To install the package from github, first install devtools package (unless you have the devtools package installed already) by running
install.packages("devtools")Then you can use the devtools::install_github function to install the EcoData package via
devtools::install_github(repo = "TheoreticalEcology/EcoData",
dependencies = T, build_vignettes = T)Besides providing data, the EcoData installation also forces the installation of most of the packages that we need in this book, so this may take a while. If you want to load only the EcoData package (without installing all the other packages), or if you encounter problems during the install, set dependencies = F, build_vignettes = F.
In addition to the packages provided in EcoData, to be able to run all examples in the book, please install the following additional packages:
install.packages("BayesianTools")
install.packages("rjags")
install.packages("R2jags")
install.packages("blavaan")
install.packages("runjags")Additionally, you have to install the JAGS MCMC sampler, which is a program independent from R. You can find downloads of Jags versions for different operating systems here.
1.4 Exercises and R code from the book
If you want to run code from the book, you can copy and paste it to your RStudio, but for your convenience, I also provide a plain R version of the code of each chapter here.
The same folder holds some R scripts that are meant as classroom exercises, accompanying each chapter.
1.5 Assumed R and statistics knowledge
As mentioned in the preface, this book assumes that you have basic knowledge about statistical concepts and regression models, as well as data manipulation (reading in data, removing or selecting columns or rows, calculating means per group etc.) and plotting in R.
If you want to brush up your statistical concepts, I would recommend to work through
My lecture notes for the course [Advanced Regression Models](https://theoreticalecology.github.io/AdvancedRegressionModels/).
McElreath, R. (2018). Statistical rethinking: A Bayesian course with examples in R and Stan. Chapman and Hall/CRC.
Regarding data manipulation and coding in R, I want to note that there are currently two main schools in the R environment which do the same things, but with very different syntax:
- base R, which uses functions such as
plot(),apply(),aggregate() - tidyverse, with packages such as dplyr and ggplot2, which provide functions such as
mutate(),filter()and heavily rely on the%>%pipe operator.
There are many opinions about advantages and disadvantages of the two schools. I’m agnostic about this, or more precisely, I think you should get to know both schools and then decide based on the purpose. I see advantages of tidyverse in particular for data manipulation, while I often prefer baseR plots over ggplot2. To keep it simple, however, all code in this course uses base R.
The tidyverse framework is currently trying to expand to the tasks of statistical / machine learning models as well, trying to streamline statistical workflows. While this certainly has a lot of potential, I don’t see it as general / mature enough to recommend it as a default for the statistical workflow.
In the following box, you will find an exercise that asks you to perform basic plots and data manipulations. To test yourself, please check that you can perform these operations. If you have problems, you should study an introductory R course (for example here) before continuing with this text.



