|
| 1 | +--- |
| 2 | +title: "Chapter 8: Bayesian Learning Beyond Standard Regression Analysis" |
| 3 | +output: rmarkdown::html_vignette |
| 4 | +vignette: > |
| 5 | + %\VignetteEncoding{UTF-8} |
| 6 | + %\VignetteIndexEntry{Chapter 8: Bayesian Learning Beyond Standard Regression Analysis} |
| 7 | + %\VignetteEngine{knitr::rmarkdown} |
| 8 | +editor_options: |
| 9 | + chunk_output_type: console |
| 10 | +--- |
| 11 | + |
| 12 | +```{r, include = FALSE} |
| 13 | +knitr::opts_chunk$set( |
| 14 | + collapse = TRUE, |
| 15 | + comment = "#>", |
| 16 | + fig.width = 8, |
| 17 | + fig.height = 6 |
| 18 | +) |
| 19 | +knitr::opts_knit$set(global.par = TRUE) |
| 20 | +pdfplots <- FALSE # default: FALSE; set this to TRUE only if you like pdf figures |
| 21 | +``` |
| 22 | + |
| 23 | +```{r, include = FALSE} |
| 24 | +par(mgp = c(1.6, .6, 0), mar = c(2.6, 2.6, 2.6, .4), lwd = 1) |
| 25 | +``` |
| 26 | + |
| 27 | +# Section 8.1 |
| 28 | +We illustrate probit regression analysis for the labor market data. |
| 29 | +```{r} |
| 30 | +library("BayesianLearningCode") |
| 31 | +data("labor", package = "BayesianLearningCode") |
| 32 | +``` |
| 33 | + |
| 34 | +We model the binary variable unemployment and use as covariates the variables female |
| 35 | +(binary), wcollar (binary) and age18 (quantitative, centered at 18 years). |
| 36 | + |
| 37 | +```{r} |
| 38 | +y <- labor$unemp98 |
| 39 | +N <- length(y) # number of observations |
| 40 | +
|
| 41 | +X <- cbind("intercept" = rep(1, N), "female"=labor$female, "wcollar"=labor$wcollar, |
| 42 | + "age18"=labor$age-18) # regressor matrix |
| 43 | +
|
| 44 | +d <- dim(X)[2] # number regression effects |
| 45 | +p <- d - 1 # number of regression effects without intercept |
| 46 | +``` |
| 47 | +We specify the prior on the regression effects as a rather flat multivariate |
| 48 | +Normal. |
| 49 | + |
| 50 | +```{r} |
| 51 | +# define prior parameters |
| 52 | +B0.inv <- diag(rep(1 / 10000, d), nrow = d) |
| 53 | +b0 <- rep(0, d) |
| 54 | +B0inv.b0 <- iB0%*%b0 |
| 55 | +``` |
| 56 | +The regression coefficients are estimated using data augmentation and Gibbs |
| 57 | +sampling. |
| 58 | +```{r} |
| 59 | +set.seed(1) |
| 60 | +
|
| 61 | +#define burnin and M |
| 62 | +burnin <- 1000 |
| 63 | +M <- 10000 |
| 64 | +
|
| 65 | +
|
| 66 | +# define quantities for the Gibbs sampler |
| 67 | +XX <- crossprod(X) |
| 68 | +BN <- solve(B0.inv + XX) |
| 69 | +
|
| 70 | +ind0=(y==0) # indicators for zeros and ones |
| 71 | +ind1=(y==1) |
| 72 | +
|
| 73 | +# starting values |
| 74 | +beta <- rep(0,k) |
| 75 | +z <- rep(NA_real_,n) |
| 76 | +
|
| 77 | +
|
| 78 | +for (m in 1:(burnin + M)) { |
| 79 | + |
| 80 | + # Draw z conditional on y and beta |
| 81 | + u<- runif(N) |
| 82 | + eta <- X %*% beta |
| 83 | + pi<- pnorm(eta) |
| 84 | + |
| 85 | + z[ind0] <- eta[ind0] + qnorm(u[ind0]*(1-pi[ind0])) |
| 86 | + z[ind1] <- eta[ind1] + qnorm (1-u[ind1]*pi[ind1]) |
| 87 | + |
| 88 | + # sample beta from the full conditional |
| 89 | + bN <- BN %*% (B0inv.b0 + t(X) %*% z) |
| 90 | + beta <- t(mvtnorm::rmvnorm(1, mean = bN, sigma = BN)) |
| 91 | + |
| 92 | + # Store the beta draws |
| 93 | + betas[m, ] <- beta |
| 94 | + |
| 95 | +} |
| 96 | +``` |
| 97 | + |
0 commit comments