I am interesting in getting a vector of points values from the SPDE Matérn model on S^2
where the sampling sites belong to a small area on the sphere (large enough for the curvature to matter).
I got something along the line of the below which seems to work fine for a window large enough
but returns many NAs for a small window. Any work around?
require(INLA);require(fields)
set.seed(1)
n <- 100
sigma2.approx = 0.5^2
range.approx = 0.4
kappa = sqrt(8)/range.approx
tau = 1/(4*pi*kappa^2*sigma2.approx)^0.5
## here sampling window smaller than needed to excerbate the problem
s <- cbind(runif(n,0,5),runif(n,40,45))
lonlat3D=function(lon,lat){
cbind(cos((lon/180)*pi)*cos((lat/180)*pi),
sin((lon/180)*pi)*cos((lat/180)*pi),
sin((lat/180)*pi))
}
true.radius.of.earth = 6371
s3 <- lonlat3D(s[,1],s[,2])
mesh <- inla.mesh.create(loc=s3,
cutoff=100/true.radius.of.earth,
refine=list(max.edge=500/true.radius.of.earth))
spde=inla.spde.create(mesh, model="matern")
## proj = inla.mesh.projector(mesh,dims=c(180,90),projection="longsinlat")
proj = inla.mesh.projector(mesh,dims=c(180,90),projection="longlat") ## better than longsinlat in terms of not getting NAs
x <- inla.spde.query(spde, sample=list(tau=tau, kappa2=kappa^2))$sample
omesh = old.mesh.class(mesh)
cp=colorRampPalette(c("blue","cyan","yellow","red"))
plot(omesh, x, color.palette=cp, draw.edges=TRUE, draw.vertices=TRUE)
plotdata.x = inla.mesh.project(proj, x)
dev.new();image.plot(plotdata.x, col=cp(256))
## extracting values from matrices for sampling sites
xdat <- numeric(nrow(s))
xmat <- matrix(nr=180,nc=90,plotdata.x)
for(k in 1:nrow(s))
{
xc <- s[k,1]
yc <- s[k,2]
ii <- which.min(abs(xc-seq(-180,180,length=180)))
jj <- which.min(abs(yc-seq(-90,90,length=90)))
xdat[k] <- xmat[ii,jj]
}
mean(
is.na(xdat)) ## many NAs