spde <- inla.spde2.pcmatern(Mesh1, alpha = 2,
prior.range=c(3, 0.05),
prior.sigma=c(6, 0.05)
)
s_index_horz <- inla.spde.make.index(name="spatial.field.horz",
n.spde=spde$n.spde)
stk.horz <- inla.stack(data=list(y=cbind(data$horz_break,NA)),
A=list(A),
effects=list(c(s_index_horz, list(intercept1=1))),
tag="horz")
s_index_induced <- inla.spde.make.index(name="spatial.field.induced",
n.spde=spde$n.spde)
stk.induced <- inla.stack(data=list(y=cbind(NA,data$induced_vert_break)),
A=list(A),
effects=list(c(s_index_induced, list(intercept2=1, s12=1:spde$n.spde))),
tag="induced")
stk <- inla.stack(stk.horz, stk.induced)
horz_mean <- mean(data$horz_break)
horz_prec <- 1/sd(data$horz_break)^2
induced_mean <- mean(data$induced_vert_break)
induced_prec <- 1/sd(data$induced_vert_break)^2
hyper <- list(theta=list(prior='normal', param=c(0,10)))
formula <- y ~ -1 +
f(intercept1, model='linear', prec.linear=horz_prec, mean.linear=horz_mean) +
f(intercept2, model='linear', prec.linear=induced_prec, mean.linear=induced_mean) +
f(spatial.field.horz, model = spde) +
f(spatial.field.induced, model = spde) +
f(s12, copy="spatial.field.horz", fixed=FALSE, hyper=hyper)
hyper.eps <- list(hyper = list(theta = list(prior = 'pc.prec', param = c(1, 0.01))))
int.strategy <- 'auto'
fit <- inla(formula, family=c('gaussian', 'gaussian'),
data = inla.stack.data(stk),
control.predictor = list(A=inla.stack.A(stk)),
control.inla=list(strategy='adaptive',
int.strategy=int.strategy),
control.family=list(hyper.eps, hyper.eps),
verbose=TRUE
)
# Prediction
n=1000
samples = inla.posterior.sample(n=n, fit, num.threads=6)
contents=fit$misc$configs$contents
## intercept1
idx_intercept1=which(contents$tag=="intercept1")
idx_intercept1=contents$start[idx_intercept1]:(contents$start[idx_intercept1]+contents$length[idx_intercept1]-1)
intercept1 = lapply(samples, function(x) x$latent[idx_intercept1])
intercept1 = matrix(unlist(intercept1), ncol = length(idx_intercept1), byrow = T)
## intercept2
idx_intercept2=which(contents$tag=="intercept2")
idx_intercept2=contents$start[idx_intercept2]:(contents$start[idx_intercept2]+contents$length[idx_intercept2]-1)
intercept2 = lapply(samples, function(x) x$latent[idx_intercept2])
intercept2 = matrix(unlist(intercept2), ncol = length(idx_intercept2), byrow = T)
## spatial.field.horz
idx_spat.horz=which(contents$tag=="spatial.field.horz")
idx_spat.horz=contents$start[idx_spat.horz]:(contents$start[idx_spat.horz]+contents$length[idx_spat.horz]-1)
spat.horz = lapply(samples, function(x) x$latent[idx_spat.horz])
spat.horz = matrix(unlist(spat.horz), ncol = length(idx_spat.horz), byrow = T)
## spatial.field.induced
idx_spat.induced=which(contents$tag=="spatial.field.induced")
idx_spat.induced=contents$start[idx_spat.induced]:(contents$start[idx_spat.induced]+contents$length[idx_spat.induced]-1)
spat.induced = lapply(samples, function(x) x$latent[idx_spat.induced])
spat.induced = matrix(unlist(spat.induced), ncol = length(idx_spat.induced), byrow = T)
# s12
idx_spat.horz.induced=which(contents$tag=="s12")
idx_spat.horz.induced=contents$start[idx_spat.horz.induced]:(contents$start[idx_spat.horz.induced]+contents$length[idx_spat.horz.induced]-1)
spat.horz.induced = lapply(samples, function(x) x$latent[idx_spat.horz.induced])
spat.horz.induced = matrix(unlist(spat.horz.induced), ncol = length(idx_spat.horz.induced), byrow = T)
A_i <- inla.spde.make.A(Mesh1, loc=as.matrix(loc.domain))
pred.horz <- A_i %*% t(spat.horz)
pred.horz <- intercept1 + t(pred.horz)
pred_horz_mean <- apply(pred.horz, 2, mean)
# Much lower than expected
pred_horz_sd <- apply(pred.horz,2,sd)
pred.induced <- A_i %*% t(spat.induced)
pred.horz.induced <- A_i %*% t(spat.horz.induced)
pred.induced <- intercept2 + t(pred.induced) + t(pred.horz.induced)
pred_induced_mean <- apply(pred.induced, 2, mean)
# Much lower than expected
pred_induced_sd <- apply(pred.induced, 2, sd)
# This looks very different than the covariance of the data
cov(data.frame(x=pred.horz[,1],y=pred.induced[,1]))