Following the FAQ Help Topic: "How can I get predictions using INLA", I was able to predict posterior means. How can I get the 0.025quant, and 0.975quant to make the plot given below?
I would eventually want to do out of sample predictions. But that will be a next step.
Data Files attached:
data4<-read.csv("Falldata4.csv")
data4$y1<-exp(data4$y)-1
Fluke.adj <- paste(getwd(),"/Fluke.graph",sep="")
g = inla.read.graph("Fluke.graph")
formula.zip<- y1 ~ BotTemp+BotSalin+
f(ID.area,model="bym", graph=g) +
f(ID.year,model="rw2") +
f(ID.year1,model="iid")+
f(ID.area.year,model="iid")
link = rep(NA, nrow(data4))
link[which(
is.na(data4$y1))] = 1
result.pred = inla(formula.zip,
data = data4, offset = log(data4$E+1),
family = "zeroinflatedpoisson1",
control.predictor=list(compute=T))
n = nrow(data4)
fitted.values.mean = numeric(n)
for(i in 1:n) {
if (
is.na(data4$y1[i])) {
if (FALSE) {
## either like this, which is slower...
marg = inla.marginal.transform(
function(x) exp(x)/(1+exp(x)),
result.pred$marginals.fitted.values[[i]] )
fitted.values.mean[i] = inla.expectation(
function(x) x, marg)
} else {
## or like this, which is much faster...
fitted.values.mean[i] = inla.emarginal(
function(x) exp(x)/(1 +exp(x)),
result.pred$marginals.fitted.values[[i]])
}
} else {
fitted.values.mean[i] = result.pred$summary.fitted.values[i,"mean"]
}
}
fitted.vals<-as.data.frame(fitted.values.mean[1:n])
#Plot Results
data.pred<-cbind(data4,fitted.vals)
data.pred$Year<-data4$ID.year
plot(supsmu(data.pred$Year, data.pred$fitted.values.mean[1:n]), type = "l", col = "red", ylim = c(-0.1,2), ylab = "Fluke (ALB Units)", xlab = "Years(1991-2020)")
lines(supsmu(data.pred$Year, data.pred$`0.025quant`), col = "red", lwd=0.5, lty=2)
lines(supsmu(data.pred$Year, data.pred$`0.975quant`), col = "red", lwd=0.5, lty=2)
lines(supsmu(data.pred$Year, data.pred$y1), col = "black", lwd=0.5)
abline(v=24)