This example demonstrates how to fit a Poisson generalized
linear model (GLM) using the INLA package in
R.
We simulate a small dataset with one covariate and analyze the posterior
distribution of the regression coefficients.
GLM, Poisson,
Bayesian, INLA, RegressionWe generate Poisson count data with one covariate \(x\):
\[ \eta = \beta_0 + \beta_1 x, \quad y \sim \text{Poisson}(\mu), \quad \mu = e^\eta \]
# Simulate data
set.seed(123)
n <- 100
x <- rnorm(n)
eta <- 1 + 0.5 * x
mu <- exp(eta)
y <- rpois(n, mu)
data <- data.frame(y = y, x = x)
head(data)We fit a Poisson regression model using inla() with the
formula y ~ x:
# Load INLA
library(INLA)
# Define the model
formula <- y ~ x
# Fit the model
result <- inla(formula, family = "poisson", data = data)We summarize the fixed effects and visualize the posterior means.
# Plot posterior means
plot(result$summary.fixed$mean, type = "b", pch = 19,
col = "steelblue",
main = "Posterior Means of Coefficients",
ylab = "Posterior Mean", xlab = "Coefficient")
axis(1, at = 1:2, labels = rownames(result$summary.fixed))# Posterior means with 95% credible intervals
result$summary.fixed[, c("mean", "0.025quant", "0.975quant")]We now compare observed versus fitted values:
# Add fitted values to data
data$fitted <- result$summary.fitted.values$mean
# Plot
plot(data$y, data$fitted, pch = 19, col = "darkgreen",
xlab = "Observed y", ylab = "Fitted values",
main = "Observed vs Fitted")
abline(0, 1, col = "red", lty = 2)
7 Comments and Extensions
This is a minimal INLA example. You can extend it to:
f(id, model = "iid")binomial,nbinomial,gaussianExample with random intercepts: