--- title: "Spatially-explicit logistic regression" --- [ The R Script associated with this page is available here](scripts/13_SDM_Exercise.R). Download this file and open it (or copy-paste into a new script) with RStudio so you can follow along. ```r library(knitr) library(raster) library(rasterVis) library(dplyr) library(ggplot2) # devtools::install_github("dkahle/ggmap") library(ggmap) library(rgdal) library(rgeos) library(tidyr) library(sf) library(leaflet) library(DT) library(widgetframe) ## New Packages library(mgcv) # package for Generalized Additive Models library(ncf) # has an easy function for correlograms library(grid) library(gridExtra) library(xtable) library(maptools) ``` ## Goal of this class * To demonstrate a simple presence/absence modelling in spatial context. * To model spatial dependence (autocorrelation) in the response. Overview of [R's spatial toolset is here](http://cran.r-project.org/web/views/Spatial.html). Note: this is _not_ meant to be an exhaustive introduction to species distribution modelling. ## Modeling spatial autocorrelation Today we will model space by **smooth splines** in `mgcv` package. Examples of Alternative approaches: - Simple polynomials - Eigenvector Mapping: ```vegan```, ```spdep``` - Predictive process: ```spbayes``` Methods that tweak variance-covariance matrix of **Multivariate Normal Distribution**: - Generalized Least Squares: ```MASS```, ```nlme``` - Autoregressive Models: ```spdep``` - GeoBUGS module in OpenBUGS See [Dormann et al. 2007 Ecography, 30: 609-628](http://onlinelibrary.wiley.com/doi/10.1111/j.2007.0906-7590.05171.x/full) for a review. ## Species Distribution Modeling We'll attempt to explain the spatial distribution of the Purple finch (_Carpodacus purpureus_) in San Diego county, California:  (photo/Wikimedia) ## Preparing the data Load a vector dataset (shapefile) representing the [San Diego bird atlas data](http://sdplantatlas.org/BirdAtlas/BirdPages.htm) for the Purple finch: ```r finch <- read_sf(system.file("extdata", "finch", package = "DataScienceData"), layer="finch") st_crs(finch)="+proj=utm +zone=11 +ellps=GRS80 +datum=NAD83 +units=m +no_defs " ``` ### Plot the shapefile Plot the finch dataset in leaflet. ```r st_transform(finch,"+proj=longlat +datum=WGS84")%>% leaflet() %>% addTiles() %>% addPolygons()%>% frameWidget(height=400) ```
But we can do better than that. Let's add a couple layers and an overview map. ```r st_transform(finch,"+proj=longlat +datum=WGS84")%>% leaflet() %>% addTiles() %>% addPolygons(label=paste(finch$BLOCKNAME," (NDVI=",finch$ndvi,")"), group = "NDVI", color = "#444444", weight = 0.1, smoothFactor = 0.5, opacity = 1.0, fillOpacity = 0.5, fillColor = ~colorQuantile("YlOrRd", ndvi)(ndvi), highlightOptions = highlightOptions(color = "white", weight = 2, bringToFront = TRUE)) %>% addPolygons(label=paste(finch$BLOCKNAME," (NDVI=",finch$ndvi,")"), group = "Presence/Absence", color = "#444444", weight = 0.1, smoothFactor = 0.5, opacity = 1.0, fillOpacity = 0.5, fillColor = ifelse(finch$present,"red","transparent"), highlightOptions = highlightOptions(color = "white", weight = 2, bringToFront = TRUE)) %>% addLayersControl( baseGroups = c("NDVI", "Presence/Absence"), options = layersControlOptions(collapsed = FALSE) )%>% addMiniMap()%>% frameWidget(height = 600) ```| Estimate | Std. Error | z value | Pr(>|z|) | |
|---|---|---|---|---|
| (Intercept) | -2.9388 | 0.2960 | -9.93 | 0.0000 |
| ndvi_scaled | 2.6521 | 0.3223 | 8.23 | 0.0000 |
| edf | Ref.df | Chi.sq | p-value | |
|---|---|---|---|---|
| s(X_CEN,Y_CEN) | 28.83 | 28.98 | 51.06 | 0.01 |
| edf | Ref.df | Chi.sq | p-value | |
|---|---|---|---|---|
| s(X_CEN,Y_CEN) | 23.35 | 25.84 | 47.74 | 0.01 |
| Estimate | Std. Error | z value | Pr(>|z|) | |
|---|---|---|---|---|
| (Intercept) | -8.93 | 19.66 | -0.45 | 0.65 |
| ndvi | 0.08 | 0.03 | 2.67 | 0.01 |
| meanelev | -0.01 | 0.01 | -1.02 | 0.31 |
| wintert | -0.88 | 1.57 | -0.56 | 0.58 |
| meanppt | 0.03 | 0.02 | 1.33 | 0.18 |
| urban | 0.00 | 0.03 | 0.17 | 0.87 |