knitr::opts_chunk$set(echo = TRUE, cache = TRUE)

Beispiel MCMC bei Poisson-Lognormal

library(coda)
library(bayeskurs)
data("kaiserschnitt.raw")
data<-kaiserschnitt.raw

n <-dim(data)[1]
sumx <- sum(data$n)
mcmc.simple<-poisson.lognormal.mcmc(sumx,n)

Konvergenz

MCMC-Algorithmen erzeugen eine Markovkette aus Ziehungen aus der Posteriori-Verteilung. Probleme:

MCMC mit coda {.allowframebreaks}

summary(mcmc.simple)

MCMC mit coda

plot(mcmc.simple)

Längere Ketten

mcmc.laenger<-poisson.lognormal.mcmc(sumx,n, I=10000)
plot(mcmc.laenger)

Tuning des Random Walk Proposals

Random Walk Proposal: $$\theta^* = \theta^{(i-1)} + u$$ oft mit $u\sim N(0,c)$. Wie wählt man $c$?

Akzeptanzrate = Anteil akzeptierter Vorschläge Niedrige Akzeptanzrate: Kette bleibt oft im selben Zustand $\to$ schlecht Zu hohe Akzeptanzrate: Kette bewegt sich (eventuell) nur langsam $\to$ schlecht

Tuning: Finde optimalen Wert für $c$. Für Random Walk Proposal gelten Akzeptanzraten zwischen ca. 30% und 60% als optimal (?)

Beispiel zu hohe Akzeptranzrate

plot(as.vector(mcmc.simple[,1]),type="l",ylab="mu")

Poisson-Lognormal mit Tuning

mcmc.tuning<-poisson.lognormal.mcmc(sumx,n,do.tuning=TRUE)

Poisson-Lognormal mit Tuning

plot(as.vector(mcmc.tuning[,1]),type="l",ylab="mu")

Running mean plots

par(mfrow=c(1,2))
I=1000
plot(cumsum(mcmc.simple[,1])/1:I,type="l")
plot(cumsum(mcmc.simple[,2])/1:I,type="l")

Running mean plots

par(mfrow=c(1,2))
I=1000
plot(cumsum(mcmc.tuning[,1])/1:I,type="l",)
plot(cumsum(mcmc.tuning[,2])/1:I,type="l")

Auto correlation function

par(mfrow=c(1,2))
acf(mcmc.simple[,1])
acf(mcmc.simple[,2])

Auto correlation function

par(mfrow=c(1,2))
acf(mcmc.tuning[,1])
acf(mcmc.tuning[,2])

Konvergenzdiagnostik

Gelman-Rubin-Diagnostik

Gelman-Rubin-Diagnostik

Gelman-Rubin-Diagnostik

require(coda)
mc.list<-parallel::mclapply(rep(sumx, 5),
            poisson.lognormal.mcmc,n=n)
print(gelman.diag(mc.list))

Gelman-Rubin-Diagnostik

require(coda)
mc.list<-parallel::mclapply(rep(sumx, 5),
            poisson.lognormal.mcmc,n=n,I=10000)
print(gelman.diag(mc.list))

Gelman-Rubin-Diagnostik

Geweke-Diagnostik

print(geweke.diag(mcmc.simple))

Geweke-Diagnostik

print(geweke.diag(mcmc.tuning))

Raftery und Lewis Diagnostik

Raftery und Lewis Diagnostik

print(raftery.diag(mcmc.simple, 
                   q = 0.5, r = 0.05, s = 0.9))

Heidelberg und Welch Diagnostik

Heidelberg und Welch Diagnostik

print(heidel.diag(mcmc.simple))

Heidelberg und Welch Diagnostik

print(heidel.diag(mcmc.tuning))


volkerschmid/bayeskurs documentation built on Dec. 23, 2021, 4:10 p.m.