knitr::opts_chunk$set(echo = TRUE, cache = TRUE)
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)
MCMC-Algorithmen erzeugen eine Markovkette aus Ziehungen aus der Posteriori-Verteilung. Probleme:
summary(mcmc.simple)
plot(mcmc.simple)
mcmc.laenger<-poisson.lognormal.mcmc(sumx,n, I=10000) plot(mcmc.laenger)
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 (?)
plot(as.vector(mcmc.simple[,1]),type="l",ylab="mu")
mcmc.tuning<-poisson.lognormal.mcmc(sumx,n,do.tuning=TRUE)
plot(as.vector(mcmc.tuning[,1]),type="l",ylab="mu")
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")
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")
par(mfrow=c(1,2)) acf(mcmc.simple[,1]) acf(mcmc.simple[,2])
par(mfrow=c(1,2)) acf(mcmc.tuning[,1]) acf(mcmc.tuning[,2])
Potential scale reduction factor $$ \hat{R}=\sqrt\frac{\bar{Var}(\theta)}{W} $$
Ist $\hat{R}$ zu groß ($>1.1$?), sollten die Ketten länger laufen.
require(coda) mc.list<-parallel::mclapply(rep(sumx, 5), poisson.lognormal.mcmc,n=n) print(gelman.diag(mc.list))
require(coda) mc.list<-parallel::mclapply(rep(sumx, 5), poisson.lognormal.mcmc,n=n,I=10000) print(gelman.diag(mc.list))
print(geweke.diag(mcmc.simple))
print(geweke.diag(mcmc.tuning))
print(raftery.diag(mcmc.simple, q = 0.5, r = 0.05, s = 0.9))
print(heidel.diag(mcmc.simple))
print(heidel.diag(mcmc.tuning))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.