library(mvtnorm) # package with multivariate normal density
library(latex2exp) # latex maths in plots
Bayesian logistic regression for the Titanic data
Load packages
Settings
<- 10 # Prior std beta~N(0,tau^2*I) tau
Read data and set up prior
<- read.csv(
df "https://github.com/mattiasvillani/introbayes/raw/main/data/titanic.csv",
header=TRUE)
<- as.vector(df[,1])
y <- df[,c(5, 4, 2)]
X 2] <- 1*(X[,2] == "female")
X[, 3] <- 1*(X[,3] == 1)
X[, <- as.matrix(cbind(1,X))
X <- dim(X)[2]
p = c("intercept", "age", "sex", "firstclass")
varNames
# Setting up the prior
<- as.vector(rep(0,p)) # Prior mean vector
mu <- tau^2*diag(p) Sigma
Coding up the log posterior function
<- function(betaVect, y, X, mu, Sigma){
LogPostLogistic <- length(betaVect)
p <- X%*%betaVect
linPred <- sum( linPred*y - log(1 + exp(linPred)))
logLik <- dmvnorm(betaVect, matrix(0,p,1), Sigma, log=TRUE)
logPrior return(logLik + logPrior)
}
Finding the mode and observed information using optim
<- as.vector(rep(0,p));
initVal <-optim(initVal, LogPostLogistic, gr=NULL, y, X, mu, Sigma,
OptimResultsmethod = c("BFGS"), control=list(fnscale=-1), hessian=TRUE)
= OptimResults$par
postMode = -solve(OptimResults$hessian) # inv(J) - Approx posterior covar matrix
postCov <- sqrt(diag(postCov)) # Approximate stdev postStd
Plot the marginal posterior of
Since we have approximated the joint posterior
par(mfrow=c(2,2))
for (j in 1:4){
= seq(postMode[j] - 3*postStd[j], postMode[j] + 3*postStd[j],
gridVals length = 100)
plot(gridVals, dnorm(gridVals, mean = postMode[j], sd = postStd[j]),
xlab = TeX(sprintf(r'($\beta_%d$)', j-1)), ylab= "posterior density",
type ="l", bty = "n", lwd = 2, col = "cornflowerblue", main =varNames[j])
}
Plot the marginal posterior of the odds
Since the marginal posterior for each
par(mfrow=c(2,2))
for (j in 1:4){
= exp(seq(postMode[j] - 3*postStd[j], postMode[j] + 3*postStd[j],
gridVals length = 100))
plot(gridVals, dlnorm(gridVals, meanlog = postMode[j], sdlog = postStd[j]),
xlab = TeX(sprintf(r'($\exp(\beta_%d$))', j-1)), ylab= "posterior density",
type ="l", bty = "n", lwd = 2, col = "cornflowerblue", main = varNames[j])
}