@@ -1073,10 +1073,13 @@ by a unit root, we investigate the posterior of
10731073$1 - \phi_ {1} - \ldots - \phi_ {p}$ for $p = 1,\ldots,4$.
10741074
10751075``` r
1076+ toplot <- matrix (NA_real_ , ndraws , 4 )
1077+ for (p in 1 : 4 )
1078+ toplot [, p ] <- 1 - rowSums(ardat [[p ]]$ betas [, 2 : (p + 1 ), drop = FALSE ])
10761079for (p in 1 : 4 ) {
1077- hist(1 - rowSums( ardat [[ p ]] $ betas [, 2 : ( p + 1 ), drop = FALSE ]) , freq = FALSE ,
1078- main = paste0( " AR( " , p , " ) " ), xlab = expression( delta ), ylab = " " ,
1079- breaks = 16 )
1080+ hist(toplot [, p ] , freq = FALSE ,
1081+ breaks = seq(min( toplot ), max( toplot ), length.out = 20 ) ,
1082+ main = paste0( " AR( " , p , " ) " ), xlab = expression( delta ), ylab = " " )
10801083 abline(v = 0 , lty = 2 , col = 2 )
10811084}
10821085```
@@ -1095,10 +1098,12 @@ for (p in 1:4) {
10951098```
10961099
10971100``` r
1101+ for (p in 1 : 4 )
1102+ toplot [, p ] <- 1 - rowSums(ardat [[p ]]$ betas [, 2 : (p + 1 ), drop = FALSE ])
10981103for (p in 1 : 4 ) {
1099- hist(1 - rowSums( ardat [[ p ]] $ betas [, 2 : ( p + 1 ), drop = FALSE ]) , freq = FALSE ,
1100- main = paste0( " AR( " , p , " ) " ), xlab = expression( delta ), ylab = " " ,
1101- breaks = 16 )
1104+ hist(toplot [, p ] , freq = FALSE ,
1105+ breaks = seq(min( toplot ), max( toplot ), length.out = 20 ) ,
1106+ main = paste0( " AR( " , p , " ) " ), xlab = expression( delta ), ylab = " " )
11021107 abline(v = 0 , lty = 2 , col = 2 )
11031108}
11041109```
@@ -1168,7 +1173,7 @@ for (i in seq_along(cthetas)) {
11681173}
11691174```
11701175
1171- Now we plot the ACFs.
1176+ Now we plot traceplots and ACFs.
11721177
11731178``` r
11741179for (i in seq_along(cthetas )) {
@@ -1183,6 +1188,200 @@ for (i in seq_along(cthetas)) {
11831188
11841189![ ] ( Chapter07_files/figure-html/unnamed-chunk-46-1.png )
11851190
1191+ We repeat the exercise above, but now use a truncated Gaussian proposal
1192+ for the random walk MH algorithm.
1193+
1194+ ``` r
1195+ # standard deviation for random walk MH proposal
1196+ cthetas2 <- cthetas
1197+
1198+ # Allocate space for the draws
1199+ eps0s2 <- sigma2s2 <- thetas2 <- matrix (NA_real_ , ndraws , length(cthetas2 ))
1200+ naccepts2 <- rep(0L , length(cthetas2 ))
1201+
1202+ for (i in seq_along(cthetas2 )) {
1203+ # Set the starting values
1204+ theta <- 0.9
1205+ sigma2 <- var(dat ) / 2
1206+
1207+ # MCMC loop
1208+ for (m in seq_len(ndraws + nburn )) {
1209+ # Sample epsilon_0
1210+ bs <- (- theta )^ seq_along(dat )
1211+ as <- filter(dat , - theta , " recursive" )
1212+ tmp <- 1 + sum(bs ^ 2 )
1213+ eps0 <- rnorm(1 , - sum(as * bs ) / tmp , sqrt(sigma2 / tmp ))
1214+
1215+ # Sample sigma^2
1216+ eps <- filter(dat , - theta , " recursive" , init = eps0 )
1217+ cN <- c0 + (length(dat ) + 1 ) / 2
1218+ CN <- C0 + 0.5 * (eps0 ^ 2 + sum(eps ^ 2 ))
1219+ sigma2 <- rinvgamma(1 , cN , CN )
1220+
1221+ # Sample theta via RW-MH using inverse transform sampling
1222+ # for the truncated Gaussian
1223+ pL <- pnorm(- 1 , theta , cthetas2 [i ])
1224+ pU <- pnorm(1 , theta , cthetas2 [i ])
1225+ norm <- pU - pL
1226+ U <- runif(1 )
1227+ thetaprop <- qnorm(pL + U * norm , theta , cthetas2 [i ])
1228+ epsprop <- filter(dat , - thetaprop , " recursive" , init = eps0 )
1229+ normprop <- diff(pnorm(c(- 1 , 1 ), thetaprop , cthetas2 [i ]))
1230+
1231+ # Now we accept/reject. Note that because we have a uniform prior on
1232+ # (-1, 1) and, due to the truncated proposal, we can be sure that a
1233+ # value within (-1, 1) is proposed, we do not need to include the
1234+ # prior in the acceptance ratio. However, we need to cater for the
1235+ # asymmetry of the proposal!
1236+ logR <- - 0.5 / sigma2 * (sum(epsprop ^ 2 ) - sum(eps ^ 2 )) +
1237+ log(norm ) - log(normprop )
1238+ if (log(runif(1 )) < logR ) {
1239+ theta <- thetaprop
1240+ if (m > nburn ) naccepts2 [i ] <- naccepts2 [i ] + 1L
1241+ }
1242+
1243+ # Store the results
1244+ if (m > nburn ) {
1245+ eps0s2 [m - nburn , i ] <- eps0
1246+ sigma2s2 [m - nburn , i ] <- sigma2
1247+ thetas2 [m - nburn , i ] <- theta
1248+ }
1249+ }
1250+ }
1251+ ```
1252+
1253+ We again plot traceplots and ACFs.
1254+
1255+ ``` r
1256+ for (i in seq_along(cthetas2 )) {
1257+ ts.plot(thetas2 [, i ], ylim = range(thetas2 ), ylab = expression(theta ),
1258+ xlab = " Iterations" )
1259+ title(bquote(Traceplot ~ (c [theta ] == .(cthetas2 [i ]))))
1260+ acf(thetas2 [, i ], ylab = " " )
1261+ title(bquote(ACF ~ (acceptance == .(round(naccepts2 [i ] / ndraws , 3 ))* " ," ~
1262+ IF == .(round(ndraws / coda :: effectiveSize(thetas2 [, i ]), 1 )))))
1263+ }
1264+ ```
1265+
1266+ ![ ] ( Chapter07_files/figure-html/unnamed-chunk-48-1.png )
1267+
1268+ We repeat the exercise above once more, but now use a random walk
1269+ proposal on $\log(1 + \theta) - \log(1 - \theta)$.
1270+
1271+ ``` r
1272+ # Define the transformation and its inverse
1273+ trans <- function (theta ) log(1 + theta ) - log(1 - theta )
1274+ invtrans <- function (thetatrans ) (exp(thetatrans ) - 1 ) / (exp(thetatrans ) + 1 )
1275+
1276+ # standard deviation for random walk MH proposal
1277+ cthetas3 <- 100 * cthetas2
1278+
1279+ # Allocate space for the draws
1280+ eps0s3 <- sigma2s3 <- thetas3 <- matrix (NA_real_ , ndraws , length(cthetas3 ))
1281+ naccepts3 <- rep(0L , length(cthetas3 ))
1282+
1283+ for (i in seq_along(cthetas3 )) {
1284+ # Set the starting values
1285+ theta <- 0.9
1286+ sigma2 <- var(dat ) / 2
1287+
1288+ # MCMC loop
1289+ for (m in seq_len(ndraws + nburn )) {
1290+ # Sample epsilon_0
1291+ bs <- (- theta )^ seq_along(dat )
1292+ as <- filter(dat , - theta , " recursive" )
1293+ tmp <- 1 + sum(bs ^ 2 )
1294+ eps0 <- rnorm(1 , - sum(as * bs ) / tmp , sqrt(sigma2 / tmp ))
1295+
1296+ # Sample sigma^2
1297+ eps <- filter(dat , - theta , " recursive" , init = eps0 )
1298+ cN <- c0 + (length(dat ) + 1 ) / 2
1299+ CN <- C0 + 0.5 * (eps0 ^ 2 + sum(eps ^ 2 ))
1300+ sigma2 <- rinvgamma(1 , cN , CN )
1301+
1302+ # Sample theta via RW-MH on a trans(theta)
1303+ thetatransprop <- rnorm(1 , trans(theta ), cthetas3 [i ])
1304+ thetaprop <- invtrans(thetatransprop )
1305+ epsprop <- filter(dat , - thetaprop , " recursive" , init = eps0 )
1306+
1307+ # Now we accept/reject. Note that because we have a uniform prior on
1308+ # (-1, 1) and, due to the transformation, we can be sure that a
1309+ # value within (-1, 1) is proposed, we do not need to include the
1310+ # prior in the acceptance ratio. However, we need to cater for the
1311+ # asymmetry of the proposal!
1312+ logR <- - 0.5 / sigma2 * (sum(epsprop ^ 2 ) - sum(eps ^ 2 )) +
1313+ log(1 - thetaprop ^ 2 ) - log(1 - theta ^ 2 )
1314+ if (log(runif(1 )) < logR ) {
1315+ theta <- thetaprop
1316+ if (m > nburn ) naccepts3 [i ] <- naccepts3 [i ] + 1L
1317+ }
1318+
1319+ # Store the results
1320+ if (m > nburn ) {
1321+ eps0s3 [m - nburn , i ] <- eps0
1322+ sigma2s3 [m - nburn , i ] <- sigma2
1323+ thetas3 [m - nburn , i ] <- theta
1324+ }
1325+ }
1326+ }
1327+ ```
1328+
1329+ We again plot traceplots and ACFs.
1330+
1331+ ``` r
1332+ for (i in seq_along(cthetas3 )) {
1333+ ts.plot(thetas3 [, i ], ylim = range(thetas3 ), ylab = expression(theta ),
1334+ xlab = " Iterations" )
1335+ title(bquote(Traceplot ~ (c [theta ] == .(cthetas3 [i ]))))
1336+ acf(thetas3 [, i ], ylab = " " )
1337+ title(bquote(ACF ~ (acceptance == .(round(naccepts3 [i ] / ndraws , 3 ))* " ," ~
1338+ IF == .(round(ndraws / coda :: effectiveSize(thetas3 [, i ]), 1 )))))
1339+ }
1340+ ```
1341+
1342+ ![ ] ( Chapter07_files/figure-html/unnamed-chunk-50-1.png )
1343+
1344+ Let’s also check some QQ plots for equivalence.
1345+
1346+ ``` r
1347+ abline(c(0 , 1 ), col = 2 )
1348+ qqplot(thetas [, 2 ], thetas3 [, 2 ])
1349+ abline(c(0 , 1 ), col = 2 )
1350+ ```
1351+
1352+ ![ ] ( Chapter07_files/figure-html/unnamed-chunk-51-1.png )
1353+
1354+ Now, we compare acceptance rates and inefficiency factors for all 9
1355+ samplers.
1356+
1357+ ``` r
1358+ accepts <- matrix (c(naccepts , naccepts2 , naccepts3 ) / ndraws ,
1359+ nrow = length(naccepts ), ncol = 3 )
1360+ ESS <- matrix (coda :: effectiveSize(cbind(thetas , thetas2 , thetas3 )),
1361+ nrow = ncol(thetas ), ncol = 3 )
1362+ rownames(ESS ) <- rownames(accepts ) <- c(" tiny" , " medium" , " huge" )
1363+ colnames(ESS ) <- colnames(accepts ) <-
1364+ c(" Gaussian RW" , " truncated RW" , " transformed RW" )
1365+ IF <- ndraws / ESS
1366+ knitr :: kable(round(accepts , 2 ))
1367+ ```
1368+
1369+ | | Gaussian RW | truncated RW | transformed RW |
1370+ | :-------| ------------:| -------------:| ---------------:|
1371+ | tiny | 0.87 | 0.87 | 0.93 |
1372+ | medium | 0.31 | 0.35 | 0.52 |
1373+ | huge | 0.03 | 0.06 | 0.07 |
1374+
1375+ ``` r
1376+ knitr :: kable(round(IF , 1 ))
1377+ ```
1378+
1379+ | | Gaussian RW | truncated RW | transformed RW |
1380+ | :-------| ------------:| -------------:| ---------------:|
1381+ | tiny | 41.3 | 36.5 | 155.6 |
1382+ | medium | 5.4 | 5.2 | 4.7 |
1383+ | huge | 46.7 | 31.5 | 20.8 |
1384+
11861385## Section 7.4: Markov modeling for a panel of categorical time series
11871386
11881387### Example 7.13: Wage mobility data
@@ -1219,7 +1418,7 @@ for (i in index) {
12191418}
12201419```
12211420
1222- ![ ] ( Chapter07_files/figure-html/unnamed-chunk-49 -1.png )
1421+ ![ ] ( Chapter07_files/figure-html/unnamed-chunk-55 -1.png )
12231422
12241423### Example 7.14: Wage mobility data – comparing wage mobility of men and women
12251424
@@ -1311,7 +1510,7 @@ corrplot::corrplot(mean_xi_male, method = "square", is.corr = FALSE,
13111510 col = 1 , cl.pos = " n" )
13121511```
13131512
1314- ![ ] ( Chapter07_files/figure-html/unnamed-chunk-53 -1.png )
1513+ ![ ] ( Chapter07_files/figure-html/unnamed-chunk-59 -1.png )
13151514
13161515We compare the posterior densities of various transition probabilities
13171516$\xi_ {g,hk}$ for women and men.
@@ -1353,7 +1552,7 @@ legend("topright", col = 1, lty = 1:2,
13531552 legend = c(" female" , " male" ))
13541553```
13551554
1356- ![ ] ( Chapter07_files/figure-html/unnamed-chunk-54 -1.png )
1555+ ![ ] ( Chapter07_files/figure-html/unnamed-chunk-60 -1.png )
13571556
13581557### Example 7.15: Wage mobility data – long run
13591558
@@ -1378,7 +1577,7 @@ barplot(eta_hat_female_t, main = "Women", xlab = "Year", ylab = "Wage groups")
13781577barplot(eta_hat_male_t , main = " Men" , xlab = " Year" , ylab = " Wage groups" )
13791578```
13801579
1381- ![ ] ( Chapter07_files/figure-html/unnamed-chunk-55 -1.png )
1580+ ![ ] ( Chapter07_files/figure-html/unnamed-chunk-61 -1.png )
13821581
13831582We inspect the posterior distributions of $\eta_ {t,2}$ for wage category
138415832 (left-hand side) versus $\eta_ {t,5}$ for wage category 5 (right-hand
@@ -1413,4 +1612,4 @@ hist(eta_male_t[, 6], breaks = breaks,
14131612legend(" topright" , c(" female" , " male" ), fill = rgb(c(0 , 1 ), 0 , 0 , 0.2 ))
14141613```
14151614
1416- ![ ] ( Chapter07_files/figure-html/unnamed-chunk-56 -1.png )
1615+ ![ ] ( Chapter07_files/figure-html/unnamed-chunk-62 -1.png )
0 commit comments