# geostatistical model for the swiss rainfall data
if(requireNamespace("INLA", quietly=TRUE) ) {
INLA::inla.setOption(num.threads=2)
# not all versions of INLA support blas.num.threads
try(INLA::inla.setOption(blas.num.threads=2), silent=TRUE)
}
require("geostatsp")
data("swissRain")
swissRain = unwrap(swissRain)
swissAltitude = unwrap(swissAltitude)
swissBorder = unwrap(swissBorder)
swissRain$lograin = log(swissRain$rain)
swissFit = glgm(formula="lograin", data=swissRain,
grid=30,
covariates=swissAltitude, family="gaussian",
buffer=2000,
prior = list(sd=1, range=100*1000, sdObs = 2),
control.inla = list(strategy='gaussian')
)
if(!is.null(swissFit$parameters) ) {
swissExc = excProb(swissFit, threshold=log(25))
swissExcRE = excProb(swissFit$inla$marginals.random$space,
log(1.5),template=swissFit$raster)
swissFit$parameters$summary
matplot(
swissFit$parameters$range$postK[,'x'],
swissFit$parameters$range$postK[,c('y','prior')],
type="l", lty=1, xlim = c(0, 1000),
xlab = 'km', ylab='dens')
legend('topright', lty=1, col=1:2, legend=c('post','prior'))
plot(swissFit$raster[["predict.exp"]])
mycol = c("green","yellow","orange","red")
mybreaks = c(0, 0.2, 0.8, 0.95, 1)
plot(swissBorder)
plot(swissExc, breaks=mybreaks, col=mycol,add=TRUE,legend=FALSE)
plot(swissBorder, add=TRUE)
legend("topleft",legend=mybreaks, fill=c(NA,mycol))
plot(swissBorder)
plot(swissExcRE, breaks=mybreaks, col=mycol,add=TRUE,legend=FALSE)
plot(swissBorder, add=TRUE)
legend("topleft",legend=mybreaks, fill=c(NA,mycol))
}
# a log-Gaussian Cox process example
myPoints = vect(cbind(rbeta(100,2,2), rbeta(100,3,4)))
mycov = rast(matrix(rbinom(100, 1, 0.5), 10, 10), extent=ext(0, 1, 0, 1))
names(mycov)="x1"
if(requireNamespace("INLA", quietly=TRUE) ) {
INLA::inla.setOption(num.threads=2)
# not all versions of INLA support blas.num.threads
try(INLA::inla.setOption(blas.num.threads=2), silent=TRUE)
}
res = lgcp(
formula=~factor(x1),
data=myPoints,
grid=squareRaster(ext(0,1,0,1), 20), covariates=mycov,
prior=list(sd=c(0.9, 1.1), range=c(0.4, 0.41),
control.inla = list(strategy='gaussian'), verbose=TRUE)
)
if(length(res$parameters)) {
plot(res$raster[["predict.exp"]])
plot(myPoints,add=TRUE,col="#0000FF30",cex=0.5)
}
Run the code above in your browser using DataLab