
Hello,
I am conducting a spatiotemporal analysis of binomial data observed on an island using the SPDE approach, and I am running into a strange error when attempting to map the spatial effect across my study area. I have cobbled together this approach from several guides and tutorials online, so apologies if there are any glaring omissions.
I've made my mesh from a polygon and a set of points, using unionSpatialPolygons and inla.sp2segment to turn the polygon into an inla.mesh.segment object for use in inla.mesh.create.helper.
loc<-cbind(DataWithDaymet$Long,DataWithDaymet$Lat) #Stores the lat/longs of each observation site
shape<-readOGR(dsn="LocationOfLayer", layer=("SuffolkPolygonDecDeg"))
ob <- unionSpatialPolygons(shape, rep(1, nrow(shape))) #This simplifies and unifies the boundary for faster processing
bound<-inla.sp2segment(ob) #Converts the polygon boundary to a inla.mesh.segment object, which is a series of line segments
(mesh<-inla.mesh.create.helper(points=cbind(DataWithDaymet$Long,DataWithDaymet$Lat),points.domain=bound$loc,offset=c(0.01,0.15),max.edge=c(0.1,0.2)))$n
Here is what the shapefile looks like with the observation locations printed on it:
plot(mesh,asp=1)
lines(bound,lwd=2)
points(loc,cex=.75,pch=20,col=2)
Here is the mesh I am using, with the outline of the island my study is on superimposed in blue, and my observation points superimposed in red for reference:

However, when I run the analysis using the following code:
A <-inla.spde.make.A(mesh, loc)
spde<-inla.spde2.matern(mesh,alpha=2)
formula<-Result ~ 0 + Intercept + AREIS_SepticCount1km + NDVI + Developed..Low.Intensity + Emergent.Herbaceous.Wetlands + WkMeanPrcp + WkMeanTmax + f(spatial, model=spde) #Defining the formula and variables
vars<-DataWithDaymet[,c(11,12,15,27,49,50)] #Takes out from the full dataset only the variables of interest mentioned in the formula
vars<-scale(vars)
stk<-inla.stack(data=list(Result=DataWithDaymet$Result),A=list(A,1),effect=list(list(spatial=1:spde$n.spde),data.frame(Intercept=1,vars))) #Creating the stack for easy specification of INLA
r<-inla(formula, family="binomial", Ntrials=10596, data=inla.stack.data(stk), control.family=list(link="logit"), control.predictor=list(A=inla.stack.A(stk)),control.compute=list(dic=TRUE,cpo=TRUE)) #Running the INLA and checking marginals
summary(r)
and then use the following code to try and plot the spatial effect mean and SD onto the projector grid.
#Plotting the response on a grid
library(lattice)
projection<-inla.mesh.projector(mesh, dims=c(500,500)) #projects the mesh onto a square grid
table(
xy.in <- inout(projection$lattice$loc,cbind(bound$loc[,1], bound$loc[,2])))
#Removes the grid points that are outside the boundary#These are for the spatial field random effect, not the response
prd.m<-inla.mesh.project(projection,r$summary.ran$spatial$mean) #Projects the spatial effect mean onto the grid
prd.s<-inla.mesh.project(projection,r$summary.ran$spatial$s) #Projects the spatial effect SD onto the grid
prd.m[!
xy.in] <- prd.s[!
xy.in] <- NA
#sets the projections outside the boundary to "NA"library(gridExtra)
levelplot(prd.m, col.regions=terrain.colors(16))
levelplot(prd.s, col.regions=terrain.colors(16))
The resulting graph looks like this. My outline is chopped-up and distorted, despite using the boundary shapefile "bound" shown above which, when plotted, shows a normal shape. What might be causing this?

Thank you very much,
---Mark