# rhierLinearModel: Gibbs Sampler for Hierarchical Linear Model with Normal... In bayesm: Bayesian Inference for Marketing/Micro-Econometrics

## Description

`rhierLinearModel` implements a Gibbs Sampler for hierarchical linear models with a normal prior.

## Usage

 `1` ```rhierLinearModel(Data, Prior, Mcmc) ```

## Arguments

 `Data ` list(regdata, Z) `Prior` list(Deltabar, A, nu.e, ssq, nu, V) `Mcmc ` list(R, keep, nprint)

## Details

#### Model and Priors

`nreg` regression equations with nvar X variables in each equation
y_i = X_iβ_i + e_i with e_i ~ N(0, τ_i)

τ_i ~ nu.e*ssq_i/χ^2_{nu.e} where τ_i is the variance of e_i
β_i ~ N(ZΔ[i,], V_{β})
Note: ZΔ is the matrix Z * Δ and [i,] refers to ith row of this product

vec(Δ) given V_{β} ~ N(vec(Deltabar), V_{β}(x) A^{-1})
V_{β} ~ IW(nu,V)
Delta, Deltabar are nz x nvar; A is nz x nz; V_{β} is nvar x nvar.

Note: if you don't have any Z variables, omit Z in the `Data` argument and a vector of ones will be inserted; the matrix Δ will be 1 x nvar and should be interpreted as the mean of all unit βs.

#### Argument Details

`Data = list(regdata, Z)` [`Z` optional]

 `regdata: ` list of lists with X and y matrices for each of `nreg=length(regdata)` regressions `regdata[[i]]\$X: ` n_i x nvar design matrix for equation i `regdata[[i]]\$y: ` n_i x 1 vector of observations for equation i `Z: ` nreg x nz matrix of unit characteristics (def: vector of ones)

`Prior = list(Deltabar, A, nu.e, ssq, nu, V)` [optional]

 `Deltabar: ` nz x nvar matrix of prior means (def: 0) `A: ` nz x nz matrix for prior precision (def: 0.01I) `nu.e: ` d.f. parameter for regression error variance prior (def: 3) `ssq: ` scale parameter for regression error var prior (def: `var(y_i)`) `nu: ` d.f. parameter for Vbeta prior (def: nvar+3) `V: ` Scale location matrix for Vbeta prior (def: nu*I)

`Mcmc = list(R, keep, nprint)` [only `R` required]

 `R: ` number of MCMC draws `keep: ` MCMC thinning parm -- keep every `keep`th draw (def: 1) `nprint: ` print the estimated time remaining for every `nprint`'th draw (def: 100, set to 0 for no print)

## Value

A list containing:

 `betadraw ` nreg x nvar x R/keep array of individual regression coef draws `taudraw ` R/keep x nreg matrix of error variance draws `Deltadraw ` R/keep x nz*nvar matrix of Deltadraws `Vbetadraw ` R/keep x nvar*nvar matrix of Vbeta draws

## Author(s)

Peter Rossi, Anderson School, UCLA, [email protected].

## References

For further discussion, see Chapter 3, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
http://www.perossi.org/home/bsm-1

`rhierLinearMixture`

## Examples

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40``` ```if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) nreg = 100 nobs = 100 nvar = 3 Vbeta = matrix(c(1, 0.5, 0, 0.5, 2, 0.7, 0, 0.7, 1), ncol=3) Z = cbind(c(rep(1,nreg)), 3*runif(nreg)) Z[,2] = Z[,2] - mean(Z[,2]) nz = ncol(Z) Delta = matrix(c(1,-1,2,0,1,0), ncol=2) Delta = t(Delta) # first row of Delta is means of betas Beta = matrix(rnorm(nreg*nvar),nrow=nreg)%*%chol(Vbeta) + Z%*%Delta tau = 0.1 iota = c(rep(1,nobs)) regdata = NULL for (reg in 1:nreg) { X = cbind(iota, matrix(runif(nobs*(nvar-1)),ncol=(nvar-1))) y = X%*%Beta[reg,] + sqrt(tau)*rnorm(nobs) regdata[[reg]] = list(y=y, X=X) } Data1 = list(regdata=regdata, Z=Z) Mcmc1 = list(R=R, keep=1) out = rhierLinearModel(Data=Data1, Mcmc=Mcmc1) cat("Summary of Delta draws", fill=TRUE) summary(out\$Deltadraw, tvalues=as.vector(Delta)) cat("Summary of Vbeta draws", fill=TRUE) summary(out\$Vbetadraw, tvalues=as.vector(Vbeta[upper.tri(Vbeta,diag=TRUE)])) ## plotting examples if(0){ plot(out\$betadraw) plot(out\$Deltadraw) } ```

### Example output

```
Starting Gibbs Sampler for Linear Hierarchical Model
100  Regressions
2  Variables in Z (if 1, then only intercept)

Prior Parms:
Deltabar
[,1] [,2] [,3]
[1,]    0    0    0
[2,]    0    0    0
A
[,1] [,2]
[1,] 0.01 0.00
[2,] 0.00 0.01
nu.e (d.f. parm for regression error variances)=  3
Vbeta ~ IW(nu,V)
nu =  6
V
[,1] [,2] [,3]
[1,]    6    0    0
[2,]    0    6    0
[3,]    0    0    6

MCMC parms:
R=  10  keep=  1  nprint=  100

MCMC Iteration (est time to end - min)
Total Time Elapsed: 0.00
Summary of Delta draws
fewer than 100 draws submitted
Summary of Vbeta draws
fewer than 100 draws submitted
```

bayesm documentation built on Dec. 21, 2018, 9:04 a.m.