1

Let's imagine I want to fit an INLA model with a large dataset and want to do some prediction using the fitted model. One solution is to include into the date the covariates used to predict the outcome, assigning NA values to the outcome.

In my case, I want to regularly calculate the predicted outcome based on updated covariates. Ideally, I would like to run the model once to fit it, save it, and use it later to regularly make prediction (e.g for linear model 'lm()' in R using the 'predict()' function).

I haven't found a way to do it in INLA. Below is a simple reproducible example.

library(INLA)

#simulate data
n = 100; a = 1; b = 1; tau = 100
z = rnorm(n)
eta = a + b*z

scale = exp(rnorm(n))
prec = scale*tau
y = rnorm(n, mean = eta, sd = 1/sqrt(prec))
plot(z,y)

#run INLA model
data = list(y=y, z=z)
formula = y ~ 1+z
result = inla(formula, family = "gaussian", data = data)
summary(result)

#define prediction data
data.pred$z = c(data$z,seq(2,4,length.out=100))
data.pred$y = c(data$y, y=rep(NA,100))

#run INLA model with prediction
result = inla(formula, family = "gaussian", data = data,
              control.compute=list(config = TRUE))
summary(result)

#get posterior samples of the predictions
post.samples <- inla.posterior.sample(n = 10, result = result)
pred <- do.call(cbind,
                lapply(post.samples,
      

             function(X) X$latent[startsWith(rownames(X$latent), "Pred")]))
Anthony
  • 377
  • 2
  • 6
  • 13

0 Answers0