---
title: "assignR Examples"
author: "Gabe Bowen, Chao Ma"
date: "November 10, 2020"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{assignR Examples}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE)
```

This vignette introduces the basic functionality of the *assignR* package using data bundled with the package. We'll review how to access compiled data for known-origin biological samples and environmental models, use these to fit and apply functions estimating the probability of sample origin across a study region, and summarize these results to answer research and conservation questions. We'll also demonstrate an assignment quality analysis tool useful in study design, method comparison, and uncertainty analysis. 

*****
Let's load the package 

```{r install, message=FALSE, warning=FALSE, results="hide"}
library(assignR)
library(raster)
library(sp)
```

*****
Now add some data from the package to your local environment. Load and plot the North America boundary mask.

```{r boundary}
data("naMap")
plot(naMap)
```

*****
Load and plot a growing season precipitation H isoscape for North America. Notice this is a RasterBrick with two layers, the mean prediction and a standard deviation of the prediction. The layers are from [waterisotopes.org](https://wateriso.utah.edu/waterisotopes/), and resolution has been reduced to speed up processing in these examples. Full-resolution global growing season isoscapes are included in the package as *d2h_world.rda* and *d18o_world.rda*. 

```{r isoscape, fig.width=7, fig.asp=0.45}
data("d2h_lrNA")
plot(d2h_lrNA)
```

*****
The package includes a database of H and O isotope data for known origin samples (*knownOrig.rda*), which consists of three features (*sites*, *samples*, and *sources*). Let's load it and have a look. First we'll get the names of the data fields available in the tables.

```{r knownOrig_names}
data("knownOrig")
names(knownOrig$sites)
names(knownOrig$samples)
names(knownOrig$sources)
```

The *sites* feature is a spatial object that records the geographic location of all sites from which samples are available.

```{r knownOrig_sites, fig.width=5, fig.asp=0.8}
plot(assignR:::wrld_simpl)
points(knownOrig$sites, col = "red")
```

Now lets look at a list of species names available.

```{r knownOrig_taxa}
unique(knownOrig$samples$Taxon)
```

*****
Load H isotope data for North American Loggerhead Shrike from the package database.

```{r birdData, fig.width=5, fig.asp=0.8}
d = subOrigData(taxon = "Lanius ludovicianus", mask = naMap)
```

By default, the *subOrigData* function transforms all data to a common reference scale (defined by the standard materials and assigned, calibrated values for those; by default VSMOW-SLAP) using data from co-analysis of different laboratory standards (Magozzi et al., in press). The calibrations used are documented in the function's return object.  

```{r birdChains}
d$chains
```

Information on these calibrations is contained in the *stds.rda* data file. 

Transformation is important when blending data from different labs or papers because different reference scales have been used to calibrate published data and these calibrations are not always comparable. In this case all the data come from one paper:

```{r birdSources}
d$sources[,1:3]
```

If we didn't want to transform the data, and instead wished to use the reference scale from the original publication, we can specify that in our call to *subOrigData*. Keep in mind that any subsequent analyses using these data will be based on this calibration scale: for example, if you wish to assign samples of unknown origin, the values for those samples should be reported on the same scale.

```{r birdNoTrans, fig.width=5, fig.asp=0.8}
d = subOrigData(taxon = "Lanius ludovicianus", mask = naMap, ref_scale = NULL)
d$sources$H_std_scale
```

For a real application you would want to explore the database to find measurements that are appropriate to your study system (same or similar taxon, geographic region, measurement approach, etc.) or collect and import known-origin data that are specific to your system.

*****
# Isoscape Calibration and Probability of Origin for Unknown Samples 

We need to start by assessing how the environmental (precipitation) isoscape values correlate with the sample values. *calRaster* fits a linear model relating the precipitation isoscape values to sample values, and applies it to produce a sample-type specific isoscape.

```{r calRaster, fig.width=5, fig.asp=0.8, out.width='45%'}
r = calRaster(known = d, isoscape = d2h_lrNA, mask = naMap)
```

*****
Let's create some hypothetical sample IDs and values to demonstrate how samples of unknown origin can be assigned to the calibrated isoscape. The isotope values are drawn from a random distribution with a standard deviation of 8 per mil, which is a pretty reasonable variance for conspecific residents at a single location. We'll also add made-up values for the analytical uncertainty for each sample and a column recording the calibration scale used for our measurements (see below). If you had real measured data for your study samples you would load them here, instead.

```{r samples}
id = letters[1:5]
set.seed(123)
d2H = rnorm(5, -110, 8)
d2H.sd = runif(5, 1.5, 2.5)
d2H_cal = rep("UT_H_1", 5)
un = data.frame(id, d2H, d2H.sd, d2H_cal)
print(un)
```

As discussed above, one issue that must be considered with any organic H or O isotope data is the reference scale used by the laboratory producing the data. The reference scale for your unknown samples should be the same as that for the known origin data used in calRaster. Remember that the scale for our known origin data 'd' is *OldEC.1_H_1*. Let's assume that our fake data were normalized to the *UT_H_1* scale. The *refTrans* function allows us to convert between the two.

```{r refTrans}
un = refTrans(un, ref_scale = "OldEC.1_H_1")
print(un)
```

Notice that both the d2H values and the uncertainties have been updated to reflect the scale transformation.

*****
Produce posterior probability density maps used to the assign the unknown origin samples. For reference on the Bayesian inversion method see [Wunder, 2010](https://www.researchgate.net/profile/Michael_Wunder/publication/226054272_Using_Isoscapes_to_Model_Probability_Surfaces_for_Determining_Geographic_Origins/links/00b49526ab1e02ed11000000.pdf)

```{r pdRaster, fig.width=5, fig.asp=0.8, out.width='45%'}
asn = pdRaster(r, unknown = un$data)
```

Cell values in these maps are small because each cell's value represents the probability that this one cell, out of all of them on the map, is the actual origin of the sample. Together, all cell values on the map should sum to '1', reflecting the assumption that the sample originated *somewhere* in the study area. Let's check this for sample 'a'.

```{r sums}
cellStats(asn[[1]], 'sum')
```

Check out the help page for *pdRaster* for additional options, including the use of informative prior probabilities.

*****
# Post-hoc Analysis
## Odds Ratio

The *oddsRatio* tool compares the posterior probabilities for two different locations or regions. This might be useful in answering real-world questions...for example "is this sample more likely from France or Spain?", or "how likely is this hypothesized location relative to other possibilities?". 

Let's compare probabilities for two spatial areas - the states of Utah and New Mexico. First we'll load the SpatialPolygons and plot them.

```{r polygons}
data("states")
s1 = states[states$STATE_ABBR == "UT",]
s2 = states[states$STATE_ABBR == "NM",]
plot(naMap)
lines(s1, col = c("red"))
lines(s2, col = c("blue"))
```

Get the odds ratio for the two regions. The result reports the odds ratio for the regions (first relative to second) for each of the 5 unknown samples plus the ratio of the areas of the regions. If the isotope values (& prior) were completely uninformative the odds ratios would equal the ratio of areas.

```{r oddsRatio1}
s12 = rbind(s1, s2)
oddsRatio(asn, s12)
```

Here you can see that even though Utah is quite a bit smaller the isotopic evidence suggests it's much more likely to be the origin of each sample. This result is consistent with what you might infer from a first-order comparison of the state map with the posterior probability maps, above.

*****
Comparisons can also be made using points. Let's create two points (one in each of the Plover regions) and compare their odds. This result also shows the odds ratio for each point relative to the most- and least-likely grid cells on the posterior probability map.

```{r oddsRatio2}
pp1 = c(-112,40)
pp2 = c(-105,33)
pp12 = SpatialPoints(coords = rbind(pp1,pp2))
proj4string(pp12) = proj4string(naMap)
oddsRatio(asn, pp12)
```

The odds of the first point being the location of origin are pretty high for each sample, and much higher than for the second point.

## Assignment

Researchers often want to classify their study area in to regions that are and are not likely to be the origin of the sample (effectively 'assigning' the sample to a part of the area). This requires choosing a subjective threshold to define how much of the study domain is represented in the assignment region. *qtlRaster* offers two choices.

Extract 10% of the study area, giving maps that show the 10% of grid cells with the highest posterior probability for each sample.

```{r qtlRaster1, fig.width=5, fig.asp=0.8, out.width='45%'}
qtlRaster(asn, threshold = 0.1)
```

*****
Extract 80% of the posterior probability density, giving maps that show the smallest region within which there is an 80% chance each sample originated.

```{r qtlRaster2, fig.width=5, fig.asp=0.8, out.width='45%'}
qtlRaster(asn, threshold = 0.8, thresholdType = "prob")
```

Comparing the two results, the probability-based assignment regions are broader. This suggests that we'll need to assign to more than 10% of the study area if we want to correctly assign 80% or more of our samples. We'll revisit this below and see how we can chose thresholds that are as specific as possible while achieving a desired level of assignment 'quality'.

## Summarization

Most studies involve assigning multiple individuals, and often it is desirable to summarize the results from these individuals. *jointP* and *unionP* offer two options for summarizing posterior probabilities from multiple samples.

Calculate the probability that **all** samples came from any given grid cell in the analysis area. Note that this summarization will only be useful if all samples are truly derived from a single population of common geographic origin.

```{r jointP, fig.width=5, fig.asp=0.8}
jointP(asn)
```

*****
Calculate the probability that **any** sample came from any given grid cell in the analysis area. In this case we'll save the output to a variable for later use.

```{r unionP, fig.width=5, fig.asp=0.8}
up = unionP(asn)
```

The results from *unionP* highlight a broader region, as you might expect.

*****
Any of the other post-hoc analysis tools can be applied to the summarized results. Here we'll use *qtlRaster* to identify the 10% of the study area that is most likely to be the origin of one or more samples.

```{r qtlRaster3, fig.width=5, fig.asp=0.8}
qtlRaster(up, threshold = 0.1)
```

*****
## Quality analysis and method comparison

How good are the geographic assignments? What area or probability threshold should be used? Is it better to use isoscape *A* or *B* for my analysis? These questions can be answered through split-sample validation using *QA*.

We will run quality assessment on the known-origin dataset and precipitation isoscape. These analyses take some time to run, depending on the number of stations and iterations used (this one took about two minutes on my desktop PC).  

```{r QA1, warning=FALSE}
qa1 = QA(d, d2h_lrNA, valiStation = 8, valiTime = 4, by = 5, mask = naMap, name = "normal")
```

*****
Plot the result. (Please note that because of changes in R's random number generator your results may not exactly match those shown here if you are using R version 3.5.X.)

```{r plot.QA1, fig.width=4, fig.asp=1, out.width='45%'}
plot(qa1)
```

The first three panels show three metrics, granularity (higher is better), bias (closer to 1:1 is better), and sensitivity (higher is better). The second plot shows the posterior probabilities at the known locations of origin relative to random (=1, higher is better). More information is provided in [Ma et al., 2020](https://doi.org/10.1111/2041-210X.13426).

A researcher might refer to the sensitivity plot, for example, to assess what *qtlRaster* area threshold would be required to obtain 90% correct assignments in their study system. Here it's somewhere between 0.25 and 0.3.

*****
How would using a different isoscape or different known origin dataset affect the analysis? Multiple QA objects can be compared to make these types of assessments.

Let's modify our isoscape to add some random noise.

```{r modraster, fig.width=5, fig.asp=0.8}
dv = getValues(d2h_lrNA[[1]])
dv = dv + rnorm(length(dv), 0, 15)
d2h_fuzzy = setValues(d2h_lrNA[[1]], dv)
plot(d2h_fuzzy)
```

*****
We'll combine the fuzzy isoscape with the uncertainty layer from the original isoscape, then rerun *QA* using the new version. Obviously this is not something you'd do in real work, but as an example it allows us to ask the question "how would the quality of my assignments change if my isoscape predictions were of reduced quality?". 

```{r QA2, warning=FALSE}
d2h_fuzzy = brick(d2h_fuzzy, d2h_lrNA[[2]])
qa2 = QA(d, d2h_fuzzy, valiStation = 8, valiTime = 4, by = 5, mask = naMap, name = "fuzzy")
```

*****
Now *plot* to compare.

```{r plot.QA2, fig.width=4, fig.asp=1, out.width='45%'}
plot(qa1, qa2)
```

Assignments made using the fuzzy isoscape are generally poorer than those made without fuzzing. Hopefully that's not a surprise, but you might encounter cases where decisions about how to design your project or conduct your data analysis do have previously unknown or unexpected consequences. These types of comparisons can help reveal them!

*****
*****
Questions or comments? <gabe.bowen@utah.edu>
