Introduction to Spatial Analysis for Infectious Diseases
Welcome to the Introduction of Spatial Analysis for Infectious diseases tutorial pages. In this part of the workshop we aim to teach basic concepts, skills and tools for working with data so that you can get more done in less time, and with less pain. The lessons here focus mainly on the use of R and QGIS.
The main sections of this workshop are designed to be introductory
for participants with little to no programming experience. The advanced
section taught in R for Geostatistical Modelling using
R-INLA
requires participants to have some statistical
knowledge, some understanding of Bayesian statistics and some prior
knowledge of spatial data.
Goal of this tutorial:
We want to provide our audience with a few useful tools for their day-to-day data activities. Given the restricted time of ASTMH, we may not be able to cover all important aspects of data handling. But the team are excited to release future workshops more tailored to regional/country requests. Please get in touch with us at on our GoogleGroup
All data for this workshop will be available on a USB stick provided in attendance. The data can also be found in advance of the training in a GoogleDrive folder.
We have set up a Googledoc where we will put all the R scripts and notes we make during the workshop
This workshops is expected to be hands-on, and to follow this lessons participants must have R, RStudio and QGIS installed on their computers. You also need to be able to install a number of R packages, create directories and download files.
To avoid troubleshooting during the lesson, participants should follow the instructions below to download and install everything beforehand. If you are using your own computers this should be no problem, but if the computer is managed by your organization’s IT department you might need help from an IT administrator.
Follow the QGIS download for the appropriate operating system to download on your computer.
R and RStudio are two separate pieces of software:
If you don’t already have R and RStudio installed, follow the instructions for your operating system below. You have to install R before you install RStudio.
.exe
file that was just downloaded.pkg
file for the latest R versionsudo apt-get install r-base
, and for Fedora
sudo yum install R
), but we don’t recommend this approach
as the versions provided by this are usually out of date. In any case,
make sure you have at least R 3.3.1.sudo dpkg -i rstudio-YYYY.MM.X-ZZZ-amd64.deb
at the
terminal).If you already have R and RStudio installed, first check if your R version is up to date:
sessionInfo()
into the console. If your R version is 4.0.0
or later, you don’t need to update R for this lesson. If your version of
R is older than that, download and install the latest version of R from
the R project website for Windows, for MacOS, or for LinuxTools > Global Options > General > Basic
.installr
that can help you with upgrading your R
version and migrate your package library.To update RStudio to the latest version, open RStudio and click on
Help > Check for Updates
. If a new version is available
follow the instruction on screen. By default, RStudio will also
automatically notify you of new versions every once in a while.
During the course we will need a number of R packages. Packages
contain useful R code written by other people. We will use the packages
tidyverse
, sf
and terra
mainly
for this workshop.
To try to install these packages, open RStudio. If this is your first
time opening it you may be prompted to set a CRAN mirror - select any
country or even the R global cran. This step is important because CRAN
aka Comprehensive R Archive Network is where R stores many
useful packages that we may use. If you forgot to set this up or wish to
change yours you can go to Tools
->
Global Options
-> Packages
, you will have a
screen that looks like this:
You can change CRAN by clicking the change...
button.
Additionally, make sure that the section circled in red in the image
above is not selected
Next, copy and paste the following command into the console window (look for a blinking cursor on the bottom left), then press the Enter (Windows and Linux) or Return (MacOS) to execute the command.
list.of.packages <- c("tidyverse", "sf", "terra", "rgdal", "sp", "raster", "scico", "RColorBrewer", "viridis", "viridisLite", "ggplot2", "tidyr", "dplyr", "readxl")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
Alternatively, you can install the packages using RStudio’s graphical
user interface by going to Tools > Install Packages
and
typing the names of the packages separated by a comma.
R tries to download and install the packages on your machine. Tidyverse is a group of packages so it can take some time to load. When the installation has finished, you can try to load the packages by pasting the following code into the console:
library(tidyverse)
library(sf)
library(terra)
library(rgdal)
If you do not see an error like
there is no package called ‘...’
you are good to go!
Also note that when you load a package like tidyverse in for the first time you may see such test in your console:
This text is simply telling you what packages have loaded under tidyverse and also telling you that there were function in base R that had the same names as those in tidyverse and they have been ‘over-written’ by the tidyverse ones. Additionally some of you may see some warning signs:
Warnings
are normally ok to ignore. Usually when you see
a warning in R, it will still process the command you’ve entered, and
just wants to draw your attention that later on you may have issues
because of the text in the warning. Please ignore any warnings you may
see.
Generally, it is recommended to keep your R version and all packages
up to date, because new versions bring improvements and important
bugfixes. To update the packages that you have installed, click
Update
in the Packages
tab in the bottom right
panel of RStudio, or go to
Tools > Check for Package Updates...
.
Sometimes, package updates introduce changes that break your old
code, which can be very frustrating. To avoid this problem, you can use
a package called renv
. It locks the package versions you
have used for a given project and makes it straightforward to reinstall
those exact package version in a new environment, for example after
updating your R version or on another computer. However, the details are
outside of the scope of this lesson.