README.md

monkeytools

R-CMD-check

The goal of monkeytools is to provide some utilities functions that I use routinely. Every contribution is welcomed.

Installation

You can install the development version of monkeytools from GitHub with:

devtools::install_github("emilio-berti/monkeytools")

monkeytools is not released on CRAN.

Example

Simple assessments of species distribution models.

Let’s first generate some data for a virtual species that never goes above 130 meters.

library(monkeytools)
library(raster)
#> Loading required package: sp

data(volcano)
r <- raster(volcano)
xy <- xyFromCell(r, sample(1:ncell(r), 100))
x <- extract(r ,xy) #elevation
p <- ifelse(x < 130, 1, 0) #real presence/absence - doesn't like heights
occ <- data.frame(x = xy[, 1], y = xy[, 2], p = p) #combined in a dataframe
head(occ)
#>            x          y p
#> 1 0.64754098 0.18965517 1
#> 2 0.43442623 0.97126437 1
#> 3 0.33606557 0.90229885 1
#> 4 0.05737705 0.05172414 1
#> 5 0.28688525 0.23563218 0
#> 6 0.58196721 0.39655172 0

# sdm potential output
sdm <- 1 / r + rnorm(ncell(r), 0, .001) #sdm predictions
sdm <- sdm - sdm@data@min
sdm <- sdm / sdm@data@max

A very useful summary table is the confusion matrix. The confusion table calculates the fraction of correctly predicted occurrences based on observed data. For this, SDM output needs to be binarized.

sdm_bin <- sdm
sdm_bin[sdm < .5] <- 0
sdm_bin[sdm >= .5] <- 1
confusion(sdm_bin, occ)
#> $confusion.table
#>               obs.presence obs.absence
#> pred.presence           43           9
#> pred.absence             7          41
#> 
#> $specificity
#> [1] 0.8269231
#> 
#> $sensitivity
#> [1] 0.8541667

The confusion matrix is used in the famous AUROC curve, which shows how much some statistics extracted from the confusion matrix change for different binarization thresholds. The area under the AUROC curve (AUC) measures how well the SDM fits data better than random (dashed line). There are other (and better) summary statistics for goodness-of-fit of SDMs, but AUC remains one the most adopted.

null <- AUROC(sdm, occ, th = seq(0, 1, length.out = 50))

And for a very bad SDM the two lines largely overlap.

values(sdm) <- rnorm(ncell(sdm))
null <- AUROC(sdm, occ, th = seq(0, 1, length.out = 50))

Draw causal graphs

This is wrapper to DiagrammeR pacakge. It works only in HTML files.

dag_data <- data.frame(
  from = c("Rain", "Moisture", "Hair drier", "Rain"),
  to = c("Wet hair", "Wet hair", "Wet hair", "Moisture")
)
draw_dag(dag_data)
#> PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
{"x":{"diagram":"digraph {\n graph []\n node [shape = plaintext]\n edge []\nRain -> Wet_hair \nMoisture -> Wet_hair \nHair_drier -> Wet_hair \nRain -> Moisture \n}","config":{"engine":"dot","options":null}},"evals":[],"jsHooks":[]}

emilio-berti/monkeytools documentation built on April 15, 2022, 8:14 a.m.