Review

Today’s lab is relatively short, before we begin we will do some interactive review of the lab material we have covered to date. Open the following link on one of the lab computers to follow along https://poll.gis.unbc.ca/p/31359493

Moran’s I

In today’s lab we will be looking at calculating Moran’s I, and Local Moran’s I using R; while in R we will also take a look at how we can produce reproducible reports using R-Markdown documents. For those unfamiliar Markdown is a tool used by various programs that allow for documents to be written in plain text, and rendered with various style options that were defined in said text.

In RStudio you can create your first Markdown document by going into File, New and choosing R Notebook.

Once the template is created save with the Rmd file extension. If you were curious the difference between a R Notebook and R Markdown is that in the header of a notebook we have a line that shows “output: html_notebook” this will allow us to preview work as we go; this makes the document behave similar to a Jupyter Notebook.

The main parts of the new new document are

  • The heading starts and ends with —
  • Markdown (Think of this as plain text
  • R Code surrounded by “`{r} “`

It is generally considered proper form to include your name and date in the file header.

R

With that in place you can preview your document with the preview button

However there is a distinct absence of a plot in this document only the code shows up. There are two options for this, one is to select the line showing plot(cars) press [Ctrl] + [Enter] to preview the graph in the editor which will also make it appear in the HTML preview. The other option is to knit the document; this is found in the drop-down beside the preview window, for example try knitting to PDF.

Finally the Table of Contents is another very helpful tool allowing the document to get some structure, outside of r code this is done with the # symbol, for example,

R

Will produce the following headings, these will both appear in the final document, and can also be used for navigating within the document inside of R Studio, by clicking on the desired heading.

Preparing for Moran’s-I

For today’s lab we will be calculating Moran’s-I on some trials conducted by the Ministry of Forests, and will be using a package not normally available in R, this will be installed in the R command line with the following command.

R

After this is installed you can import the libraries that we will be using today. Note there is an argument include=FALSE, this will prevent all these import statements from appearing in our final report.

R

Download the trial data from GitHub https://github.com/FLNRO-Smithers-Research/OffSite_Trial_Reporting/blob/master/trial_data.Rdata

After you have downloaded into your working directory you can open with the following. It is relatively large code, as it expands the data from a single data-frame to multiple data-frames as well as calculate the extent of data (ie how many years does the data cover).

R

To prepare for calculating the statistics the data must be opened, this sample data set contains 3 attributes that we can use to check for spatial auto correlation with Species, Seedlot (which tree farm are the seedlings from), and condition. You may note that there are also height and diameter columns, while potentially interested this data has been scrubbed from the open source side of the project.

R

Next the data needs to be sorted by trial site, (Identified by FID)

R

To compute Moran’s I we need to convert the Trees to SF features and project into UTM

R

Once the data is sf the next stage is to calculate the inverse distance of all points

R

When working with categorical data it will be converted to factor in R, for proper calculation we would like this numbers to have a logical pattern, as such we will re-level the condition attribute

R

Finally we want to plot the attribute and calculate Moran’s I

R

This is put all together as the following function

R

Assignment

Create a Report on the Spatial Auto-correlation between Annual Precipitation and location of weather station using the stations data found in L:\GEOG413\lab08

You should be able to

  • Import the data, and confirm the CRS / Translate if Needed
  • Plot the data on a basemap (note that as you are making a PDF report interactive leaflet will not be an option)
  • Report on if there is a correlation, and if it is significant.
Categories: GEOG 413Labs