1 Beer Example

This document contains the code for reproducing the PCA found for the beers example in the PCA lecture for the Social Epidemiology course at LSHTM in 2019.

This code can run in RStudio which is freely available to download, or is on the school computers. Open a new R Notebook in R and copy each line of code into a new code chunk (Ctrl-Alt-I is the default shortcut; this may not work on all computers and can be changed under Tools -> Modify Keyboard Shortcuts), making your own annotations in the whitespace.

An alternative is to save a copy of the R Notebook itself to your own H:/ drive or the place of your choosing. Notebooks are essentially text documents (in white) with inline code (in grey), so you can modify the text and code as you see fit.

To run a command in R, press ctrl-Enter with your cursor anywhere in the line you wish to run or press the green button in the top right hand corner of a code chunk to run the entire chunk.

As R is open source software, not every tool we might want for analysis is contained in the base programme. Some useful user written functions are in various packages. Once a package is installed, we load the functions from it using the library() command. Try loading the libraries in the code chunk below. Should any fail, uncomment the install.packages() commands by deleting the # symbol at the start of the line and run those lines of code. Ensure the tidyverse is loaded last.