Skip to content

Commit d32fb62

Browse files
committed
Changes in reporting results
1 parent f81f58b commit d32fb62

1 file changed

Lines changed: 33 additions & 35 deletions

File tree

vignettes/Chapter08.Rmd

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,20 @@ To compute summary statistics from the posterior we use the following function.
112112

113113
```{r}
114114
res.mcmc <- function(x, lower = 0.025, upper = 0.975) {
115-
y <- c(quantile(x, lower), mean(x), quantile(x, upper))
115+
y <- cbind(quantile(x, lower), mean(x), quantile(x, upper))
116116
names(y) <- c(paste0(lower * 100, "%"), "Mean", paste0(upper * 100, "%"))
117117
y
118118
}
119119
```
120120

121-
122121
```{r}
123122
res_beta <- apply(betas, 2, res.mcmc)
124-
knitr::kable(round(res_beta, 3))
123+
knitr::kable(round(t(res_beta), 3))
125124
126125
(p_unemploy_base <- pnorm(res_beta[2, 1]))
127126
```
128-
The estimated risk of unemployment for a baseline person is low and it
129-
is even lower for a white collar worker. It is higher for females,
130-
older persons and particularly for those unemployed in 1997.
127+
128+
The estimated risk of unemployment for a baseline person is low and it is even lower for a white collar worker. It is higher for females, older persons and particularly for those unemployed in 1997.
131129

132130
```{r, echo = -c(1:2)}
133131
if (pdfplots) {
@@ -140,8 +138,7 @@ for (j in seq_len(ncol(betas))) {
140138
}
141139
```
142140

143-
A plot of the autocorrelation of the draws shows that although there is some
144-
autocorrelation, it vanishes after a few lags.
141+
A plot of the autocorrelation of the draws shows that although there is some autocorrelation, it vanishes after a few lags.
145142

146143
```{r, echo = -c(1:2)}
147144
if (pdfplots) {
@@ -152,12 +149,13 @@ for (j in seq_len(ncol(betas))) {
152149
acf(betas[, j], main = "", xlab = colnames(betas)[j], ylab = "")
153150
}
154151
```
155-
The sampler is easy to implement, however there might be problems when the
156-
response variable contains either only few or very many successes.
152+
The sampler is easy to implement, however there might be problems when the response variable contains either only few or very many successes.
157153
To illustrate this issue, we use data where in $N = 500$ trials only 1 success
158154
or only 1 failure is observed.
159155

160156
```{r}
157+
set.seed(1234)
158+
161159
N <- 500
162160
X <- matrix(1, nrow = N)
163161
@@ -168,7 +166,8 @@ y2 <- c(rep(0, N-1), 1)
168166
betas2 <- probit(y2, X, b0 = 0, B0 = 10000)
169167
```
170168

171-
In both cases the autocorrelation of the draws decreases very slowly.
169+
In both cases the autocorrelation of the draws decreases very slowly and remains high even higher lags.
170+
172171
```{r, echo = -c(1:2)}
173172
if (pdfplots) {
174173
pdf("8-1_3.pdf", width = 8, height = 5)
@@ -180,13 +179,9 @@ plot(betas2, type = "l", main = "", xlab = "", ylab = "")
180179
acf(betas2)
181180
```
182181

183-
High autocorrelated draws in probit models not only occur if successes
184-
or failures are very rare, but also when a covariate (or a linear
185-
combination of covariates) perfectly allows to predict successes
182+
High autocorrelation in MCMC draws for probit models not only occur if successesor failures are very rare, but also when a covariate (or a linear combination of covariates) perfectly allows to predict successes
186183
and/or failures. Complete separation means that both successes and
187-
failures can be perfectly predicted by a covariate, whereas with
188-
quasi-complete separation only either successes or failures can be
189-
predicted perfectly.
184+
failures can be perfectly predicted by a covariate, whereas quasi-complete separation means that either successes or failures can be predicted perfectly.
190185

191186
# Example 8.3
192187

