Skip to content

Commit

Permalink
Added offset to mapSpecies and the modified the vignette accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
guiblanchet committed Apr 17, 2019
1 parent a244106 commit 4af8768
Show file tree
Hide file tree
Showing 44 changed files with 1,104 additions and 524 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ Depends:
sp,
rgeos,
raster,
deldir
deldir,
Matrix,
parallel
RoxygenNote: 6.1.1
Encoding: UTF-8
Suggests:
Expand Down
27 changes: 24 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,45 +1,66 @@
# Generated by roxygen2: do not edit by hand

S3method(print,ppWeight)
S3method(summary,ppSpace)
S3method(summary,uniSpace)
export(explanaMesh)
export(mapSpace)
export(mapSpaceTime)
export(offsetExpert)
export(ppSpace)
export(ppSpaceLarge)
export(ppSpaceTime)
export(ppSpaceTimeLarge)
export(ppWeight)
export(uniSpace)
export(weightPP)
importFrom(INLA,inla)
importFrom(INLA,inla.mesh.fem)
importFrom(INLA,inla.mesh.project)
importFrom(INLA,inla.mesh.projector)
importFrom(INLA,inla.spde.make.A)
importFrom(INLA,inla.spde.make.index)
importFrom(INLA,inla.spde2.pcmatern)
importFrom(INLA,inla.stack)
importFrom(INLA,inla.stack.A)
importFrom(INLA,inla.stack.data)
importFrom(INLA,inla.stack.index)
importFrom(Matrix,Diagonal)
importFrom(Matrix,rBind)
importFrom(bossMaps,logistic)
importFrom(deldir,deldir)
importFrom(deldir,tile.list)
importFrom(gnlm,bnlr)
importFrom(gnlm,gnlr)
importFrom(parallel,mclapply)
importFrom(raster,brick)
importFrom(raster,cellFromXY)
importFrom(raster,crop)
importFrom(raster,crs)
importFrom(raster,distance)
importFrom(raster,extend)
importFrom(raster,extent)
importFrom(raster,extract)
importFrom(raster,mask)
importFrom(raster,ncell)
importFrom(raster,raster)
importFrom(raster,rasterFromXYZ)
importFrom(raster,rasterize)
importFrom(raster,res)
importFrom(raster,stack)
importFrom(raster,values)
importFrom(raster,xmax)
importFrom(raster,xmin)
importFrom(raster,xyFromCell)
importFrom(raster,ymax)
importFrom(raster,ymin)
importFrom(rgeos,gArea)
importFrom(rgeos,gIntersection)
importFrom(rgeos,gIntersects)
importFrom(sp,Polygon)
importFrom(sp,Polygons)
importFrom(sp,SpatialPoints)
importFrom(sp,SpatialPolygons)
importFrom(sp,bbox)
importFrom(sp,coordinates)
importFrom(sp,over)
importFrom(stats,dist)
importFrom(stats,model.frame)
importFrom(stats,model.matrix)
3 changes: 2 additions & 1 deletion R/aggData.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ aggData <- function(xyt, meshSpace, meshTime=NULL){
### Find Dirichlet tesselation of the mesh
#=========================================
### Build Delaunay and Dirichlet
DelauDirich <- deldir(meshSpace$loc[,1], meshSpace$loc[,2])
DelauDirich <- deldir(meshSpace$loc[,1], meshSpace$loc[,2],
suppressMsge = TRUE)

### Dirichlet tiles
tiles <- tile.list(DelauDirich)
Expand Down
16 changes: 16 additions & 0 deletions R/expertMEGR-data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#' Expert map for pseudospecies MEGR
#'
#' An artificial expert map for pseudospecies MEGR
#'
#' @docType data
#'
#' @usage data(expertMEGR)
#'
#' @details
#'
#' A \code{SpatialPolygons} that can serve as an expert map for the sake of illustrations. This map highlights all the area where the soil water content is below 400 in the sampling regions. It was constructed from the soil water content (WatrCont) layer of \code{\link{mite.envRaster}}.
#'
#' The projection used for these data preserves the fine (meter) scale, which is useful for some of the illustration presented in the vignettes.
#'
#' @keywords datasets
"expertMEGR"
33 changes: 25 additions & 8 deletions R/explanaMesh.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#'
#' @description Prepare and organise the data for model prediction. This function is meant to be used as a starting point for all spatial and spatiotemporal model in this package. This function also gathers explanatory variables values associated to the edges within and outside the area of interest.
#'
#' @param sp A \code{SpatialPolygons} or a \code{SpatialPolygonsDataFrame}
#' @param sPoly A \code{\link[sp]{SpatialPolygons}} or a \code{\link[sp]{SpatialPolygonsDataFrame}} defining the sampling region.
#' @param mesh An \code{inla.mesh} object
#' @param X A \code{\link{raster}} that includes the explanatory variables to consider for the analysis. This could also be a \code{\link{stack}} or a \code{\link{brick}}.
#' @param verbose Logical. Whether or not to print on the screen (five times) the number of edges that have been assigned values.
Expand All @@ -12,9 +12,14 @@
#'
#' The time it takes to run this function is directly related to the number of vertex (points) in the mesh; more specifically the ones outside the region of interest.
#'
#' Also, this function checks the projections of \code{sp}, \code{mesh} and \code{X} to make sure they match. Note that if all projections are NAs, the function will run but warnings messages will be printed on screen related to this.
#' Also, this function checks the projections of \code{sPoly}, \code{mesh} and \code{X} to make sure they match. Note that if all projections are NAs, the function will run but warnings messages will be printed on screen related to this.
#'
#' @return
#'
#' An object of class \code{explanaMesh} that includes a list of all the objects used as arguments (except for the verbose call) and \code{Xmesh} the values of the explanatory variables for all edges of the mesh.
#'
#' @importFrom sp SpatialPoints
#' @importFrom sp bbox
#' @importFrom raster crs
#' @importFrom raster raster
#' @importFrom raster res
Expand All @@ -26,19 +31,27 @@
#' @importFrom raster crop
#' @importFrom raster mask
#' @importFrom raster values
#' @importFrom raster xyFromCell
#' @importFrom raster ncell
#' @importFrom raster cellFromXY
#' @importFrom raster xmin
#' @importFrom raster ymin
#' @importFrom raster xmax
#' @importFrom raster ymax
#' @importFrom stats dist
#'
#' @keywords manip
#'
#' @export
explanaMesh <- function(sp, mesh, X, verbose = TRUE){
explanaMesh <- function(sPoly, mesh, X, verbose = TRUE){
#=================
# Check projection
#=================
# Extract projection of objects
projSp <- crs(sp)@projargs
projSp <- crs(sPoly)@projargs
projX <- crs(X)@projargs

if(is.na(crs(mesh))){
if(is.null(mesh$crs)){
projMesh <- NA
}else{
projMesh <- mesh$crs@projargs
Expand Down Expand Up @@ -101,7 +114,7 @@ explanaMesh <- function(sp, mesh, X, verbose = TRUE){

# Construct SpatialPoints from mesh edges
loc <- SpatialPoints(coords = new.pts,
proj4string = crs(sp))
proj4string = crs(sPoly))

# Extract values from X at loc
locVal <- extract(X, loc)
Expand All @@ -115,7 +128,7 @@ explanaMesh <- function(sp, mesh, X, verbose = TRUE){

if(nlocValNA > 0){
loc <- SpatialPoints(coords = coordinates(loc)[locValNA,1:2],
proj4string = crs(sp))
proj4string = crs(sPoly))

for(i in 1:nlocValNA){
extX <- extent(X)
Expand Down Expand Up @@ -153,6 +166,10 @@ explanaMesh <- function(sp, mesh, X, verbose = TRUE){
locVal <- as.data.frame(locVal)

# Check for factors in X and convert locVal accordingly
if(class(X) =="RasterBrick"){
X <- stack(X)
}

Xfactor <- unlist(lapply(X@layers,function(x) x@data@isfactor))

if(any(Xfactor)){
Expand All @@ -164,7 +181,7 @@ explanaMesh <- function(sp, mesh, X, verbose = TRUE){
}

# return
results <- list(mesh = mesh, Xmesh = locVal, X = X)
results <- list(sPoly = sPoly, mesh = mesh, X = X, Xmesh = locVal)
class(results) <- "explanaMesh"

return(results)
Expand Down
2 changes: 2 additions & 0 deletions R/inla.mesh.dual.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#' @importFrom parallel mclapply

inla.mesh.dual <- function(mesh) {
### Function obtained from : http://www.math.ntnu.no/inla/ r-inla.org/tutorials/spde/R/spde-tutorial-functions.R
###
Expand Down
18 changes: 11 additions & 7 deletions R/mapSpace.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
#' @param modelSpace An object of class \code{ppSpace} or of class \code{uniSpace}.
#' @param dims A vector of length 2 defining the number of pixels to use as rows and columns to define the map.
#' @param type Either "mean", "sd", "0.025quant", "0.5quant", "0.975quant" or "mode". Defines the map to be drawn.
#' @param sp A spatial polygon to isolate the region of interest. If none is given, a map is drawn for the entire region covered by the mesh.
#' @param sPoly A spatial polygon to isolate the region of interest. If none is given, a map is drawn for the entire region covered by the mesh.
#'
#' @importFrom INLA inla.mesh.projector
#' @importFrom INLA inla.mesh.project
#' @importFrom INLA inla.stack.index
#' @importFrom raster raster
#' @importFrom raster mask
#' @importFrom raster xmin
#' @importFrom raster ymin
#' @importFrom raster xmax
#' @importFrom raster ymax
#'
#' @keywords hplot
#'
Expand All @@ -20,23 +24,23 @@
mapSpace <- function(modelSpace, dims,
type = c("mean", "sd", "0.025quant",
"0.5quant", "0.975quant",
"mode"), sp = NULL){
"mode"), sPoly = NULL){
### General check
if(length(type) > 1){
stop("Only one type should be defined")
}

### Define map basis
if(is.null(sp)){
if(is.null(sPoly)){
mapBasis <- inla.mesh.projector(attributes(modelSpace)$mesh,
dims = dims,
crs = crs(attributes(modelSpace)$spdf))
crs = attributes(modelSpace)$mesh$crs)
}else{
mapBasis <- inla.mesh.projector(attributes(modelSpace)$mesh,
dims = dims,
xlim = c(xmin(sp), xmax(sp)),
ylim = c(ymin(sp), ymax(sp)),
crs = crs(attributes(modelSpace)$spdf))
xlim = c(xmin(sPoly), xmax(sPoly)),
ylim = c(ymin(sPoly), ymax(sPoly)),
crs = attributes(modelSpace)$mesh$crs)
}

### Find the mesh edges on which predictions should be made
Expand Down
9 changes: 5 additions & 4 deletions R/mapSpaceTime.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
#' @param spaceTimeModel An object of class \code{spatialTimePP}.
#' @param dims A vector of length 2 defining the number of pixels in rows and columns of the map.
#' @param type Either "mean", "sd", "0.025quant", "0.5quant", "0.975quant" or "mode". Defines the map to be drawn.
#' @param sp A spatial polygon to isolate the region of interest. If none is given, a map is drawn for the entire region covered by the mesh.
#' @param sPoly A spatial polygon to isolate the region of interest. If none is given, a map is drawn for the entire region covered by the mesh.
#'
#' @importFrom INLA inla.mesh.projector
#' @importFrom INLA inla.mesh.project
#' @importFrom INLA inla.stack.index
#' @importFrom raster raster
#' @importFrom raster mask
#' @importFrom raster stack
#'
#' @keywords hplot
#'
Expand All @@ -20,7 +21,7 @@
mapSpaceTime <- function(spaceTimeModel, dims,
type = c("mean", "sd", "0.025quant",
"0.5quant", "0.975quant",
"mode"), sp = NULL){
"mode"), sPoly = NULL){
### General check
if(length(type) > 1){
stop("Only one type should be defined")
Expand Down Expand Up @@ -52,8 +53,8 @@ mapSpaceTime <- function(spaceTimeModel, dims,
mapRaster <- stack(mapPred)

### Isolate region of interest
if(!is.null(sp)){
mapRaster <- mask(mapRaster, sp)
if(!is.null(sPoly)){
mapRaster <- mask(mapRaster, sPoly)
}

### Return
Expand Down
5 changes: 4 additions & 1 deletion R/mite.envRaster-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
#'
#' The \code{mite.envRaster} data is a \code{rasterStack} object contaning 5 environmental variables gather across the 2.6 m by 10 m transect. Originally, substrate density (SubsDens) and soil water content (WatrCont) were only gathered at the sampled locations; they were interpolated using kriging for values to be available across the full area of the transect. The other thress variables (Substrate, Shrub and Topo) were rasterized using Figure 1 of Borcard and Legendre (1994) as reference.
#'
#' The projection used for these data preserves the fine (meter) scale, which is useful for some of the illustration presented in the vignettes.
#'
#' @references
#' Borcard, D., and P. Legendre. 1994. Environmental control and spatial structure in ecological communities: an example using oribatid mites (Acari, Oribatei). Environmental and Ecological Statistics \emph{1}:37–61.
#'
#' Borcard, D., and P. Legendre. 1994. Environmental control and spatial structure in ecological communities: an example using oribatid mites (Acari, Oribatei). \emph{Environmental and Ecological Statistics} \strong{1}:37–61.
#'
#' @keywords datasets
"mite.envRaster"
6 changes: 5 additions & 1 deletion R/mite.spdf-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
#'
#' The \code{mite.envRaster} data is a \code{rasterStack} object contaning 5 environmental variables gather across the 2.6 m by 10 m transect. Originally, substrate density (SubsDens) and soil water content (WatrCont) were only gathered at the sampled locations; they were interpolated using kriging for values to be available across the full area of the transect. The other thress variables (Substrate, Shrub and Topo) were rasterized using Figure 1 of Borcard and Legendre (1994) as reference.
#'
#' The projection used for these data preserves the fine (meter) scale, which is useful for some of the illustration presented in the vignettes.
#'
#'
#' @references
#' Borcard, D., and P. Legendre. 1994. Environmental control and spatial structure in ecological communities: an example using oribatid mites (Acari, Oribatei). Environmental and Ecological Statistics \emph{1}:37–61.
#'
#' Borcard, D., and P. Legendre. 1994. Environmental control and spatial structure in ecological communities: an example using oribatid mites (Acari, Oribatei). \emph{Environmental and Ecological Statistics} \strong{1}:37–61.
#'
#' @keywords datasets
"mite.spdf"
Loading

0 comments on commit 4af8768

Please sign in to comment.