I will use example dataset bei from spatstat package. The data are positions of 3605 individual trees of Beilschmiedia pendula (Lauraceae) in a 1000 by 500 metre rectangular sampling region in the tropical rainforest of Barro Colorado Island. The data are stored in a point process pattern ppp object.

-- jags/jagam test  

  library(mgcv)   # fits GAMs
  library(spatstat) # the source of the example data
  library(raster) # for operations with rasters
  library(R2jags) # interface between R and JAGS

  plot(bei, cex=0.1, main=NULL)

 # cropping the data so that they have exactly 500 x 1000 cells
  ext <- extent(0, 1000, 0, 500)         # spatial extent of the raster
  empty <- raster(ext, nrow=25, ncol=50) # empty raster
  # aggregating the point data into the raster
  xy <- data.frame(x = bei$x, y = bei$y)
  rst <- rasterize(xy, empty, fun = "count")
  # replacing the NA values by 0
  rst[] <- 0
  # extracting the cell values and their coordinates to a data.frame
  coord <- xyFromCell(rst,1:ncell(rst))
  count <- extract(rst, 1:ncell(rst)) <- data.frame(coord, count=count)

  plot(rst, axes=FALSE)
  points(xy, cex=0.1)

  # the gam model with s() indicating that I fit splines
  space.only <- gam(count~s(x, y),, family = "poisson")
  # extraction of the predictions
  preds.mgcv <- as.vector(predict(space.only, type = "response"))

  # putting the predictions into a raster
  rst.mgcv <- rst
  rst.mgcv[] <- preds.mgcv
    plot(rst.mgcv, axes=FALSE)
  points(xy, cex=0.1)

jags.ready <- jagam(count~s(x, y),, 

readLines("jagam.bug") <- jags(data=jags.ready$, 
  n.burnin=500) <- as.vector($BUGSoutput$mean$mu)

 rst.jags <- rst
  rst.jags[] <-

 plot(rst.jags, axes=FALSE)
  points(xy, cex=0.1) # adding the positions of individual trees
Beothuk/bio.spacetime documentation built on Oct. 18, 2017, 8:50 a.m.