Dear all,
I am trying to fit a point pattern model with inla that involves a spde spatial field and one (for now) covariate that doesn’t have a linear relation to the intensity, and so it is modelled as an “rw2” model. Because I am new at inla, I am working by steps of increasing complexity to try and understand what is going on.
First of all, mi data, mesh, boundary and covariate values at the mesh nodes and data points are in the attached datafile.
I set the spde model like so:
spde <- inla.spde2.pcmatern(mesh = mesh, alpha = 2,### mesh and smoothness parameter
prior.range = c(300, 0.01), ### P(practic.range<300)=0.01
prior.sigma = c(1, 0.1)) ### P(sigma>3)=0.1
And construct my “response” variable (with 0 weights outside the boundary) with the following code:
# 1.2 calculate weights for each node ####
dmesh <- inla.mesh.dual(mesh)
# number of meshnodes and number of datapoints
(nv <- mesh$n)
(n <- nrow(ds.df3)) #ds.df3 is the dataset containing the points
# calculate weights for each meshnode depending on whether they are inside or outside the boundary
sum(w <- sapply(1:length(dmesh), function(i) {
if (gIntersects(dmesh[i,], bound))
return(gArea(gIntersection(dmesh[i,], bound)))
else return(0)
}))
table(w > 0) # false = nodes with weight 0 (outside the boundary)/true = nodes with some weight (inside the boundary)
# plot mesh, weights, and boundary. As we are working with lgcp we don't mind the integration points being in the nodes of the mesh
par(mar = c(2,2,1,1), mgp = 2:0)
plot(mesh$loc, asp = 1, col = (w == 0) + 1, pch = 19, xlab = '', ylab = '')
plot(mesh, add = TRUE)
plot(bound, border = 3, add = T)
# 1.3 Construct response ####
length(y.pp <- rep(0:1, c(nv, n))) # response (0 for all meshnodes, 1 for all data points)
summary(y.pp) # all 0 and 1
# expected (weights at each mesh node and 0 for all data points)
length(e.pp <- c(w, rep(0, n)))
summary(e.pp) # different values
The A matrix is
generated like this:
lmat <- inla.spde.make.A(mesh, ds.df3@coords)
imat <- Diagonal(nv, rep(1, nv))
A.pp <- rBind(imat, lmat)
I now load the dataset with the value of the covariates at the mesh/datapoints. For the example, I’ll use the first column
load("covars.Rdata")
Now I construct the data stack and prepare the values for the hyperparameters of the rw2 model
stk.data <- inla.stack(data = list(y = y.pp, e = e.pp),
A = list(A.pp,1), tag = 'dat',
effects = list(list(sp_rdm = 1:spde$n.spde),
data.frame(b0 = 1,
gCovar = inla.group(covars[,1]))))
# 1.7 set priors for the covariate model in a list ####
pcprec <- list(prior = "pcprec",
param = c(1, 0.01))
As an intermediate step, I run the model without a prediction stack, just to check the covariate’s effect.
# 2.1 formula for the model with covariate ####
frml_covar <- y ~ 0 + b0 +
f(gCovar, model = 'rw2', scale.model = T, hyper = list(prec = list(param = pcprec$param))) +
f(sp_rdm, model = spde)
# 2.2 Run the model ####
cov.model <- inla(frml_covar, family = 'poisson',
data = inla.stack.data(stk.data),
control.predictor = list(A = inla.stack.A(stk.data),
compute = FALSE, link = 1),
E = inla.stack.data(stk.data)$e,
control.compute = list(dic = F, waic = F),
control.inla = list(int.strategy = "eb"),
verbose = TRUE)
You can check the
output of the model in this image.
Everything seems more or less correct, as
well as the spatial field you can see in this other image.
On with step 2, I try and do a prediction stack. For this, following the SPDE tutorial, I first create a dataset of prediction points based on a lattice, and load the covariate values at these new set of points (done previously)
# 3.1 prepare the prediction points ####
stepsize <- 10 # this will control the resolution (smaller step size, more resolution, but more computing time)
# dimensions of the lattice
(nxy <- round(c(diff(poly.barrier@bbox[1,]), # range of longitude
diff(poly.barrier@bbox[2,]))/stepsize)) # range of latitude
projgrid <- inla.mesh.projector(mesh, xlim = poly.barrier@bbox[1,], ylim = poly.barrier@bbox[2,], dims = nxy)
# create a Spatial Points dataset where we'll predict
predpts <- SpatialPoints(projgrid$lattice$loc, bbox = bound@bbox)
length(predpts)
# check these points are inside the boundary
plot(predpts, pch = ".")
plot(poly.barrier, add = T)
load("covarspred.Rdata")
I am interested in predicting the effect of the covariate on its own, without the spatial field or the intercept, and so I set the prediction stack like so:
stk.pred <- inla.stack(data = list(y = NA), #because we're making predictions
tag = "pred",
A = list(1),
effects = list(data.frame(Intercept = 1,
gcovarpred = inla.group(covarspred[,1]))))
And now I stack the stacks
stk.all <- inla.stack(stk.data, stk.pred)
Finally, I run the model including the prediction
# 3.4 formula for the predictions
form_all <- y ~ 0 + Intercept +
f(gcovarpred, model = 'rw2', scale.model = T, hyper = list(prec = list(param = pcprec$param))) +
f(sp_rdm, model = spde)
# 3.6 Run the model ####
pred.model <- inla(form_all, family = 'poisson',
data = inla.stack.data(stk.all),
E = inla.stack.data(stk.all)$e,
control.predictor = list(A = inla.stack.A(stk.all), compute = TRUE, link = 1),
control.compute = list(dic = FALSE, waic = FALSE),
control.inla = list(int.strategy = "eb"),
verbose = TRUE)
However, when I plot
the output of the model, the range and sd for the spatial field, the intercept,
the precision for the covariate, and the effect of the covariate all are
different. The quantiles of the effect are so far away from the mean that don’t
even appear in the plot.
When plotting the mean (top) and sd (bottom) effect at each of the
integration points I get complete nonsensical results.
Out of experience I am pretty sure I have missed something along the way, but after checking and rechecking the SPDE tutorial and several posts on this group, I still can’t figure out what it is, so any tips will be greatly appreciated!
I attach full code and data necessary to rerun the code.
Thanks in advance and sorry for the lengthy post!
--
You received this message because you are subscribed to the Google Groups "R-inla discussion group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to r-inla-discussion-group+unsub...@googlegroups.com.
To post to this group, send email to r-inla-discussion-group@googlegroups.com.
Visit this group at https://groups.google.com/group/r-inla-discussion-group.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to r-inla-discussion...@googlegroups.com.
To post to this group, send email to r-inla-disc...@googlegroups.com.
Visit this group at https://groups.google.com/group/r-inla-discussion-group.
For more options, visit https://groups.google.com/d/optout.
Formal class 'dgeMatrix' [package "Matrix"] with 4 slots
..@ x : num [1:4658] 1 1 1 1 1 1 1 1 1 1 ...
..@ Dim : int [1:2] 4658 1
..@ Dimnames:List of 2
.. ..$ : NULL
.. ..$ : NULL
..@ factors : list()
Error in (function (data, A, effects, tag = "", compress = TRUE, remove.unused = TRUE) :
ncol(A) does not match nrow(effect) for block 2: 1 != 28
Error in rbind(deparse.level, ...) :
numbers of columns of arguments do not match
Which doesn't give me a lot of info to work with.
Sorry if I seem a bit clueless but of all the things I don't understand about inla (and believe me, there are many), A matrices are the thing I understand the least!
<INLA_fitting.R>
<INLA_fitting.R>
<INLA_fitting.R>