rhierLinearModel {rpud} | R Documentation |
rhierLinearModel
implements a Gibbs Sampler for hierarchical linear models with a normal prior.
rhierLinearModel(Data, Prior, Mcmc, output)
Data |
list(regdata,Z) (Z optional). |
Prior |
list(Deltabar,A,nu.e,ssq,nu,V) (optional). |
Mcmc |
list(R,keep,chains) (R required). |
output |
binary output format. |
Model: length(regdata) regression equations.
y_i = X_ibeta_i + e_i
. e_i
\sim
N(0,tau_i)
. nvar X vars in each equation.
Priors:
tau_i
\sim
nu.e*ssq_i/\chi^2_{nu.e}
. tau_i
is the variance of e_i
.
beta_i
\sim
N(ZDelta[i,],V_{beta}
).
Note: ZDelta is the matrix Z * Delta; [i,] refers to ith row of this product.
vec(Delta)
given V_{beta}
\sim
N(vec(Deltabar),V_{beta} (x) A^{-1})
.
V_{beta}
\sim
IW(nu,V)
.
Delta, Deltabar
are nz x nvar. A
is nz x nz. V_{beta}
is nvar x nvar.
Note: The default of Z is iota(nreg x 1).
List arguments contain:
regdata
list of lists with X,y matrices for each of length(regdata) regressions
regdata[[i]]$X
X matrix for equation i
regdata[[i]]$y
y vector for equation i
Deltabar
nz x nvar matrix of prior means (def: 0)
A
nz x nz matrix for prior precision (def: .01I)
nu.e
d.f. parm for regression error variance prior (def: 3)
ssq
scale parm for regression error var prior (def: var(y_i
))
nu
d.f. parm for Vbeta prior (def: nvar+3)
V
Scale location matrix for Vbeta prior (def: nu*I)
R
number of MCMC draws
keep
MCMC thinning parm: keep every keepth draw (def: 1)
chains
number of MCMC simulation chains for CODA
diagnostics
output
supported binary output format:
default
, bayesm
, or coda
.
For default
format: a list containing
betadraw |
R/keep x nreg x nvar array of individual regression coef draws |
taudraw |
R/keep x nreg array of error variance draws |
Deltadraw |
R/keep x nz x nvar array of Deltadraws |
Vbetadraw |
R/keep x nvar*nvar array of Vbeta draws |
For bayesm
format: a list containing
betadraw |
nreg x nvar x R/keep array of individual regression coef draws |
taudraw |
R/keep x nreg array of error variance draws |
Deltadraw |
R/keep x nz x nvar array of Deltadraws |
Vbetadraw |
R/keep x nvar*nvar array of Vbeta draws |
For CODA
output format:
A list of CODA compatible simulation chains in default format.
Only the upper triangle of each VbetaDraw sample is kept for coda diagnostics.
Chi Yau (based on R doc of rhierLinearModel
in bayesm
by Peter Rossi)
chi.yau@r-tutor.com
Rossi, Allenby and McCulloch:
Bayesian Statistics and Marketing Ch 3
http://www.perossi.org/home/bsm-1
## Not run:
library(rpud)
data.path <- file.path(path.package(package="rpud"), "runit/data")
#################################################
# sample data
Z <- read.table(
file.path(data.path, "rhierlm-n100-z.txt"),
header=FALSE)
Z <- as.matrix(Z)
data <- read.table(
file.path(data.path, "rhierlm-n100-data.txt"),
header=FALSE)
data <- as.matrix(data)
regkey <- unique(data[,1])
colvar <- 3:ncol(data)
regdata <- NULL
for (reg in regkey) {
filter <- (data[,1]==reg)
y <- data[filter,2]
X <- data[filter,colvar]
regdata[[reg]] <- list(y=y,X=X)
}
# parameters
R <- 1000
keep <- 1
Data <- list(regdata=regdata,Z=Z)
Mcmc <- list(R=R,keep=keep)
set.seed(66)
out<-rpud::rhierLinearModel(Data=Data,Mcmc=Mcmc,output="bayesm")
library(bayesm)
cat("\n*** Summary of Delta Draws ***",fill=TRUE)
summary(out$Deltadraw)
cat("\n*** Summary of Vbeta Draws ***",fill=TRUE)
summary(out$Vbetadraw)
## End(Not run)