Problem Outline:
I am attempting to do some species distribution modeling using the function bioclim(). My map of Sri Lanka (Image 1: see below) was extracted from GADM resources, which I understand is a vector object.
For the bioclim() function to work, you need to use this format:
Usage
bioclim(x, p.....)
Arguments:
x Raster*object or matrix
p two column matrix or SpatialPoints object
I am quite new in regards to producing maps and doing species distribution modeling in R. After spending a lot of hours researching, I have tried my very best figure out how to rasterize this GADM vector object into raster file format to be inputted into the bioclim() function. I have been following these species distribution exercises in order to construct a species distribution model for GPS points which constitute the latitude/longitude points for blue whales that were recorded during field surveys.
Aim
The code below was used to produce a map of Sri Lanka (see image 1) with associated GPS points, and my aim is to produce a species distribution model using the function bioclim() and then display the probability bar onto a separate map (see image 2).
The aim of the model is to illustrate the probability of detecting blue whales within particular locations in Sri Lankan waters. Image 2 is the desired output.
After many attempts, I have finally produced code that works, but I am experiencing problems rasterizing the GADM vector object into raster format using the function system.time() and running the bioclim() model. Please see the 2 error message below.
If anyone can help me solve this problem, I cannot express my gratitude.
I cannot publish the data but I have provided a mini data frame below:
Many thanks in advance.
Error messages:
##Error message when I run the system.time() function to rasterize the GADM vector object into a raster file
Error in .getPutVals(p, field, npol, mask) : invalid value for field
Timing stopped at: 0.003 0 0.004
##Error message when I run the bioclim() model
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘bioclim’ for signature ‘"SpatialPolygonsDataFrame", "missing"’
R-code:
###Open Packages
library("sp")
library("rgdal")
library("raster")
library("maptools")
library("rgdal")
library("dismo")
library("spatialEco")
library("ggplot2")
library("dplyr")
library("maps")
library("ggspatial")
library("GADMTools")
library("maps")
##Mini dataframe for Blue.whale.new
Blue.whale.new <- data.frame(longitude = c(80.5, 80.5, 80.5, 80.5, 80.4, 80.4, 80.5, 80.5, 80.4),
latitude = c(5.84, 5.82, 5.85, 5.85, 5.89, 5.82, 5.82, 5.84, 5.83))
###Uploading the map from GADM resources
##Plotting the map of Sri Lanka
dev.new()
bioclim1.data <- getData('GADM', country='LKA', level=1)
####Get boudnign box of Sri Lanka shape file
bb=bioclim1.data@bbox
# Determine geographic extent of our data
max.lat <- ceiling(max(Blue.whale$latitude))
min.lat <- floor(min(Blue.whale$latitude))
max.lon <- ceiling(max(Blue.whale$longitude))
min.lon <- floor(min(Blue.whale$longitude))
geographic.extent <- extent(x = c(min.lon, max.lon, min.lat, max.lat))
#####Plot map
dev.new()
plot(bioclim1.data,
xlim = c(min(c(min.lon,bb[1,1])), max(c(max.lon,bb[1,2]))),
ylim = c(min(c(min.lat,bb[2,1])), max(c(max.lat,bb[2,2]))),
axes = TRUE,
col = "grey95")
# Add the points for individual observation
points(x = Blue.whale$longitude,
y = Blue.whale$latitude,
col = "olivedrab",
pch = 15,
cex = 0.50)
###Building a model and visualising results
##Crop bioclim data to geographic extent of blue whales
bioclim.data.blue.whale<-crop(x=bioclim1.data, y=geographic.extent)
###Rasterize the GADM Formatinto raster format
##Define Rasterlayer object
r.raster<-raster()
##Define raster layer extent
extent(r.raster)<-extent(bioclim1.data)
# Rasterize
system.time(bioclim1.data <- rasterize(bioclim1.data, r.raster))
Error in .getPutVals(p, field, npol, mask) : invalid value for field
Timing stopped at: 0.003 0 0.004
##Build species distribution model
Blue.whale.model<-bioclim(x=bioclim1.data, y=Blue.whale_New)
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘bioclim’ for signature ‘"SpatialPolygonsDataFrame", "data.frame"’
R-code I plan to run once this error message issue is solved:
# Predict presence from model
predict.presence <- dismo::predict(object = Blue.whale.model, x = bioclim1.data, ext = geographic.extent)
# Plot base map
plot(bioclim1.data,
xlim = c(min(c(min.lon,bb[1,1])), max(c(max.lon,bb[1,2]))),
ylim = c(min(c(min.lat,bb[2,1])), max(c(max.lat,bb[2,2]))),
axes = TRUE,
col = "grey95")
# Add model probabilities
plot(predict.presence, add = TRUE)
# Redraw those country borders
plot(bioclim1.data, add = TRUE, border = "grey5")
# Add original observations
points(Blue.whale_New$longitude, Blue.whale_New$latitude, col = "olivedrab", pch = 20, cex = 0.75)
box()
Image 1
Image 2 (Example of desired output):


bioclim1.datadoesn't have a variable called "NAME_2". It does have a variable called "NAME_1" but that will still fail as it is character data, not numeric. You need to reconsider your approach as you need data over the ocean, not land. You should build a rasterStack or matrix of ocean variables for variablexin thebioclimfunction, e.g. sea surface temp, salinity, bathymetry, currents, etc.