@@ -202,14 +197,15 @@ y <- rep(c(0,1), c(ns, N - ns))
202197
table(x,y)
203198
```
204199

205-
We estimate the model parameters and plot the ACF of the draws. Again the
206-
autocorrelations remain high even for lag 35.
200+
We estimate the model parameters and plot the ACF of the draws. Again the autocorrelations remain high even for lag 35.
207201

208202
```{r, echo = -c(1:2)}
209203
if (pdfplots) {
210204
pdf("8-1_4.pdf", width = 8, height = 5)
211205
}
212206
par(mfrow = c(2, 2), mar = c(2.5, 1.5, 1.5, .1), mgp = c(1.5, .5, 0), lwd = 1.5)
207+
208+
set.seed(1234)
213209
X <- cbind(rep(1, N), x)
214210
betas <- probit(y, X, b0 = 0, B0 = 10000)
215211
@@ -220,7 +216,8 @@ acf(betas[, 2])
220216
```
221217
# Example 8.4
222218

223-
To illustrate quasi-seperation we use the responses as in Example 8.3., but now $x=0$ for all successes and additionally for 100 failures.
219+
To illustrate quasi-seperation we use the responses as in Example 8.3., but now $x=1$ for all successes and additionally for 100 failures. Hence for $x=1$ always a success is observed, whereas for $x=0$ both successes and failures occur,
220+
224221
```{r}
225222
x <- rep(c(0,1), c(ns-100, N - ns+100))
226223
table(x, y)
@@ -234,6 +231,8 @@ if (pdfplots) {
234231
}
235232
236233
par(mfrow = c(2, 2), mar = c(2.5, 1.5, 1.5, .1), mgp = c(1.5, .5, 0), lwd = 1.5)
234+
235+
set.seed(1234)
237236
X <- cbind(rep(1, N), x)
238237
betas <- probit(y, X, b0 = 0, B0 = 10000)
239238
@@ -243,36 +242,34 @@ plot(betas[, 2], type = "l", main = "", xlab = "", ylab = "")
243242
acf(betas[, 2])
244243
```
245244

246-
If we change the setting so that x takes values of $0$ not only for failures but also for some successes, the
245+
If we change the setting so that x takes values of $1$ not only for failures but also for some successes, the
247246
autocorrelations are low for the intercept but still high for the covariate effect.
247+
248248
```{r, echo = -c(1:2)}
249249
if (pdfplots) {
250250
pdf("8-1_5a.pdf", width = 8, height = 5)
251251
}
252+
252253
x <- rep(c(0,1), c(ns+100, N - ns-100))
253254
table(x, y)
254255
255-
par(mfrow = c(2, 2), mar = c(2.5, 1.5, 1.5, .1), mgp = c(1.5, .5, 0), lwd = 1.5)
256+
set.seed(1234)
256257
X <- cbind(rep(1, N), x)
257258
betas <- probit(y, X, b0 = 0, B0 = 10000)
258259
260+
par(mfrow = c(2, 2), mar = c(2.5, 1.5, 1.5, .1), mgp = c(1.5, .5, 0), lwd = 1.5)
259261
plot(betas[, 1], type = "l", main = "", xlab = "", ylab = "")
260262
acf(betas[, 1])
261263
plot(betas[, 2], type = "l", main = "", xlab = "", ylab = "")
262264
acf(betas[, 2])
263265
```
264266

265-
266-
High autocorrelations typically indicate problems with the sampler. If
267-
there is complete or quasi-complete separation in the data, the
268-
likelihood is monotone and the maximum likelihood estiamte does not
269-
exist. In a Bayesian approach using a flat, improper prior on the
267+
High autocorrelations typically indicate problems with the sampler. If there is complete or quasi-complete separation in the data, the likelihood is monotone and the maximum likelihood estimate does not exist. In a Bayesian approach using a flat, improper prior on the
270268
regression effects will result in an improper posterior
271269
distribution. Hence, a proper prior is required to avoid improper
272270
posteriors in case of separation.
273271

274-
In the examples above we used a proper prior which is rather
275-
flat. With a more informative prior, the autocorrelations of the draws
272+
In the examples above we used a very flat but proper prior With a more informative prior, the autocorrelations of the draws
276273
are lower. This can be seen in the next figure, where the simulated
277274
data under quasi-separation are re-analyzed with a Normal prior that
278275
is tighter around zero.
@@ -281,9 +278,11 @@ is tighter around zero.
281278
if (pdfplots) {
282279
pdf("8-1_6.pdf", width = 8, height = 5)
283280
}
284-
par(mfrow = c(2, 2), mar = c(2.5, 1.5, 1.5, .1), mgp = c(1.5, .5, 0), lwd = 1.5)
281+
set.seed(1234)
285282
betas <- probit(y, X, b0 = 0, B0 = 2.5^2)
286283
284+
par(mfrow = c(2, 2), mar = c(2.5, 1.5, 1.5, .1), mgp = c(1.5, .5, 0), lwd = 1.5)
285+
287286
plot(betas[, 1], type = "l", main = "", xlab = "", ylab = "")
288287
acf(betas[, 1])
289288
plot(betas[, 2], type = "l", main = "", xlab = "", ylab = "")
@@ -668,18 +667,17 @@ pri.alpha <- data.frame(shape = 2, rate = 0.5)
668667
res1 <- negbin(y, X, e, qmean = parms.proposal$mean, qvar = parms.proposal$var,
669668
pri.alpha = pri.alpha, full.gibbs = TRUE)
670669
671-
res.negbin.full <- cbind(apply(res1$beta.post, 2, res.mcmc),
670+
res.negbin.full <- rbind(t(apply(res1$beta.post, 2, res.mcmc)),
672671
res.mcmc(res1$alpha.post))
673-
colnames(res.negbin.full)[4] <- "alpha"
672+
rownames(res.negbin.full)[4] <- "alpha"
674673
knitr::kable(round(res.negbin.full, 3))
675674
676675
677676
res2 <- negbin(y, X, e, qmean = parms.proposal$mean, qvar = parms.proposal$var,
678677
pri.alpha = pri.alpha, full.gibbs = FALSE)
679678
680-
res.negbin.partial <- cbind(apply(res2$beta.post, 2, res.mcmc),
681-
res.mcmc(res2$alpha.post))
682-
colnames(res.negbin.partial)[4] <- "alpha"
679+
res.negbin.partial <- rbind(t(apply(res2$beta.post, 2, res.mcmc)), res.mcmc(res2$alpha.post))
680+
rownames(res.negbin.partial)[4] <- "alpha"
683681
knitr::kable(round(res.negbin.partial, 3))
684682
```
685683
As expected estimation results using both samplers are rather similar.

0 commit comments

Comments
 (0)