Skip to content

Commit 37be8b7

Browse files
committed
Merge branch 'main' of github.com:gregorkastner/BayesianLearningCode
2 parents ece72c8 + 8fb460a commit 37be8b7

1 file changed

Lines changed: 28 additions & 17 deletions

File tree

vignettes/Chapter08.Rmd

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ knitr::opts_chunk$set(
1717
fig.height = 6
1818
)
1919
knitr::opts_knit$set(global.par = TRUE)
20-
pdfplots <- FALSE # default: FALSE; set this to TRUE only if you like pdf figures
20+
pdfplots <- FALSE# default: FALSE; set this to TRUE only if you like pdf figures
2121
```
2222

2323
```{r, include = FALSE}
@@ -816,7 +816,7 @@ Gibbs sampler and the partially marginalised Gibbs sampler using a log random wa
816816
sample_alpha <- function(y, linpred,e, phi, pri.alpha,alpha.old,
817817
c.alpha,full.gibbs){
818818
819-
alpha.proposed <- exp(rnorm(1,log(alpha.old),c.alpha ))
819+
alpha.proposed <- exp(rnorm(1,log(alpha.old),c.alpha))
820820
821821
if (full.gibbs) {
822822
llik_alpha.proposed <- sum(dgamma(phi, shape = alpha.proposed,
@@ -904,7 +904,7 @@ negbin<- function(y,X,e, b0,B0, pri.alpha,c.alpha,
904904

905905
We use the same Normal prior as in the Poisson model for the
906906
regression effects $\boldsymbol{\beta}$ and a Gamma prior
907-
$\mathcal{G}(2 , 0.5)$ for $\alpha$. We first run the full Gibbs sampler for $M=50,000$ iterations after a burn-in of 1000.
907+
$\mathcal{G}(2 , 0.5)$ for $\alpha$. We first run the full Gibbs sampler for $M=10,000$ iterations after a burn-in of 1000.
908908

909909
```{r}
910910
d <- ncol(X)
@@ -914,7 +914,7 @@ B0=diag(100,d)
914914
pri.alpha <- data.frame(shape = 2, rate = 0.5)
915915
c.alpha=0.1
916916
917-
M <- 10000L
917+
M <- 50000L
918918
919919
# Full Gibbs sampler
920920
set.seed(1234)
@@ -985,11 +985,13 @@ negbin_check_abc <- function(X,e, b0,B0, pri.alpha,c.alpha,
985985
beta <- as.vector(mvtnorm::rmvnorm(1, mean = b0, sigma = B0))
986986
alpha <- pri.alpha$shape/pri.alpha$rate
987987
phi <- rgamma(N, shape = alpha , rate = alpha)
988+
linpred=X%*%beta
988989
989990
for (m in seq_len(burnin + M)){
990991
991992
# sample new data
992-
y <- rpois(N, phi*e * exp(X%*%beta))
993+
y <-rnbinom(N, alpha, mu=e* exp(linpred))
994+
#rpois(N, phi*e * exp(X%*%beta))
993995
994996
# Step a
995997
parms.proposal <- gen.proposal.poisson(y, X, e*phi, b0, B0)
@@ -1020,10 +1022,19 @@ negbin_check_abc <- function(X,e, b0,B0, pri.alpha,c.alpha,
10201022
}
10211023
```
10221024

1023-
We use a tighter prior for the regression effects.
1025+
We use a tighter prior for the regression effects and a sample size of N=200 observations.
10241026

10251027
```{r}
1026-
B0=diag(1,d)
1028+
N <- 200
1029+
X <- cbind(rep(1,N), rnorm(N), rnorm(N))
1030+
e <- rep(1,N)
1031+
d <- ncol(X)
1032+
1033+
b0 <- rep(0,d)
1034+
B0 <- diag(0.25,d)
1035+
1036+
pri.alpha <- data.frame(shape = 4, rate =2)
1037+
10271038
```
10281039

10291040
We run the sampler and investigate the draws of intercept and heterogeneity
@@ -1035,7 +1046,7 @@ if (pdfplots) {
10351046
}
10361047
par(mfrow = c(1, 2), mar = c(2.5, 2.5, 1.5, .1), mgp = c(1.5, .5, 0), lwd = 1.5)
10371048
1038-
set.seed(123)
1049+
set.seed(1234)
10391050
res_check_abc <- negbin_check_abc(X,e, b0,B0, pri.alpha,c.alpha,
10401051
full.gibbs = TRUE, M = M)
10411052
@@ -1048,7 +1059,7 @@ abline(a = 0, b = 1)
10481059
10491060
qqplot(alpha.prior,res_check_abc$alpha.post, xlab = "Prior",
10501061
ylab = "Posterior", main = "Heterogeneity parameter",
1051-
xlim = c(0, 30), ylim = c(0, 30))
1062+
xlim = c(0, 12), ylim = c(0, 12))
10521063
abline(a = 0, b = 1)
10531064
```
10541065

@@ -1080,7 +1091,8 @@ negbin_check_cba <- function(X,e, b0,B0,pri.alpha,c.alpha,
10801091
10811092
# sample new data
10821093
linpred <- X%*%beta
1083-
y <- rpois(N, phi*e * exp(linpred))
1094+
y <- rnbinom(N, alpha, mu=e* exp(X%*%beta))
1095+
#rpois(N, phi*e * exp(linpred))
10841096
10851097
# Step c
10861098
phi <- rgamma(N, shape = alpha + y, rate = alpha + e * exp(linpred))
@@ -1104,7 +1116,6 @@ negbin_check_cba <- function(X,e, b0,B0,pri.alpha,c.alpha,
11041116
alpha.post[m - burnin] <- alpha
11051117
acc.alpha[m - burnin] <- alpha.draw$acc
11061118
}
1107-
11081119
}
11091120
return(res = list(beta.post = beta.post, acc.beta = acc.beta,
11101121
alpha.post = alpha.post, acc.alpha = acc.alpha))
@@ -1120,7 +1131,7 @@ if (pdfplots) {
11201131
}
11211132
par(mfrow = c(1, 2), mar = c(2.5, 2.5, 1.5, .1), mgp = c(1.5, .5, 0), lwd = 1.5)
11221133
1123-
set.seed(123)
1134+
set.seed(1234)
11241135
res_check_cba <- negbin_check_cba(X,e, b0,B0,pri.alpha,c.alpha,
11251136
full.gibbs = TRUE, M=M)
11261137
@@ -1130,7 +1141,7 @@ abline(a = 0, b = 1)
11301141
11311142
qqplot(alpha.prior,res_check_cba$alpha.post, xlab = "Prior",
11321143
ylab = "Posterior", main = "Heterogeneity parameter",
1133-
xlim = c(0, 30), ylim = c(0, 30))
1144+
xlim = c(0, 12), ylim = c(0, 12))
11341145
abline(a = 0, b = 1)
11351146
```
11361147

@@ -1144,7 +1155,7 @@ if (pdfplots) {
11441155
}
11451156
par(mfrow = c(1, 2), mar = c(2.5, 2.5, 1.5, .1), mgp = c(1.5, .5, 0), lwd = 1.5)
11461157
1147-
set.seed(123)
1158+
set.seed(1234)
11481159
# order (a)-(b)-(c)
11491160
res_check_abc <- negbin_check_abc(X,e, b0,B0,pri.alpha,c.alpha,
11501161
full.gibbs = FALSE, M = M)
@@ -1155,7 +1166,7 @@ abline(a = 0, b = 1)
11551166
11561167
qqplot(alpha.prior,res_check_abc$alpha.post, xlab = "Prior",
11571168
ylab = "Posterior", main = "Heterogeneity parameter",
1158-
xlim = c(0, 30), ylim = c(0, 30))
1169+
xlim = c(0, 12), ylim = c(0, 12))
11591170
abline(a = 0, b = 1)
11601171
```
11611172

@@ -1167,7 +1178,7 @@ if (pdfplots) {
11671178
}
11681179
par(mfrow = c(1, 2), mar = c(2.5, 2.5, 1.5, .1), mgp = c(1.5, .5, 0), lwd = 1.5)
11691180
1170-
set.seed(123)
1181+
set.seed(1234)
11711182
# order (c)- (b)-(a)
11721183
res_check_cba <- negbin_check_cba(X,e, b0,B0,pri.alpha,c.alpha,
11731184
full.gibbs = FALSE,M = M)
@@ -1178,7 +1189,7 @@ abline(a = 0, b = 1)
11781189
11791190
qqplot(alpha.prior,res_check_cba$alpha.post, xlab = "Prior",
11801191
ylab = "Posterior", main = "Heterogeneity parameter",
1181-
xlim = c(0, 30), ylim = c(0, 30))
1192+
xlim = c(0, 12), ylim = c(0, 12))
11821193
abline(a = 0, b = 1)
11831194
```
11841195

0 commit comments

Comments
 (0)