Title: | Bayesian Inference for Marketing/Micro-Econometrics |
---|---|
Description: | Covers many important models used in marketing and micro-econometrics applications. The package includes: Bayes Regression (univariate or multivariate dep var), Bayes Seemingly Unrelated Regression (SUR), Binary and Ordinal Probit, Multinomial Logit (MNL) and Multinomial Probit (MNP), Multivariate Probit, Negative Binomial (Poisson) Regression, Multivariate Mixtures of Normals (including clustering), Dirichlet Process Prior Density Estimation with normal base, Hierarchical Linear Models with normal prior and covariates, Hierarchical Linear Models with a mixture of normals prior and covariates, Hierarchical Multinomial Logits with a mixture of normals prior and covariates, Hierarchical Multinomial Logits with a Dirichlet Process prior and covariates, Hierarchical Negative Binomial Regression Models, Bayesian analysis of choice-based conjoint data, Bayesian treatment of linear instrumental variables models, Analysis of Multivariate Ordinal survey data with scale usage heterogeneity (as in Rossi et al, JASA (01)), Bayesian Analysis of Aggregate Random Coefficient Logit Models as in BLP (see Jiang, Manchanda, Rossi 2009) For further reference, consult our book, Bayesian Statistics and Marketing by Rossi, Allenby and McCulloch (Wiley first edition 2005 and second forthcoming) and Bayesian Non- and Semi-Parametric Methods and Applications (Princeton U Press 2014). |
Authors: | Peter Rossi <[email protected]> |
Maintainer: | Peter Rossi <[email protected]> |
License: | GPL (>= 2) |
Version: | 3.1-6 |
Built: | 2024-12-17 07:05:12 UTC |
Source: | CRAN |
A panel dataset from a conjoint experiment in which two partial profiles of credit cards were presented to 946 respondents from a regional bank wanting to offer credit cards to customers outside of its normal operating region. Each respondent was presented with between 13 and 17 paired comparisons. The bank and attribute levels are disguised to protect the proprietary interests of the cooperating firm.
data(bank)
data(bank)
The bank
object is a list containing two data frames. The first, choiceAtt
, provides choice attributes for the partial credit card profiles. The second, demo
, provides demographic information on the respondents.
In the choiceAtt
data frame:
...$id |
respondent id |
...$choice |
profile chosen |
...$Med_FInt |
medium fixed interest rate |
...$Low_FInt |
low fixed interest rate |
...$Med_VInt |
variable interest rate |
...$Rewrd_2 |
reward level 2 |
...$Rewrd_3 |
reward level 3 |
...$Rewrd_4 |
reward level 4 |
...$Med_Fee |
medium annual fee level |
...$Low_Fee |
low annual fee level |
...$Bank_B |
bank offering the credit card |
...$Out_State |
location of the bank offering the credit card |
...$Med_Rebate |
medium rebate level |
...$High_Rebate |
high rebate level |
...$High_CredLine |
high credit line level |
...$Long_Grace |
grace period |
The profiles are coded as the difference in attribute levels. Thus, that a "-1" means the profile coded as a choice of "0" has the attribute. A value of 0 means that the attribute was not present in the comparison.
In the demo
data frame:
...$id |
respondent id |
...$age |
respondent age in years |
...$income |
respondent income category |
...$gender |
female=1 |
Allenby, Gregg and James Ginter (1995), "Using Extremes to Design Products and Segment Markets," Journal of Marketing Research, 392–403.
Appendix A, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
data(bank) cat(" table of Binary Dep Var", fill=TRUE) print(table(bank$choiceAtt[,2])) cat(" table of Attribute Variables", fill=TRUE) mat = apply(as.matrix(bank$choiceAtt[,3:16]), 2, table) print(mat) cat(" means of Demographic Variables", fill=TRUE) mat=apply(as.matrix(bank$demo[,2:3]), 2, mean) print(mat) ## example of processing for use with rhierBinLogit if(0) { choiceAtt = bank$choiceAtt Z = bank$demo ## center demo data so that mean of random-effects ## distribution can be interpreted as the average respondent Z[,1] = rep(1,nrow(Z)) Z[,2] = Z[,2] - mean(Z[,2]) Z[,3] = Z[,3] - mean(Z[,3]) Z[,4] = Z[,4] - mean(Z[,4]) Z = as.matrix(Z) hh = levels(factor(choiceAtt$id)) nhh = length(hh) lgtdata = NULL for (i in 1:nhh) { y = choiceAtt[choiceAtt[,1]==hh[i], 2] nobs = length(y) X = as.matrix(choiceAtt[choiceAtt[,1]==hh[i], c(3:16)]) lgtdata[[i]] = list(y=y, X=X) } cat("Finished Reading data", fill=TRUE) Data = list(lgtdata=lgtdata, Z=Z) Mcmc = list(R=10000, sbeta=0.2, keep=20) set.seed(66) out = rhierBinLogit(Data=Data, Mcmc=Mcmc) begin = 5000/20 summary(out$Deltadraw, burnin=begin) summary(out$Vbetadraw, burnin=begin) ## plotting examples if(0){ ## plot grand means of random effects distribution (first row of Delta) index = 4*c(0:13)+1 matplot(out$Deltadraw[,index], type="l", xlab="Iterations/20", ylab="", main="Average Respondent Part-Worths") ## plot hierarchical coefs plot(out$betadraw) ## plot log-likelihood plot(out$llike, type="l", xlab="Iterations/20", ylab="", main="Log Likelihood") } }
data(bank) cat(" table of Binary Dep Var", fill=TRUE) print(table(bank$choiceAtt[,2])) cat(" table of Attribute Variables", fill=TRUE) mat = apply(as.matrix(bank$choiceAtt[,3:16]), 2, table) print(mat) cat(" means of Demographic Variables", fill=TRUE) mat=apply(as.matrix(bank$demo[,2:3]), 2, mean) print(mat) ## example of processing for use with rhierBinLogit if(0) { choiceAtt = bank$choiceAtt Z = bank$demo ## center demo data so that mean of random-effects ## distribution can be interpreted as the average respondent Z[,1] = rep(1,nrow(Z)) Z[,2] = Z[,2] - mean(Z[,2]) Z[,3] = Z[,3] - mean(Z[,3]) Z[,4] = Z[,4] - mean(Z[,4]) Z = as.matrix(Z) hh = levels(factor(choiceAtt$id)) nhh = length(hh) lgtdata = NULL for (i in 1:nhh) { y = choiceAtt[choiceAtt[,1]==hh[i], 2] nobs = length(y) X = as.matrix(choiceAtt[choiceAtt[,1]==hh[i], c(3:16)]) lgtdata[[i]] = list(y=y, X=X) } cat("Finished Reading data", fill=TRUE) Data = list(lgtdata=lgtdata, Z=Z) Mcmc = list(R=10000, sbeta=0.2, keep=20) set.seed(66) out = rhierBinLogit(Data=Data, Mcmc=Mcmc) begin = 5000/20 summary(out$Deltadraw, burnin=begin) summary(out$Vbetadraw, burnin=begin) ## plotting examples if(0){ ## plot grand means of random effects distribution (first row of Delta) index = 4*c(0:13)+1 matplot(out$Deltadraw[,index], type="l", xlab="Iterations/20", ylab="", main="Average Respondent Part-Worths") ## plot hierarchical coefs plot(out$betadraw) ## plot log-likelihood plot(out$llike, type="l", xlab="Iterations/20", ylab="", main="Log Likelihood") } }
breg
makes one draw from the posterior of a univariate regression
(scalar dependent variable) given the error variance = 1.0.
A natural conjugate (normal) prior is used.
breg(y, X, betabar, A)
breg(y, X, betabar, A)
y |
|
X |
|
betabar |
|
A |
|
model: with
.
prior:
.
vector containing a draw from the posterior distribution
This routine is a utility routine that does not check theinput arguments for proper dimensions and type. In particular, X
must be a matrix. If you have a vector for X
, coerce itinto a matrix with one column.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=1000} else {R=10} ## simulate data set.seed(66) n = 100 X = cbind(rep(1,n), runif(n)); beta = c(1,2) y = X %*% beta + rnorm(n) ## set prior betabar = c(0,0) A = diag(c(0.05, 0.05)) ## make draws from posterior betadraw = matrix(double(R*2), ncol = 2) for (rep in 1:R) {betadraw[rep,] = breg(y,X,betabar,A)} ## summarize draws mat = apply(betadraw, 2, quantile, probs=c(0.01, 0.05, 0.50, 0.95, 0.99)) mat = rbind(beta,mat); rownames(mat)[1] = "beta" print(mat)
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=1000} else {R=10} ## simulate data set.seed(66) n = 100 X = cbind(rep(1,n), runif(n)); beta = c(1,2) y = X %*% beta + rnorm(n) ## set prior betabar = c(0,0) A = diag(c(0.05, 0.05)) ## make draws from posterior betadraw = matrix(double(R*2), ncol = 2) for (rep in 1:R) {betadraw[rep,] = breg(y,X,betabar,A)} ## summarize draws mat = apply(betadraw, 2, quantile, probs=c(0.01, 0.05, 0.50, 0.95, 0.99)) mat = rbind(beta,mat); rownames(mat)[1] = "beta" print(mat)
Panel dataset from a conjoint survey for digital cameras with 332 respondents. Data exclude respondents that always answered none, always picked the same brand, always selected the highest priced offering, or who appeared to be answering randomly.
data(camera)
data(camera)
A list of lists. Each inner list corresponds to one survey respondent and contains a numeric vector (y
) of choice indicators and a numeric matrix (X
) of covariates. Each respondent participated in 16 choice scenarios each including 4 camera options (and an outside option) for a total of 80 rows per respondent.
The covariates included in each X
matrix are:
...$canon |
an indicator for brand Canon |
...$sony |
an indicator for brand Sony |
...$nikon |
an indicator for brand Nikon |
...$panasonic |
an indicator for brand Panasonic |
...$pixels |
an indicator for a higher pixel count |
...$zoom |
an indicator for a higher level of zoom |
...$video |
an indicator for the ability to capture video |
...$swivel |
an indicator for a swivel video display |
...$wifi |
an indicator for wifi capability |
...$price |
in hundreds of U.S. dollars |
Allenby, Greg, Jeff Brazell, John Howell, and Peter Rossi (2014), "Economic Valuation of Product Features," Quantitative Marketing and Economics 12, 421–456.
Allenby, Greg, Jeff Brazell, John Howell, and Peter Rossi (2014), "Valuation of Patented Product Features," Journal of Law and Economics 57, 629–663.
For analysis of a similar dataset, see Case Study 4, Bayesian Statistics and Marketing Rossi, Allenby, and McCulloch.
cgetC
obtains a list of censoring points, or cut-offs, used in the ordinal multivariate probit model of Rossi et al (2001). This approach uses a quadratic parameterization of the cut-offs. The model is useful for modeling correlated ordinal data on a scale from to
with different scale usage patterns.
cgetC(e, k)
cgetC(e, k)
e |
quadratic parameter ( |
k |
items are on a scale from |
A vector of cut-offs.
This is a utility function which implements no error-checking.
Rob McCulloch and Peter Rossi, Anderson School, UCLA. [email protected].
Rossi et al (2001), “Overcoming Scale Usage Heterogeneity,” JASA 96, 20–31.
cgetC(0.1, 10)
cgetC(0.1, 10)
Panel data with sales volume for a package of Borden Sliced Cheese as well as a measure of display activity and price. Weekly data aggregated to the "key" account or retailer/market level.
data(cheese)
data(cheese)
A data frame with 5555 observations on the following 4 variables:
...$RETAILER |
a list of 88 retailers |
...$VOLUME |
unit sales |
...$DISP |
percent ACV on display (a measure of advertising display activity) |
...$PRICE |
in U.S. dollars |
Boatwright, Peter, Robert McCulloch, and Peter Rossi (1999), "Account-Level Modeling for Trade Promotion," Journal of the American Statistical Association 94, 1063–1073.
Chapter 3, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
data(cheese) cat(" Quantiles of the Variables ",fill=TRUE) mat = apply(as.matrix(cheese[,2:4]), 2, quantile) print(mat) ## example of processing for use with rhierLinearModel if(0) { retailer = levels(cheese$RETAILER) nreg = length(retailer) nvar = 3 regdata = NULL for (reg in 1:nreg) { y = log(cheese$VOLUME[cheese$RETAILER==retailer[reg]]) iota = c(rep(1,length(y))) X = cbind(iota, cheese$DISP[cheese$RETAILER==retailer[reg]], log(cheese$PRICE[cheese$RETAILER==retailer[reg]])) regdata[[reg]] = list(y=y, X=X) } Z = matrix(c(rep(1,nreg)), ncol=1) nz = ncol(Z) ## run each individual regression and store results lscoef = matrix(double(nreg*nvar), ncol=nvar) for (reg in 1:nreg) { coef = lsfit(regdata[[reg]]$X, regdata[[reg]]$y, intercept=FALSE)$coef if (var(regdata[[reg]]$X[,2])==0) { lscoef[reg,1]=coef[1] lscoef[reg,3]=coef[2] } else {lscoef[reg,]=coef} } R = 2000 Data = list(regdata=regdata, Z=Z) Mcmc = list(R=R, keep=1) set.seed(66) out = rhierLinearModel(Data=Data, Mcmc=Mcmc) cat("Summary of Delta Draws", fill=TRUE) summary(out$Deltadraw) cat("Summary of Vbeta Draws", fill=TRUE) summary(out$Vbetadraw) # plot hier coefs if(0) {plot(out$betadraw)} }
data(cheese) cat(" Quantiles of the Variables ",fill=TRUE) mat = apply(as.matrix(cheese[,2:4]), 2, quantile) print(mat) ## example of processing for use with rhierLinearModel if(0) { retailer = levels(cheese$RETAILER) nreg = length(retailer) nvar = 3 regdata = NULL for (reg in 1:nreg) { y = log(cheese$VOLUME[cheese$RETAILER==retailer[reg]]) iota = c(rep(1,length(y))) X = cbind(iota, cheese$DISP[cheese$RETAILER==retailer[reg]], log(cheese$PRICE[cheese$RETAILER==retailer[reg]])) regdata[[reg]] = list(y=y, X=X) } Z = matrix(c(rep(1,nreg)), ncol=1) nz = ncol(Z) ## run each individual regression and store results lscoef = matrix(double(nreg*nvar), ncol=nvar) for (reg in 1:nreg) { coef = lsfit(regdata[[reg]]$X, regdata[[reg]]$y, intercept=FALSE)$coef if (var(regdata[[reg]]$X[,2])==0) { lscoef[reg,1]=coef[1] lscoef[reg,3]=coef[2] } else {lscoef[reg,]=coef} } R = 2000 Data = list(regdata=regdata, Z=Z) Mcmc = list(R=R, keep=1) set.seed(66) out = rhierLinearModel(Data=Data, Mcmc=Mcmc) cat("Summary of Delta Draws", fill=TRUE) summary(out$Deltadraw) cat("Summary of Vbeta Draws", fill=TRUE) summary(out$Vbetadraw) # plot hier coefs if(0) {plot(out$betadraw)} }
clusterMix
uses MCMC draws of indicator variables from a normal component mixture model to cluster observations based on a similarity matrix.
clusterMix(zdraw, cutoff=0.9, SILENT=FALSE, nprint=BayesmConstant.nprint)
clusterMix(zdraw, cutoff=0.9, SILENT=FALSE, nprint=BayesmConstant.nprint)
zdraw |
|
cutoff |
cutoff probability for similarity (def: |
SILENT |
logical flag for silent operation (def: |
nprint |
print every nprint'th draw (def: |
Define a similarity matrix, with
Sim[i,j]=1
if observations and
are in same component. Compute the posterior mean of Sim over indicator draws.
Clustering is achieved by two means:
Method A:
Find the indicator draw whose similarity matrix minimizes ,
where loss is absolute deviation.
Method B:
Define a Similarity matrix by setting any element of if
.
Compute the clustering scheme associated with this "windsorized" Similarity matrix.
A list containing:
clustera: |
indicator function for clustering based on method A above |
clusterb: |
indicator function for clustering based on method B above |
This routine is a utility routine that does not check the input arguments for proper dimensions and type.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch Chapter 3.
if(nchar(Sys.getenv("LONG_TEST")) != 0) { ## simulate data from mixture of normals n = 500 pvec = c(.5,.5) mu1 = c(2,2) mu2 = c(-2,-2) Sigma1 = matrix(c(1,0.5,0.5,1), ncol=2) Sigma2 = matrix(c(1,0.5,0.5,1), ncol=2) comps = NULL comps[[1]] = list(mu1, backsolve(chol(Sigma1),diag(2))) comps[[2]] = list(mu2, backsolve(chol(Sigma2),diag(2))) dm = rmixture(n, pvec, comps) ## run MCMC on normal mixture Data = list(y=dm$x) ncomp = 2 Prior = list(ncomp=ncomp, a=c(rep(100,ncomp))) R = 2000 Mcmc = list(R=R, keep=1) out = rnmixGibbs(Data=Data, Prior=Prior, Mcmc=Mcmc) ## find clusters begin = 500 end = R outclusterMix = clusterMix(out$nmix$zdraw[begin:end,]) ## check on clustering versus "truth" ## note: there could be switched labels table(outclusterMix$clustera, dm$z) table(outclusterMix$clusterb, dm$z) }
if(nchar(Sys.getenv("LONG_TEST")) != 0) { ## simulate data from mixture of normals n = 500 pvec = c(.5,.5) mu1 = c(2,2) mu2 = c(-2,-2) Sigma1 = matrix(c(1,0.5,0.5,1), ncol=2) Sigma2 = matrix(c(1,0.5,0.5,1), ncol=2) comps = NULL comps[[1]] = list(mu1, backsolve(chol(Sigma1),diag(2))) comps[[2]] = list(mu2, backsolve(chol(Sigma2),diag(2))) dm = rmixture(n, pvec, comps) ## run MCMC on normal mixture Data = list(y=dm$x) ncomp = 2 Prior = list(ncomp=ncomp, a=c(rep(100,ncomp))) R = 2000 Mcmc = list(R=R, keep=1) out = rnmixGibbs(Data=Data, Prior=Prior, Mcmc=Mcmc) ## find clusters begin = 500 end = R outclusterMix = clusterMix(out$nmix$zdraw[begin:end,]) ## check on clustering versus "truth" ## note: there could be switched labels table(outclusterMix$clustera, dm$z) table(outclusterMix$clusterb, dm$z) }
condMom
compute moments of conditional distribution of the th element of a multivariate normal given all others.
condMom(x, mu, sigi, i)
condMom(x, mu, sigi, i)
x |
vector of values to condition on; |
mu |
mean vector with |
sigi |
inverse of covariance matrix; dimension |
i |
conditional distribution of |
.
condMom
computes moments of given
.
A list containing:
cmean |
conditional mean |
cvar |
conditional variance |
This routine is a utility routine that does not check the input arguments for proper dimensions and type.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
sig = matrix(c(1, 0.5, 0.5, 0.5, 1, 0.5, 0.5, 0.5, 1), ncol=3) sigi = chol2inv(chol(sig)) mu = c(1,2,3) x = c(1,1,1) condMom(x, mu, sigi, 2)
sig = matrix(c(1, 0.5, 0.5, 0.5, 1, 0.5, 0.5, 0.5, 1), ncol=3) sigi = chol2inv(chol(sig)) mu = c(1,2,3) x = c(1,1,1) condMom(x, mu, sigi, 2)
createX
makes up an X matrix in the form expected by Multinomial
Logit (rmnlIndepMetrop
and rhierMnlRwMixture
)
and Probit (rmnpGibbs
and rmvpGibbs
) routines.
Requires an array of alternative-specific variables and/or an
array of "demographics" (or variables constant across alternatives) which
may vary across choice occasions.
createX(p, na, nd, Xa, Xd, INT = TRUE, DIFF = FALSE, base=p)
createX(p, na, nd, Xa, Xd, INT = TRUE, DIFF = FALSE, base=p)
p |
integer number of choice alternatives |
na |
integer number of alternative-specific vars in |
nd |
integer number of non-alternative specific vars |
Xa |
|
Xd |
|
INT |
logical flag for inclusion of intercepts |
DIFF |
logical flag for differencing wrt to base alternative |
base |
integer index of base choice alternative |
Note: na
, nd
, Xa
, Xd
can be NULL
to indicate lack of Xa
or Xd
variables.
X
matrix of dimension .
rmnpGibbs
assumes that the base
alternative is the default.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
na=2; nd=1; p=3 vec = c(1, 1.5, 0.5, 2, 3, 1, 3, 4.5, 1.5) Xa = matrix(vec, byrow=TRUE, ncol=3) Xa = cbind(Xa,-Xa) Xd = matrix(c(-1,-2,-3), ncol=1) createX(p=p, na=na, nd=nd, Xa=Xa, Xd=Xd) createX(p=p, na=na, nd=nd, Xa=Xa, Xd=Xd, base=1) createX(p=p, na=na, nd=nd, Xa=Xa, Xd=Xd, DIFF=TRUE) createX(p=p, na=na, nd=nd, Xa=Xa, Xd=Xd, DIFF=TRUE, base=2) createX(p=p, na=na, nd=NULL, Xa=Xa, Xd=NULL) createX(p=p, na=NULL, nd=nd, Xa=NULL, Xd=Xd)
na=2; nd=1; p=3 vec = c(1, 1.5, 0.5, 2, 3, 1, 3, 4.5, 1.5) Xa = matrix(vec, byrow=TRUE, ncol=3) Xa = cbind(Xa,-Xa) Xd = matrix(c(-1,-2,-3), ncol=1) createX(p=p, na=na, nd=nd, Xa=Xa, Xd=Xd) createX(p=p, na=na, nd=nd, Xa=Xa, Xd=Xd, base=1) createX(p=p, na=na, nd=nd, Xa=Xa, Xd=Xd, DIFF=TRUE) createX(p=p, na=na, nd=nd, Xa=Xa, Xd=Xd, DIFF=TRUE, base=2) createX(p=p, na=na, nd=NULL, Xa=Xa, Xd=NULL) createX(p=p, na=NULL, nd=nd, Xa=NULL, Xd=Xd)
Responses to a satisfaction survey for a Yellow Pages advertising product. All responses are on a 10 point scale from 1 to 10 (1 is "Poor" and 10 is "Excellent").
data(customerSat)
data(customerSat)
A data frame with 1811 observations on the following 10 variables:
...$q1 |
Overall Satisfaction |
...$q2 |
Setting Competitive Prices |
...$q3 |
Holding Price Increase to a Minimum |
...$q4 |
Appropriate Pricing given Volume |
...$q5 |
Demonstrating Effectiveness of Purchase |
...$q6 |
Reach a Large Number of Customers |
...$q7 |
Reach of Advertising |
...$q8 |
Long-term Exposure |
...$q9 |
Distribution |
...$q10 |
Distribution to Right Geographic Areas |
Rossi, Peter, Zvi Gilula, and Greg Allenby (2001), "Overcoming Scale Usage Heterogeneity," Journal of the American Statistical Association 96, 20–31.
Case Study 3, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
data(customerSat) apply(as.matrix(customerSat),2,table) ## see also examples for 'rscaleUsage'
data(customerSat) apply(as.matrix(customerSat),2,table) ## see also examples for 'rscaleUsage'
Monthly data on physician detailing (sales calls). 23 months of data for each of 1000 physicians; includes physician covariates.
data(detailing)
data(detailing)
The detailing
object is a list containing two data frames, counts
and demo
.
In the counts
data frame:
...$id |
identifies the physician |
...$scrips |
the number of new presectiptions ordered by the physician for the drug detailed |
...$detailing |
the number of sales called made to each physician per month |
...$lagged_scripts |
scrips value for prior month |
In the demo
data frame:
...$$id |
identifies the physician |
...$generalphys |
dummy for if doctor is a "general practitioner" |
...$specialist |
dummy for if the physician is a specialist in the theraputic class for which the drug is intended |
...$mean_samples |
the mean number of free drug samples given the doctor over the sample period |
Manchanda, Puneet, Pradeep Chintagunta, and Peter Rossi (2004), "Response Modeling with Non-Random Marketing Mix Variables," Journal of Marketing Research 41, 467–478.
data(detailing) cat(" table of Counts Dep Var", fill=TRUE) print(table(detailing$counts[,2])) cat(" means of Demographic Variables",fill=TRUE) mat = apply(as.matrix(detailing$demo[,2:4]), 2, mean) print(mat) ## example of processing for use with 'rhierNegbinRw' if(0) { data(detailing) counts = detailing$counts Z = detailing$demo # Construct the Z matrix Z[,1] = 1 Z[,2] = Z[,2] - mean(Z[,2]) Z[,3] = Z[,3] - mean(Z[,3]) Z[,4] = Z[,4] - mean(Z[,4]) Z = as.matrix(Z) id = levels(factor(counts$id)) nreg = length(id) nobs = nrow(counts$id) regdata = NULL for (i in 1:nreg) { X = counts[counts[,1] == id[i], c(3:4)] X = cbind(rep(1, nrow(X)), X) y = counts[counts[,1] == id[i], 2] X = as.matrix(X) regdata[[i]] = list(X=X, y=y) } rm(detailing, counts) cat("Finished reading data", fill=TRUE) fsh() Data = list(regdata=regdata, Z=Z) nvar = ncol(X) # Number of X variables nz = ncol(Z) # Number of Z variables deltabar = matrix(rep(0,nvar*nz), nrow=nz) Vdelta = 0.01*diag(nz) nu = nvar+3 V = 0.01*diag(nvar) a = 0.5 b = 0.1 Prior = list(deltabar=deltabar, Vdelta=Vdelta, nu=nu, V=V, a=a, b=b) R = 10000 keep = 1 s_beta = 2.93/sqrt(nvar) s_alpha = 2.93 c = 2 Mcmc = list(R=R, keep=keep, s_beta=s_beta, s_alpha=s_alpha, c=c) out = rhierNegbinRw(Data, Prior, Mcmc) ## Unit level mean beta parameters Mbeta = matrix(rep(0,nreg*nvar), nrow=nreg) ndraws = length(out$alphadraw) for (i in 1:nreg) { Mbeta[i,] = rowSums(out$Betadraw[i,,])/ndraws } cat(" Deltadraws ", fill=TRUE) summary(out$Deltadraw) cat(" Vbetadraws ", fill=TRUE) summary(out$Vbetadraw) cat(" alphadraws ", fill=TRUE) summary(out$alphadraw) ## plotting examples if(0){ plot(out$betadraw) plot(out$alphadraw) plot(out$Deltadraw) } }
data(detailing) cat(" table of Counts Dep Var", fill=TRUE) print(table(detailing$counts[,2])) cat(" means of Demographic Variables",fill=TRUE) mat = apply(as.matrix(detailing$demo[,2:4]), 2, mean) print(mat) ## example of processing for use with 'rhierNegbinRw' if(0) { data(detailing) counts = detailing$counts Z = detailing$demo # Construct the Z matrix Z[,1] = 1 Z[,2] = Z[,2] - mean(Z[,2]) Z[,3] = Z[,3] - mean(Z[,3]) Z[,4] = Z[,4] - mean(Z[,4]) Z = as.matrix(Z) id = levels(factor(counts$id)) nreg = length(id) nobs = nrow(counts$id) regdata = NULL for (i in 1:nreg) { X = counts[counts[,1] == id[i], c(3:4)] X = cbind(rep(1, nrow(X)), X) y = counts[counts[,1] == id[i], 2] X = as.matrix(X) regdata[[i]] = list(X=X, y=y) } rm(detailing, counts) cat("Finished reading data", fill=TRUE) fsh() Data = list(regdata=regdata, Z=Z) nvar = ncol(X) # Number of X variables nz = ncol(Z) # Number of Z variables deltabar = matrix(rep(0,nvar*nz), nrow=nz) Vdelta = 0.01*diag(nz) nu = nvar+3 V = 0.01*diag(nvar) a = 0.5 b = 0.1 Prior = list(deltabar=deltabar, Vdelta=Vdelta, nu=nu, V=V, a=a, b=b) R = 10000 keep = 1 s_beta = 2.93/sqrt(nvar) s_alpha = 2.93 c = 2 Mcmc = list(R=R, keep=keep, s_beta=s_beta, s_alpha=s_alpha, c=c) out = rhierNegbinRw(Data, Prior, Mcmc) ## Unit level mean beta parameters Mbeta = matrix(rep(0,nreg*nvar), nrow=nreg) ndraws = length(out$alphadraw) for (i in 1:nreg) { Mbeta[i,] = rowSums(out$Betadraw[i,,])/ndraws } cat(" Deltadraws ", fill=TRUE) summary(out$Deltadraw) cat(" Vbetadraws ", fill=TRUE) summary(out$Vbetadraw) cat(" alphadraws ", fill=TRUE) summary(out$alphadraw) ## plotting examples if(0){ plot(out$betadraw) plot(out$alphadraw) plot(out$Deltadraw) } }
eMixMargDen
assumes that a multivariate mixture of normals has been fitted
via MCMC (using rnmixGibbs
). For each MCMC draw, eMixMargDen
computes
the marginal densities for each component in the multivariate mixture on a user-supplied
grid and then averages over the MCMC draws.
eMixMargDen(grid, probdraw, compdraw)
eMixMargDen(grid, probdraw, compdraw)
grid |
array of grid points, |
probdraw |
array where each row contains a draw of probabilities of the mixture component |
compdraw |
list of lists of draws of mixture component moments |
length(compdraw) |
is the number of MCMC draws. |
compdraw[[i]] |
is a list draws of mu and of the inverse Cholesky root for each of mixture components. |
compdraw[[i]][[j]] |
is j th component. |
compdraw[[i]][[j]]$mu |
is mean vector. |
compdraw[[i]][[j]]$rooti |
is the UL decomp of .
|
An array of the same dimension as grid
with density values.
This routine is a utility routine that does not check the input arguments for proper dimensions and type. To avoid errors, call with output from rnmixGibbs
.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Bayesian Statistics and Marketingby Rossi, Allenby, and McCulloch.
ghkvec
computes the GHK approximation to the integral of a multivariate normal density over a half plane defined by a set of truncation points.
ghkvec(L, trunpt, above, r, HALTON=TRUE, pn)
ghkvec(L, trunpt, above, r, HALTON=TRUE, pn)
L |
lower triangular Cholesky root of covariance matrix |
trunpt |
vector of truncation points |
above |
vector of indicators for truncation above(1) or below(0) on an element by element basis |
r |
number of draws to use in GHK |
HALTON |
if |
pn |
prime number used for Halton sequence (def: the smallest prime numbers, i.e. 2, 3, 5, ...) |
Approximation to integral
ghkvec
can accept a vector of truncations and compute more than one integral. That is, length(trunpt)/length(above)
number of different integrals, each with the same variance and mean 0 but different truncation points. See 'examples' below for an example with two integrals at different truncation points. The above
argument specifies truncation from above (1) or below (0) on an element by element basis. Only one vector of above is allowed but multiple truncation points are allowed.
The user can choose between two random number generators for the numerical integration: psuedo-random numbers by R::runif
or quasi-random numbers by a Halton sequence. Generally, the quasi-random (Halton) sequence is more uniformly distributed within domain, so it shows lower error and improved convergence than the psuedo-random (runif
) sequence (Morokoff and Caflisch, 1995).
For the prime numbers generating Halton sequence, we suggest to use the first smallest prime numbers. Halton (1960) and Kocis and Whiten (1997) prove that their discrepancy measures (how uniformly the sample points are distributed) have the upper bounds, which decrease as the generating prime number decreases.
Note: For a high dimensional integration (10 or more dimension), we suggest use of the psuedo-random number generator (R::runif
) because, according to Kocis and Whiten (1997), Halton sequences may be highly correlated when the dimension is 10 or more.
Peter Rossi, Anderson School, UCLA, [email protected].
Keunwoo Kim, Anderson School, UCLA, [email protected].
For further discussion, see Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch, Chapter 2.
For Halton sequence, see Halton (1960, Numerische Mathematik), Morokoff and Caflisch (1995, Journal of Computational Physics), and Kocis and Whiten (1997, ACM Transactions on Mathematical Software).
Sigma = matrix(c(1, 0.5, 0.5, 1), ncol=2) L = t(chol(Sigma)) trunpt = c(0,0,1,1) above = c(1,1) # here we have a two dimensional integral with two different truncation points # (0,0) and (1,1) # however, there is only one vector of "above" indicators for each integral # above=c(1,1) is applied to both integrals. # drawn by Halton sequence ghkvec(L, trunpt, above, r=100) # use prime number 11 and 13 ghkvec(L, trunpt, above, r=100, HALTON=TRUE, pn=c(11,13)) # drawn by R::runif ghkvec(L, trunpt, above, r=100, HALTON=FALSE)
Sigma = matrix(c(1, 0.5, 0.5, 1), ncol=2) L = t(chol(Sigma)) trunpt = c(0,0,1,1) above = c(1,1) # here we have a two dimensional integral with two different truncation points # (0,0) and (1,1) # however, there is only one vector of "above" indicators for each integral # above=c(1,1) is applied to both integrals. # drawn by Halton sequence ghkvec(L, trunpt, above, r=100) # use prime number 11 and 13 ghkvec(L, trunpt, above, r=100, HALTON=TRUE, pn=c(11,13)) # drawn by R::runif ghkvec(L, trunpt, above, r=100, HALTON=FALSE)
llmnl
evaluates log-likelihood for the multinomial logit model.
llmnl(beta, y, X)
llmnl(beta, y, X)
beta |
|
y |
|
X |
|
Let , then
.
is the submatrix of
corresponding to the
th observation.
has
rows.
Use createX
to create .
Value of log-likelihood (sum of log prob of observed multinomial outcomes).
This routine is a utility routine that does not check the input arguments for proper dimensions and type.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
## Not run: ll=llmnl(beta,y,X)
## Not run: ll=llmnl(beta,y,X)
llmnp
evaluates the log-likelihood for the multinomial probit model.
llmnp(beta, Sigma, X, y, r)
llmnp(beta, Sigma, X, y, r)
beta |
k x 1 vector of coefficients |
Sigma |
(p-1) x (p-1) covariance matrix of errors |
X |
n*(p-1) x k array where X is from differenced system |
y |
vector of n indicators of multinomial response (1, ..., p) |
r |
number of draws used in GHK |
is
matrix. Use
createX
with DIFF=TRUE
to create .
Model for each obs: with
.
Censoring mechanism:
if and
if
To use GHK, we must transform so that these are rectangular regions
e.g. if and
.
Define such that if
then
is equivalent to
. Thus, if
, we have
. Lower truncation is
and
. For
,
.
Value of log-likelihood (sum of log prob of observed multinomial outcomes)
This routine is a utility routine that does not check the input arguments for proper dimensions and type.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapters 2 and 4, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
## Not run: ll=llmnp(beta,Sigma,X,y,r)
## Not run: ll=llmnp(beta,Sigma,X,y,r)
llnhlogit
evaluates log-likelihood for the Non-homothetic Logit model.
llnhlogit(theta, choice, lnprices, Xexpend)
llnhlogit(theta, choice, lnprices, Xexpend)
theta |
parameter vector (see details section) |
choice |
|
lnprices |
|
Xexpend |
|
Non-homothetic logit model,
tau is the scale parameter of extreme value error distribution. solves
.
.
.
Structure of theta vector:
alpha: vector of utility intercepts.
kappaStar: vector of utility rotation parms expressed on natural log scale.
gamma: – expenditure variable coefs.
tau: – logit scale parameter.
Value of log-likelihood (sum of log prob of observed multinomial outcomes).
This routine is a utility routine that does not check the input arguments for proper dimensions and type.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 4, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
N=1000; p=3; k=1 theta = c(rep(1,p), seq(from=-1,to=1,length=p), rep(2,k), 0.5) lnprices = matrix(runif(N*p), ncol=p) Xexpend = matrix(runif(N*k), ncol=k) simdata = simnhlogit(theta, lnprices, Xexpend) ## evaluate likelihood at true theta llstar = llnhlogit(theta, simdata$y, simdata$lnprices, simdata$Xexpend)
N=1000; p=3; k=1 theta = c(rep(1,p), seq(from=-1,to=1,length=p), rep(2,k), 0.5) lnprices = matrix(runif(N*p), ncol=p) Xexpend = matrix(runif(N*k), ncol=k) simdata = simnhlogit(theta, lnprices, Xexpend) ## evaluate likelihood at true theta llstar = llnhlogit(theta, simdata$y, simdata$lnprices, simdata$Xexpend)
lndIChisq
computes the log of an Inverted Chi-Squared Density.
lndIChisq(nu, ssq, X)
lndIChisq(nu, ssq, X)
nu |
d.f. parameter |
ssq |
scale parameter |
X |
ordinate for density evaluation (this must be a matrix) |
with
Inverted Chi-Squared.
lndIChisq
computes the complete log-density, including normalizing constants.
Log density value
This routine is a utility routine that does not check the input arguments for proper dimensions and type.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 2, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
lndIChisq(3, 1, matrix(2))
lndIChisq(3, 1, matrix(2))
lndIWishart
computes the log of an Inverted Wishart density.
lndIWishart(nu, V, IW)
lndIWishart(nu, V, IW)
nu |
d.f. parameter |
V |
"location" parameter |
IW |
ordinate for density evaluation |
Inverted Wishart(nu,V).
In this parameterization, , where
is a
matrix
lndIWishart
computes the complete log-density, including normalizing constants.
Log density value
This routine is a utility routine that does not check the input arguments for proper dimensions and type.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 2, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
lndIWishart(5, diag(3), diag(3)+0.5)
lndIWishart(5, diag(3), diag(3)+0.5)
lndMvn
computes the log of a Multivariate Normal Density.
lndMvn(x, mu, rooti)
lndMvn(x, mu, rooti)
x |
density ordinate |
mu |
mu vector |
rooti |
inv of upper triangular Cholesky root of |
Log density value
This routine is a utility routine that does not check the input arguments for proper dimensions and type.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 2, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
Sigma = matrix(c(1, 0.5, 0.5, 1), ncol=2) lndMvn(x=c(rep(0,2)), mu=c(rep(0,2)), rooti=backsolve(chol(Sigma),diag(2)))
Sigma = matrix(c(1, 0.5, 0.5, 1), ncol=2) lndMvn(x=c(rep(0,2)), mu=c(rep(0,2)), rooti=backsolve(chol(Sigma),diag(2)))
lndMvst
computes the log of a Multivariate Student-t Density.
lndMvst(x, nu, mu, rooti, NORMC)
lndMvst(x, nu, mu, rooti, NORMC)
x |
density ordinate |
nu |
d.f. parameter |
mu |
mu vector |
rooti |
inv of Cholesky root of |
NORMC |
include normalizing constant (def: |
Log density value
This routine is a utility routine that does not check the input arguments for proper dimensions and type.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 2, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
Sigma = matrix(c(1, 0.5, 0.5, 1), ncol=2) lndMvst(x=c(rep(0,2)), nu=4,mu=c(rep(0,2)), rooti=backsolve(chol(Sigma),diag(2)))
Sigma = matrix(c(1, 0.5, 0.5, 1), ncol=2) lndMvst(x=c(rep(0,2)), nu=4,mu=c(rep(0,2)), rooti=backsolve(chol(Sigma),diag(2)))
logMargDenNR
computes log marginal density using the Newton-Raftery approximation.
logMargDenNR(ll)
logMargDenNR(ll)
ll |
vector of log-likelihoods evaluated at |
Approximation to log marginal density value.
This approximation can be influenced by outliers in the vector of log-likelihoods; use with care. This routine is a utility routine that does not check the input arguments for proper dimensions and type.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 6, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
Panel data on purchases of margarine by 516 households. Demographic variables are included.
data(margarine)
data(margarine)
The detailing
object is a list containing two data frames, choicePrice
and demos
.
In the choicePrice
data frame:
...$hhid |
household ID |
...$choice |
multinomial indicator of one of the 10 products |
The products are indicated by brand and type.
Brands:
...$Pk |
Parkay |
...$BB |
BlueBonnett |
...$Fl |
Fleischmanns |
...$Hse |
house |
...$Gen |
generic |
...$Imp |
Imperial |
...$SS |
Shed Spread |
Product type:
...$_Stk |
stick |
...$_Tub |
tub |
In the demos
data frame:
...$Fs3_4 |
dummy for family size 3-4 |
...$Fs5 |
dummy for family size >= 5 |
...$college |
dummy for education status |
...$whtcollar |
dummy for job status |
...$retired |
dummy for retirement status |
All prices are in U.S. dollars.
Allenby, Greg and Peter Rossi (1991), "Quality Perceptions and Asymmetric Switching Between Brands," Marketing Science 10, 185–205.
Chapter 5, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
data(margarine) cat(" Table of Choice Variable ", fill=TRUE) print(table(margarine$choicePrice[,2])) cat(" Means of Prices", fill=TRUE) mat=apply(as.matrix(margarine$choicePrice[,3:12]), 2, mean) print(mat) cat(" Quantiles of Demographic Variables", fill=TRUE) mat=apply(as.matrix(margarine$demos[,2:8]), 2, quantile) print(mat) ## example of processing for use with 'rhierMnlRwMixture' if(0) { select = c(1:5,7) ## select brands chPr = as.matrix(margarine$choicePrice) ## make sure to log prices chPr = cbind(chPr[,1], chPr[,2], log(chPr[,2+select])) demos = as.matrix(margarine$demos[,c(1,2,5)]) ## remove obs for other alts chPr = chPr[chPr[,2] <= 7,] chPr = chPr[chPr[,2] != 6,] ## recode choice chPr[chPr[,2] == 7,2] = 6 hhidl = levels(as.factor(chPr[,1])) lgtdata = NULL nlgt = length(hhidl) p = length(select) ## number of choice alts ind = 1 for (i in 1:nlgt) { nobs = sum(chPr[,1]==hhidl[i]) if(nobs >=5) { data = chPr[chPr[,1]==hhidl[i],] y = data[,2] names(y) = NULL X = createX(p=p, na=1, Xa=data[,3:8], nd=NULL, Xd=NULL, INT=TRUE, base=1) lgtdata[[ind]] = list(y=y, X=X, hhid=hhidl[i]) ind = ind+1 } } nlgt = length(lgtdata) ## now extract demos corresponding to hhs in lgtdata Z = NULL nlgt = length(lgtdata) for(i in 1:nlgt){ Z = rbind(Z, demos[demos[,1]==lgtdata[[i]]$hhid, 2:3]) } ## take log of income and family size and demean Z = log(Z) Z[,1] = Z[,1] - mean(Z[,1]) Z[,2] = Z[,2] - mean(Z[,2]) keep = 5 R = 20000 mcmc1 = list(keep=keep, R=R) out = rhierMnlRwMixture(Data=list(p=p,lgtdata=lgtdata, Z=Z), Prior=list(ncomp=1), Mcmc=mcmc1) summary(out$Deltadraw) summary(out$nmix) ## plotting examples if(0){ plot(out$nmix) plot(out$Deltadraw) } }
data(margarine) cat(" Table of Choice Variable ", fill=TRUE) print(table(margarine$choicePrice[,2])) cat(" Means of Prices", fill=TRUE) mat=apply(as.matrix(margarine$choicePrice[,3:12]), 2, mean) print(mat) cat(" Quantiles of Demographic Variables", fill=TRUE) mat=apply(as.matrix(margarine$demos[,2:8]), 2, quantile) print(mat) ## example of processing for use with 'rhierMnlRwMixture' if(0) { select = c(1:5,7) ## select brands chPr = as.matrix(margarine$choicePrice) ## make sure to log prices chPr = cbind(chPr[,1], chPr[,2], log(chPr[,2+select])) demos = as.matrix(margarine$demos[,c(1,2,5)]) ## remove obs for other alts chPr = chPr[chPr[,2] <= 7,] chPr = chPr[chPr[,2] != 6,] ## recode choice chPr[chPr[,2] == 7,2] = 6 hhidl = levels(as.factor(chPr[,1])) lgtdata = NULL nlgt = length(hhidl) p = length(select) ## number of choice alts ind = 1 for (i in 1:nlgt) { nobs = sum(chPr[,1]==hhidl[i]) if(nobs >=5) { data = chPr[chPr[,1]==hhidl[i],] y = data[,2] names(y) = NULL X = createX(p=p, na=1, Xa=data[,3:8], nd=NULL, Xd=NULL, INT=TRUE, base=1) lgtdata[[ind]] = list(y=y, X=X, hhid=hhidl[i]) ind = ind+1 } } nlgt = length(lgtdata) ## now extract demos corresponding to hhs in lgtdata Z = NULL nlgt = length(lgtdata) for(i in 1:nlgt){ Z = rbind(Z, demos[demos[,1]==lgtdata[[i]]$hhid, 2:3]) } ## take log of income and family size and demean Z = log(Z) Z[,1] = Z[,1] - mean(Z[,1]) Z[,2] = Z[,2] - mean(Z[,2]) keep = 5 R = 20000 mcmc1 = list(keep=keep, R=R) out = rhierMnlRwMixture(Data=list(p=p,lgtdata=lgtdata, Z=Z), Prior=list(ncomp=1), Mcmc=mcmc1) summary(out$Deltadraw) summary(out$nmix) ## plotting examples if(0){ plot(out$nmix) plot(out$Deltadraw) } }
mixDen
computes the marginal density for each dimension of a normal mixture at each of the points on a user-specifed grid.
mixDen(x, pvec, comps)
mixDen(x, pvec, comps)
x |
array where |
pvec |
vector of mixture component probabilites |
comps |
list of lists of components for normal mixture |
length(comps) |
is the number of mixture components |
comps[[j]] |
is a list of parameters of the th component |
comps[[j]]$mu |
is mean vector |
comps[[j]]$rooti |
is the UL decomp of
|
An array of the same dimension as grid with density values
This routine is a utility routine that does not check the input arguments for proper dimensions and type.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 3, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
mixDenBi
computes the implied bivariate marginal density from a mixture of normals with specified mixture probabilities and component parameters.
mixDenBi(i, j, xi, xj, pvec, comps)
mixDenBi(i, j, xi, xj, pvec, comps)
i |
index of first variable |
j |
index of second variable |
xi |
grid of values of first variable |
xj |
grid of values of second variable |
pvec |
normal mixture probabilities |
comps |
list of lists of components |
length(comps) |
is the number of mixture components |
comps[[j]] |
is a list of parameters of the th component |
comps[[j]]$mu |
is mean vector |
comps[[j]]$rooti |
is the UL decomp of
|
An array (length(xi)=length(xj) x 2
) with density value
This routine is a utility routine that does not check the input arguments for proper dimensions and type.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 3, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
mnlHess
computes expected Hessian () for Multinomial Logit Model.
mnlHess(beta, y, X)
mnlHess(beta, y, X)
beta |
|
y |
|
X |
|
See llmnl
for information on structure of X array. Use createX
to make X.
matrix
This routine is a utility routine that does not check the input arguments for proper dimensions and type.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 3, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
llmnl
, createX
, rmnlIndepMetrop
## Not run: mnlHess(beta, y, X)
## Not run: mnlHess(beta, y, X)
mnpProb
computes MNP probabilities for a given matrix corresponding to one observation. This function can be used with output from
rmnpGibbs
to simulate the posterior distribution of market shares or fitted probabilties.
mnpProb(beta, Sigma, X, r)
mnpProb(beta, Sigma, X, r)
beta |
MNP coefficients |
Sigma |
Covariance matrix of latents |
X |
|
r |
number of draws used in GHK (def: 100) |
See rmnpGibbs
for definition of the model and the interpretation of the beta and Sigma parameters. Uses the GHK method to compute choice probabilities. To simulate a distribution of probabilities, loop over the beta and Sigma draws from rmnpGibbs
output.
vector of choice probabilites
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapters 2 and 4, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
## example of computing MNP probabilites ## here Xa has the prices of each of the 3 alternatives Xa = matrix(c(1,.5,1.5), nrow=1) X = createX(p=3, na=1, nd=NULL, Xa=Xa, Xd=NULL, DIFF=TRUE) beta = c(1,-1,-2) ## beta contains two intercepts and the price coefficient Sigma = matrix(c(1, 0.5, 0.5, 1), ncol=2) mnpProb(beta, Sigma, X)
## example of computing MNP probabilites ## here Xa has the prices of each of the 3 alternatives Xa = matrix(c(1,.5,1.5), nrow=1) X = createX(p=3, na=1, nd=NULL, Xa=Xa, Xd=NULL, DIFF=TRUE) beta = c(1,-1,-2) ## beta contains two intercepts and the price coefficient Sigma = matrix(c(1, 0.5, 0.5, 1), ncol=2) mnpProb(beta, Sigma, X)
momMix
averages the moments of a normal mixture model over MCMC draws.
momMix(probdraw, compdraw)
momMix(probdraw, compdraw)
probdraw |
|
compdraw |
list of length |
R |
is the number of MCMC draws in argument list above. |
ncomp |
is the number of mixture components fitted. |
compdraw |
is a list of lists of lists with mixture components. |
compdraw[[i]] |
is th draw. |
compdraw[[i]][[j]][[1]] |
is the mean parameter vector for the th component, th MCMC draw. |
compdraw[[i]][[j]][[2]] |
is the UL decomposition of for the th component, th MCMC draw
|
A list containing:
mu |
posterior expectation of mean |
sigma |
posterior expectation of covariance matrix |
sd |
posterior expectation of vector of standard deviations |
corr |
posterior expectation of correlation matrix |
This routine is a utility routine that does not check the input arguments for proper dimensions and type.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 5, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
nmat
converts a covariance matrix (stored as a vector, col by col) to a correlation matrix (also stored as a vector).
nmat(vec)
nmat(vec)
vec |
|
This routine is often used with apply to convert an array of covariance MCMC draws to correlations. As in
corrdraws = apply(vardraws, 1, nmat)
.
vector with correlation matrix
This routine is a utility routine that does not check the input arguments for proper dimensions and type.
Peter Rossi, Anderson School, UCLA, [email protected].
set.seed(66) X = matrix(rnorm(200,4), ncol=2) Varmat = var(X) nmat(as.vector(Varmat))
set.seed(66) X = matrix(rnorm(200,4), ncol=2) Varmat = var(X) nmat(as.vector(Varmat))
numEff
computes the numerical standard error for the mean of a vector of draws as well as the relative numerical efficiency (ratio of variance of mean of this time series process relative to iid sequence).
numEff(x, m = as.integer(min(length(x),(100/sqrt(5000))*sqrt(length(x)))))
numEff(x, m = as.integer(min(length(x),(100/sqrt(5000))*sqrt(length(x)))))
x |
|
m |
number of lags for autocorrelations |
default for number of lags is chosen so that if ,
and increases as the
.
A list containing:
stderr |
standard error of the mean of |
f |
variance ratio (relative numerical efficiency) |
This routine is a utility routine that does not check the input arguments for proper dimensions and type.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 3, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
numEff(rnorm(1000), m=20) numEff(rnorm(1000))
numEff(rnorm(1000), m=20) numEff(rnorm(1000))
Weekly sales of refrigerated orange juice at 83 stores. Contains demographic information on those stores.
data(orangeJuice)
data(orangeJuice)
The orangeJuice
object is a list containing two data frames, yx
and storedemo
.
In the yx
data frame:
...$store |
store number |
...$brand |
brand indicator |
...$week |
week number |
...$logmove |
log of the number of units sold |
...$constant |
a vector of 1s |
...$price# |
price of brand # |
...$deal |
in-store coupon activity |
...$feature |
feature advertisement |
...$profit |
profit |
The price variables correspond to the following brands:
1 | Tropicana Premium 64 oz |
2 | Tropicana Premium 96 oz |
3 | Florida's Natural 64 oz |
4 | Tropicana 64 oz |
5 | Minute Maid 64 oz |
6 | Minute Maid 96 oz |
7 | Citrus Hill 64 oz |
8 | Tree Fresh 64 oz |
9 | Florida Gold 64 oz |
10 | Dominicks 64 oz |
11 | Dominicks 128 oz |
In the storedemo
data frame:
...$STORE |
store number |
...$AGE60 |
percentage of the population that is aged 60 or older |
...$EDUC |
percentage of the population that has a college degree |
...$ETHNIC |
percent of the population that is black or Hispanic |
...$INCOME |
median income |
...$HHLARGE |
percentage of households with 5 or more persons |
...$WORKWOM |
percentage of women with full-time jobs |
...$HVAL150 |
percentage of households worth more than $150,000 |
...$SSTRDIST |
distance to the nearest warehouse store |
...$SSTRVOL |
ratio of sales of this store to the nearest warehouse store |
...$CPDIST5 |
average distance in miles to the nearest 5 supermarkets |
...$CPWVOL5 |
ratio of sales of this store to the average of the nearest five stores |
Alan L. Montgomery (1997), "Creating Micro-Marketing Pricing Strategies Using Supermarket Scanner Data," Marketing Science 16(4) 315–337.
Chapter 5, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
## load data data(orangeJuice) ## print some quantiles of yx data cat("Quantiles of the Variables in yx data",fill=TRUE) mat = apply(as.matrix(orangeJuice$yx), 2, quantile) print(mat) ## print some quantiles of storedemo data cat("Quantiles of the Variables in storedemo data",fill=TRUE) mat = apply(as.matrix(orangeJuice$storedemo), 2, quantile) print(mat) ## processing for use with rhierLinearModel if(0) { ## select brand 1 for analysis brand1 = orangeJuice$yx[(orangeJuice$yx$brand==1),] store = sort(unique(brand1$store)) nreg = length(store) nvar = 14 regdata=NULL for (reg in 1:nreg) { y = brand1$logmove[brand1$store==store[reg]] iota = c(rep(1,length(y))) X = cbind(iota,log(brand1$price1[brand1$store==store[reg]]), log(brand1$price2[brand1$store==store[reg]]), log(brand1$price3[brand1$store==store[reg]]), log(brand1$price4[brand1$store==store[reg]]), log(brand1$price5[brand1$store==store[reg]]), log(brand1$price6[brand1$store==store[reg]]), log(brand1$price7[brand1$store==store[reg]]), log(brand1$price8[brand1$store==store[reg]]), log(brand1$price9[brand1$store==store[reg]]), log(brand1$price10[brand1$store==store[reg]]), log(brand1$price11[brand1$store==store[reg]]), brand1$deal[brand1$store==store[reg]], brand1$feat[brand1$store==store[reg]] ) regdata[[reg]] = list(y=y, X=X) } ## storedemo is standardized to zero mean. Z = as.matrix(orangeJuice$storedemo[,2:12]) dmean = apply(Z, 2, mean) for (s in 1:nreg) {Z[s,] = Z[s,] - dmean} iotaz = c(rep(1,nrow(Z))) Z = cbind(iotaz, Z) nz = ncol(Z) Data = list(regdata=regdata, Z=Z) Mcmc = list(R=R, keep=1) out = rhierLinearModel(Data=Data, Mcmc=Mcmc) summary(out$Deltadraw) summary(out$Vbetadraw) ## plotting examples if(0){ plot(out$betadraw) } }
## load data data(orangeJuice) ## print some quantiles of yx data cat("Quantiles of the Variables in yx data",fill=TRUE) mat = apply(as.matrix(orangeJuice$yx), 2, quantile) print(mat) ## print some quantiles of storedemo data cat("Quantiles of the Variables in storedemo data",fill=TRUE) mat = apply(as.matrix(orangeJuice$storedemo), 2, quantile) print(mat) ## processing for use with rhierLinearModel if(0) { ## select brand 1 for analysis brand1 = orangeJuice$yx[(orangeJuice$yx$brand==1),] store = sort(unique(brand1$store)) nreg = length(store) nvar = 14 regdata=NULL for (reg in 1:nreg) { y = brand1$logmove[brand1$store==store[reg]] iota = c(rep(1,length(y))) X = cbind(iota,log(brand1$price1[brand1$store==store[reg]]), log(brand1$price2[brand1$store==store[reg]]), log(brand1$price3[brand1$store==store[reg]]), log(brand1$price4[brand1$store==store[reg]]), log(brand1$price5[brand1$store==store[reg]]), log(brand1$price6[brand1$store==store[reg]]), log(brand1$price7[brand1$store==store[reg]]), log(brand1$price8[brand1$store==store[reg]]), log(brand1$price9[brand1$store==store[reg]]), log(brand1$price10[brand1$store==store[reg]]), log(brand1$price11[brand1$store==store[reg]]), brand1$deal[brand1$store==store[reg]], brand1$feat[brand1$store==store[reg]] ) regdata[[reg]] = list(y=y, X=X) } ## storedemo is standardized to zero mean. Z = as.matrix(orangeJuice$storedemo[,2:12]) dmean = apply(Z, 2, mean) for (s in 1:nreg) {Z[s,] = Z[s,] - dmean} iotaz = c(rep(1,nrow(Z))) Z = cbind(iotaz, Z) nz = ncol(Z) Data = list(regdata=regdata, Z=Z) Mcmc = list(R=R, keep=1) out = rhierLinearModel(Data=Data, Mcmc=Mcmc) summary(out$Deltadraw) summary(out$Vbetadraw) ## plotting examples if(0){ plot(out$betadraw) } }
plot.bayesm.hcoef
is an S3 method to plot 3 dim arrays of hierarchical coefficients. Arrays are of class bayesm.hcoef
with dimensions: cross-sectional unit coef
MCMC draw.
## S3 method for class 'bayesm.hcoef' plot(x,names,burnin,...)
## S3 method for class 'bayesm.hcoef' plot(x,names,burnin,...)
x |
An object of S3 class, |
names |
a list of names for the variables in the hierarchical model |
burnin |
no draws to burnin (def: |
... |
standard graphics parameters |
Typically, plot.bayesm.hcoef
will be invoked by a call to the generic plot function as in plot(object)
where object is of class bayesm.hcoef
. All of the bayesm
hierarchical routines return draws of hierarchical coefficients in this class (see example below). One can also simply invoke plot.bayesm.hcoef
on any valid 3-dim array as in plot.bayesm.hcoef(betadraws)
.
plot.bayesm.hcoef
is also exported for use as a standard function, as in plot.bayesm.hcoef(array)
.
Peter Rossi, Anderson School, UCLA, [email protected].
rhierMnlRwMixture
,rhierLinearModel
,
rhierLinearMixture
,rhierNegbinRw
## Not run: out=rhierLinearModel(Data,Prior,Mcmc); plot(out$betadraws)
## Not run: out=rhierLinearModel(Data,Prior,Mcmc); plot(out$betadraws)
plot.bayesm.mat
is an S3 method to plot arrays of MCMC draws. The columns in the array correspond to parameters and the rows to MCMC draws.
## S3 method for class 'bayesm.mat' plot(x,names,burnin,tvalues,TRACEPLOT,DEN,INT,CHECK_NDRAWS, ...)
## S3 method for class 'bayesm.mat' plot(x,names,burnin,tvalues,TRACEPLOT,DEN,INT,CHECK_NDRAWS, ...)
x |
An object of either S3 class, |
names |
optional character vector of names for coefficients |
burnin |
number of draws to discard for burn-in (def: |
tvalues |
vector of true values |
TRACEPLOT |
logical, |
DEN |
logical, |
INT |
logical, |
CHECK_NDRAWS |
logical, |
... |
standard graphics parameters |
Typically, plot.bayesm.mat
will be invoked by a call to the generic plot function as in plot(object)
where object is of class bayesm.mat
. All of the bayesm
MCMC routines return draws in this class (see example below). One can also simply invoke plot.bayesm.mat
on any valid 2-dim array as in plot.bayesm.mat(betadraws)
. plot.bayesm.mat
paints (by default) on the histogram:
green "[]" delimiting 95% Bayesian Credibility Interval
yellow "()" showing +/- 2 numerical standard errors
red "|" showing posterior mean
plot.bayesm.mat
is also exported for use as a standard function, as in plot.bayesm.mat(matrix)
Peter Rossi, Anderson School, UCLA, [email protected].
## Not run: out=runiregGibbs(Data,Prior,Mcmc); plot(out$betadraw)
## Not run: out=runiregGibbs(Data,Prior,Mcmc); plot(out$betadraw)
plot.bayesm.nmix
is an S3 method to plot aspects of the fitted density from a list of MCMC draws of normal mixture components. Plots of marginal univariate and bivariate densities are produced.
## S3 method for class 'bayesm.nmix' plot(x, names, burnin, Grid, bi.sel, nstd, marg, Data, ngrid, ndraw, ...)
## S3 method for class 'bayesm.nmix' plot(x, names, burnin, Grid, bi.sel, nstd, marg, Data, ngrid, ndraw, ...)
x |
An object of S3 class |
names |
optional character vector of names for each of the dimensions |
burnin |
number of draws to discard for burn-in (def: |
Grid |
matrix of grid points for densities, def: mean +/- nstd std deviations (if Data no supplied), range of Data if supplied) |
bi.sel |
list of vectors, each giving pairs for bivariate distributions (def: |
nstd |
number of standard deviations for default Grid (def: 2) |
marg |
logical, if TRUE display marginals (def: |
Data |
matrix of data points, used to paint histograms on marginals and for grid |
ngrid |
number of grid points for density estimates (def: 50) |
ndraw |
number of draws to average Mcmc estimates over (def: 200) |
... |
standard graphics parameters |
Typically, plot.bayesm.nmix
will be invoked by a call to the generic plot function as in plot(object)
where object is of class bayesm.nmix. These objects are lists of three components. The first component is an array of draws of mixture component probabilties. The second component is not used. The third is a lists of lists of lists with draws of each of the normal components.plot.bayesm.nmix
can also be used as a standard function, as in plot.bayesm.nmix(list)
.
Peter Rossi, Anderson School, UCLA, [email protected].
rnmixGibbs
, rhierMnlRwMixture
, rhierLinearMixture
, rDPGibbs
## not run # out = rnmixGibbs(Data, Prior, Mcmc) ## plot bivariate distributions for dimension 1,2; 3,4; and 1,3 # plot(out,bi.sel=list(c(1,2),c(3,4),c(1,3)))
## not run # out = rnmixGibbs(Data, Prior, Mcmc) ## plot bivariate distributions for dimension 1,2; 3,4; and 1,3 # plot(out,bi.sel=list(c(1,2),c(3,4),c(1,3)))
rbayesBLP
implements a hybrid MCMC algorithm for aggregate level sales data in a market with differentiated products. bayesm version 3.1-0 and prior verions contain an error when using instruments with this function; this will be fixed in a future version.
rbayesBLP(Data, Prior, Mcmc)
rbayesBLP(Data, Prior, Mcmc)
Data |
list(X, share, J, Z) |
Prior |
list(sigmasqR, theta_hat, A, deltabar, Ad, nu0, s0_sq, VOmega) |
Mcmc |
list(R, keep, nprint, H, initial_theta_bar, initial_r, initial_tau_sq, initial_Omega, initial_delta, s, cand_cov, tol) |
A list containing:
thetabardraw |
|
Sigmadraw |
|
rdraw |
|
tausqdraw |
|
Omegadraw |
|
deltadraw |
|
acceptrate |
scalor of acceptance rate of Metropolis-Hasting |
s |
scale parameter used for Metropolis-Hasting |
cand_cov |
var-cov matrix used for Metropolis-Hasting |
Data = list(X, share, J, Z)
[Z
optional]
J: |
number of alternatives, excluding an outside option |
X: |
matrix (no outside option, which is normalized to 0). |
If IV is used, the last column of X is the endogeneous variable. |
|
share: |
vector (no outside option). |
Note that both the share vector and the X matrix are organized by the index. |
|
varies faster than , i.e. |
|
Z: |
matrix of instrumental variables (optional)
|
Prior = list(sigmasqR, theta_hat, A, deltabar, Ad, nu0, s0_sq, VOmega)
[optional]
sigmasqR: |
vector for prior variance (def: diffuse prior for ) |
theta_hat: |
vector for prior mean (def: 0 vector) |
A: |
matrix for prior precision (def: 0.01*diag(K) ) |
deltabar: |
vector for prior mean (def: 0 vector) |
Ad: |
matrix for prior precision (def: 0.01*diag(I) ) |
nu0: |
d.f. parameter for and prior (def: K+1) |
s0_sq: |
scale parameter for prior (def: 1) |
VOmega: |
matrix parameter for prior (def: matrix(c(1,0.5,0.5,1),2,2) )
|
Mcmc = list(R, keep, nprint, H, initial_theta_bar, initial_r, initial_tau_sq, initial_Omega, initial_delta, s, cand_cov, tol)
[only R
and H
required]
R: |
number of MCMC draws |
keep: |
MCMC thinning parameter -- 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) |
H: |
number of random draws used for Monte-Carlo integration |
initial_theta_bar: |
initial value of (def: 0 vector) |
initial_r: |
initial value of (def: 0 vector) |
initial_tau_sq: |
initial value of (def: 0.1) |
initial_Omega: |
initial value of (def: diag(2) ) |
initial_delta: |
initial value of (def: 0 vector) |
s: |
scale parameter of Metropolis-Hasting increment (def: automatically tuned) |
cand_cov: |
var-cov matrix of Metropolis-Hasting increment (def: automatically tuned) |
tol: |
convergence tolerance for the contraction mapping (def: 1e-6) |
type I Extreme Value (logit)
This structure implies a logit model for each consumer ().
Aggregate shares (
share
) are produced by integrating this consumer level
logit model over the assumed normal distribution of .
Note: we observe the aggregate level market share, not individual level choices.
Note: is the vector of nonzero elements of cholesky root of
.
Instead of
we draw
, which is one-to-one correspondence with the positive-definite
.
type I Extreme Value (logit)
Step 1 ():
Given and
, draw
via Metropolis-Hasting.
Covert the drawn to
.
Note: if user does not specify the Metropolis-Hasting increment parameters
(s
and cand_cov
), rbayesBLP
automatically tunes the parameters.
Step 2 without IV (,
):
Given , draw
and
via Gibbs sampler.
Step 2 with IV (,
,
):
Given , draw
,
, and
via IV Gibbs sampler.
r_cand = r_old + s*N(0,cand_cov)
Fix the candidate covariance matrix as cand_cov0 = diag(rep(0.1, K), rep(1, K*(K-1)/2)).
Start from s0 = 2.38/sqrt(dim(r))
Repeat{
Run 500 MCMC chain.
If acceptance rate < 30% => update s1 = s0/5.
If acceptance rate > 50% => update s1 = s0*3.
(Store r draws if acceptance rate is 20~80%.)
s0 = s1
} until acceptance rate is 30~50%
Scale matrix C = s1*sqrt(cand_cov0)
Correlation matrix R = Corr(r draws)
Use C*R*C as s^2*cand_cov.
Peter Rossi and K. Kim, Anderson School, UCLA, [email protected].
For further discussion, see Bayesian Analysis of Random Coefficient Logit Models Using Aggregate Data by Jiang, Manchanda, and Rossi, Journal of Econometrics, 2009.
if(nchar(Sys.getenv("LONG_TEST")) != 0) { ## Simulate aggregate level data simulData <- function(para, others, Hbatch) { # Hbatch does the integration for computing market shares # in batches of size Hbatch ## parameters theta_bar <- para$theta_bar Sigma <- para$Sigma tau_sq <- para$tau_sq T <- others$T J <- others$J p <- others$p H <- others$H K <- J + p ## build X X <- matrix(runif(T*J*p), T*J, p) inter <- NULL for (t in 1:T) { inter <- rbind(inter, diag(J)) } X <- cbind(inter, X) ## draw eta ~ N(0, tau_sq) eta <- rnorm(T*J)*sqrt(tau_sq) X <- cbind(X, eta) share <- rep(0, J*T) for (HH in 1:(H/Hbatch)){ ## draw theta ~ N(theta_bar, Sigma) cho <- chol(Sigma) theta <- matrix(rnorm(K*Hbatch), nrow=K, ncol=Hbatch) theta <- t(cho)%*%theta + theta_bar ## utility V <- X%*%rbind(theta, 1) expV <- exp(V) expSum <- matrix(colSums(matrix(expV, J, T*Hbatch)), T, Hbatch) expSum <- expSum %x% matrix(1, J, 1) choiceProb <- expV / (1 + expSum) share <- share + rowSums(choiceProb) / H } ## the last K+1'th column is eta, which is unobservable. X <- X[,c(1:K)] return (list(X=X, share=share)) } ## true parameter theta_bar_true <- c(-2, -3, -4, -5) Sigma_true <- rbind(c(3,2,1.5,1), c(2,4,-1,1.5), c(1.5,-1,4,-0.5), c(1,1.5,-0.5,3)) cho <- chol(Sigma_true) r_true <- c(log(diag(cho)), cho[1,2:4], cho[2,3:4], cho[3,4]) tau_sq_true <- 1 ## simulate data set.seed(66) T <- 300 J <- 3 p <- 1 K <- 4 H <- 1000000 Hbatch <- 5000 dat <- simulData(para=list(theta_bar=theta_bar_true, Sigma=Sigma_true, tau_sq=tau_sq_true), others=list(T=T, J=J, p=p, H=H), Hbatch) X <- dat$X share <- dat$share ## Mcmc run R <- 2000 H <- 50 Data1 <- list(X=X, share=share, J=J) Mcmc1 <- list(R=R, H=H, nprint=0) set.seed(66) out <- rbayesBLP(Data=Data1, Mcmc=Mcmc1) ## acceptance rate out$acceptrate ## summary of draws summary(out$thetabardraw) summary(out$Sigmadraw) summary(out$tausqdraw) ### plotting draws plot(out$thetabardraw) plot(out$Sigmadraw) plot(out$tausqdraw) }
if(nchar(Sys.getenv("LONG_TEST")) != 0) { ## Simulate aggregate level data simulData <- function(para, others, Hbatch) { # Hbatch does the integration for computing market shares # in batches of size Hbatch ## parameters theta_bar <- para$theta_bar Sigma <- para$Sigma tau_sq <- para$tau_sq T <- others$T J <- others$J p <- others$p H <- others$H K <- J + p ## build X X <- matrix(runif(T*J*p), T*J, p) inter <- NULL for (t in 1:T) { inter <- rbind(inter, diag(J)) } X <- cbind(inter, X) ## draw eta ~ N(0, tau_sq) eta <- rnorm(T*J)*sqrt(tau_sq) X <- cbind(X, eta) share <- rep(0, J*T) for (HH in 1:(H/Hbatch)){ ## draw theta ~ N(theta_bar, Sigma) cho <- chol(Sigma) theta <- matrix(rnorm(K*Hbatch), nrow=K, ncol=Hbatch) theta <- t(cho)%*%theta + theta_bar ## utility V <- X%*%rbind(theta, 1) expV <- exp(V) expSum <- matrix(colSums(matrix(expV, J, T*Hbatch)), T, Hbatch) expSum <- expSum %x% matrix(1, J, 1) choiceProb <- expV / (1 + expSum) share <- share + rowSums(choiceProb) / H } ## the last K+1'th column is eta, which is unobservable. X <- X[,c(1:K)] return (list(X=X, share=share)) } ## true parameter theta_bar_true <- c(-2, -3, -4, -5) Sigma_true <- rbind(c(3,2,1.5,1), c(2,4,-1,1.5), c(1.5,-1,4,-0.5), c(1,1.5,-0.5,3)) cho <- chol(Sigma_true) r_true <- c(log(diag(cho)), cho[1,2:4], cho[2,3:4], cho[3,4]) tau_sq_true <- 1 ## simulate data set.seed(66) T <- 300 J <- 3 p <- 1 K <- 4 H <- 1000000 Hbatch <- 5000 dat <- simulData(para=list(theta_bar=theta_bar_true, Sigma=Sigma_true, tau_sq=tau_sq_true), others=list(T=T, J=J, p=p, H=H), Hbatch) X <- dat$X share <- dat$share ## Mcmc run R <- 2000 H <- 50 Data1 <- list(X=X, share=share, J=J) Mcmc1 <- list(R=R, H=H, nprint=0) set.seed(66) out <- rbayesBLP(Data=Data1, Mcmc=Mcmc1) ## acceptance rate out$acceptrate ## summary of draws summary(out$thetabardraw) summary(out$Sigmadraw) summary(out$tausqdraw) ### plotting draws plot(out$thetabardraw) plot(out$Sigmadraw) plot(out$tausqdraw) }
rbiNormGibbs
implements a Gibbs Sampler for the bivariate normal distribution. Intermediate moves are plotted and the output is contrasted with the iid sampler. This function is designed for illustrative/teaching purposes.
rbiNormGibbs(initx=2, inity=-2, rho, burnin=100, R=500)
rbiNormGibbs(initx=2, inity=-2, rho, burnin=100, R=500)
initx |
initial value of parameter on x axis (def: 2) |
inity |
initial value of parameter on y axis (def: -2) |
rho |
correlation for bivariate normals |
burnin |
burn-in number of draws (def: 100) |
R |
number of MCMC draws (def: 500) |
) with
=
matrix(c(1,rho,rho,1),ncol=2)
matrix of draws
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapters 2 and 3, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
## Not run: out=rbiNormGibbs(rho=0.95)
## Not run: out=rbiNormGibbs(rho=0.95)
rbprobitGibbs
implements the Albert and Chib Gibbs Sampler for the binary probit model.
rbprobitGibbs(Data, Prior, Mcmc)
rbprobitGibbs(Data, Prior, Mcmc)
Data |
list(y, X) |
Prior |
list(betabar, A) |
Mcmc |
list(R, keep, nprint) |
with
if
Data = list(y, X)
y: |
vector of 0/1 outcomes |
X: |
design matrix
|
Prior = list(betabar, A)
[optional]
betabar: |
prior mean (def: 0) |
A: |
prior precision matrix (def: 0.01*I)
|
Mcmc = list(R, keep, nprint)
[only R
required]
R: |
number of MCMC draws |
keep: |
MCMC thinning parameter -- 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)
|
A list containing:
betadraw |
|
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 3, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) ## function to simulate from binary probit including x variable simbprobit = function(X, beta) { y = ifelse((X%*%beta + rnorm(nrow(X)))<0, 0, 1) list(X=X, y=y, beta=beta) } nobs = 200 X = cbind(rep(1,nobs), runif(nobs), runif(nobs)) beta = c(0,1,-1) nvar = ncol(X) simout = simbprobit(X, beta) Data1 = list(X=simout$X, y=simout$y) Mcmc1 = list(R=R, keep=1) out = rbprobitGibbs(Data=Data1, Mcmc=Mcmc1) summary(out$betadraw, tvalues=beta) ## plotting example if(0){plot(out$betadraw, tvalues=beta)}
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) ## function to simulate from binary probit including x variable simbprobit = function(X, beta) { y = ifelse((X%*%beta + rnorm(nrow(X)))<0, 0, 1) list(X=X, y=y, beta=beta) } nobs = 200 X = cbind(rep(1,nobs), runif(nobs), runif(nobs)) beta = c(0,1,-1) nvar = ncol(X) simout = simbprobit(X, beta) Data1 = list(X=simout$X, y=simout$y) Mcmc1 = list(R=R, keep=1) out = rbprobitGibbs(Data=Data1, Mcmc=Mcmc1) summary(out$betadraw, tvalues=beta) ## plotting example if(0){plot(out$betadraw, tvalues=beta)}
rdirichlet
draws from Dirichlet
rdirichlet(alpha)
rdirichlet(alpha)
alpha |
vector of Dirichlet parms (must be > 0) |
Vector of draws from Dirichlet
This routine is a utility routine that does not check the input arguments for proper dimensions and type.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 2, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
set.seed(66) rdirichlet(c(rep(3,5)))
set.seed(66) rdirichlet(c(rep(3,5)))
rDPGibbs
implements a Gibbs Sampler to draw from the posterior for a normal mixture problem with a Dirichlet Process prior. A natural conjugate base prior is used along with priors on the hyper parameters of this distribution. One interpretation of this model is as a normal mixture with a random number of components that can grow with the sample size.
rDPGibbs(Prior, Data, Mcmc)
rDPGibbs(Prior, Data, Mcmc)
Data |
list(y) |
Prior |
list(Prioralpha, lambda_hyper) |
Mcmc |
list(R, keep, nprint, maxuniq, SCALE, gridsize) |
uniform on grid[alim[1], alimb[2]]
uniform on grid[dim(data)-1 + exp(nulim[1]), dim(data)-1 + exp(nulim[2])]
uniform on grid[vlim[1], vlim[2]]
= alphamin then expected number of components =
Istarmin
= alphamax then expected number of components =
Istarmax
We parameterize the prior on such that
. The support of nu enforces valid IW density;
We use the structure for nmix
that is compatible with the bayesm
routines for finite mixtures of normals. This allows us to use the same summary and plotting methods.
The default choices of alim
, nulim
, and vlim
determine the location and approximate size of candidate "atoms" or possible normal components. The defaults are sensible given that we scale the data. Without scaling, you want to insure that alim
is set for a wide enough range of values (remember a is a precision parameter) and the v
is big enough to propose Sigma
matrices wide enough to cover the data range.
A careful analyst should look at the posterior distribution of a
, nu
, v
to make sure that the support is set correctly in alim
, nulim
, vlim
. In other words, if we see the posterior bunched up at one end of these support ranges, we should widen the range and rerun.
If you want to force the procedure to use many small atoms, then set nulim
to consider only large values and set vlim
to consider only small scaling constants. Set Istarmax
to a large number. This will create a very "lumpy" density estimate somewhat like the classical Kernel density estimates. Of course, this is not advised if you have a prior belief that densities are relatively smooth.
Data = list(y)
y: |
matrix of observations on dimensional data
|
Prior = list(Prioralpha, lambda_hyper)
[optional]
Prioralpha: |
list(Istarmin, Istarmax, power) |
$Istarmin: |
is expected number of components at lower bound of support of alpha (def: 1) |
$Istarmax: |
is expected number of components at upper bound of support of alpha (def: min(50, 0.1*nrow(y)) ) |
$power: |
is the power parameter for alpha prior (def: 0.8) |
lambda_hyper: |
list(alim, nulim, vlim) |
$alim: |
defines support of a distribution (def: c(0.01, 10) ) |
$nulim: |
defines support of nu distribution (def: c(0.01, 3) ) |
$vlim: |
defines support of v distribution (def: c(0.1, 4) )
|
Mcmc = list(R, keep, nprint, maxuniq, SCALE, gridsize)
[only R
required]
R: |
number of MCMC draws |
keep: |
MCMC thinning parameter -- 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) |
maxuniq: |
storage constraint on the number of unique components (def: 200) |
SCALE: |
should data be scaled by mean,std deviation before posterior draws (def: TRUE ) |
gridsize: |
number of discrete points for hyperparameter priors (def: 20) |
nmix
Detailsnmix
is a list with 3 components. Several functions in the bayesm
package that involve a Dirichlet Process or mixture-of-normals return nmix
. Across these functions, a common structure is used for nmix
in order to utilize generic summary and plotting functions.
probdraw: |
matrix that reports the probability that each draw came from a particular component |
zdraw: |
matrix that indicates which component each draw is assigned to |
compdraw: |
A list of lists of lists. Each of the inner-most lists has 2 elemens: a vector of draws for mu and a matrix of draws for the Cholesky root of Sigma .
|
A list containing:
nmix |
a list containing: |
alphadraw |
|
nudraw |
|
adraw |
|
vdraw |
|
Peter Rossi, Anderson School, UCLA, [email protected].
rnmixGibbs
, rmixture
, rmixGibbs
,
eMixMargDen
, momMix
, mixDen
, mixDenBi
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) ## simulate univariate data from Chi-Sq N = 200 chisqdf = 8 y1 = as.matrix(rchisq(N,df=chisqdf)) ## set arguments for rDPGibbs Data1 = list(y=y1) Prioralpha = list(Istarmin=1, Istarmax=10, power=0.8) Prior1 = list(Prioralpha=Prioralpha) Mcmc = list(R=R, keep=1, maxuniq=200) out1 = rDPGibbs(Prior=Prior1, Data=Data1, Mcmc=Mcmc) if(0){ ## plotting examples rgi = c(0,20) grid = matrix(seq(from=rgi[1],to=rgi[2],length.out=50), ncol=1) deltax = (rgi[2]-rgi[1]) / nrow(grid) plot(out1$nmix, Grid=grid, Data=y1) ## plot true density with historgram plot(range(grid[,1]), 1.5*range(dchisq(grid[,1],df=chisqdf)), type="n", xlab=paste("Chisq ; ",N," obs",sep=""), ylab="") hist(y1, xlim=rgi, freq=FALSE, col="yellow", breaks=20, add=TRUE) lines(grid[,1], dchisq(grid[,1],df=chisqdf) / (sum(dchisq(grid[,1],df=chisqdf))*deltax), col="blue", lwd=2) } ## simulate bivariate data from the "Banana" distribution (Meng and Barnard) banana = function(A, B, C1, C2, N, keep=10, init=10) { R = init*keep + N*keep x1 = x2 = 0 bimat = matrix(double(2*N), ncol=2) for (r in 1:R) { x1 = rnorm(1,mean=(B*x2+C1) / (A*(x2^2)+1), sd=sqrt(1/(A*(x2^2)+1))) x2 = rnorm(1,mean=(B*x2+C2) / (A*(x1^2)+1), sd=sqrt(1/(A*(x1^2)+1))) if (r>init*keep && r%%keep==0) { mkeep = r/keep bimat[mkeep-init,] = c(x1,x2) } } return(bimat) } set.seed(66) nvar2 = 2 A = 0.5 B = 0 C1 = C2 = 3 y2 = banana(A=A, B=B, C1=C1, C2=C2, 1000) Data2 = list(y=y2) Prioralpha = list(Istarmin=1, Istarmax=10, power=0.8) Prior2 = list(Prioralpha=Prioralpha) Mcmc = list(R=R, keep=1, maxuniq=200) out2 = rDPGibbs(Prior=Prior2, Data=Data2, Mcmc=Mcmc) if(0){ ## plotting examples rx1 = range(y2[,1]) rx2 = range(y2[,2]) x1 = seq(from=rx1[1], to=rx1[2], length.out=50) x2 = seq(from=rx2[1], to=rx2[2], length.out=50) grid = cbind(x1,x2) plot(out2$nmix, Grid=grid, Data=y2) ## plot true bivariate density tden = matrix(double(50*50), ncol=50) for (i in 1:50) { for (j in 1:50) { tden[i,j] = exp(-0.5*(A*(x1[i]^2)*(x2[j]^2) + (x1[i]^2) + (x2[j]^2) - 2*B*x1[i]*x2[j] - 2*C1*x1[i] - 2*C2*x2[j])) }} tden = tden / sum(tden) image(x1, x2, tden, col=terrain.colors(100), xlab="", ylab="") contour(x1, x2, tden, add=TRUE, drawlabels=FALSE) title("True Density") }
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) ## simulate univariate data from Chi-Sq N = 200 chisqdf = 8 y1 = as.matrix(rchisq(N,df=chisqdf)) ## set arguments for rDPGibbs Data1 = list(y=y1) Prioralpha = list(Istarmin=1, Istarmax=10, power=0.8) Prior1 = list(Prioralpha=Prioralpha) Mcmc = list(R=R, keep=1, maxuniq=200) out1 = rDPGibbs(Prior=Prior1, Data=Data1, Mcmc=Mcmc) if(0){ ## plotting examples rgi = c(0,20) grid = matrix(seq(from=rgi[1],to=rgi[2],length.out=50), ncol=1) deltax = (rgi[2]-rgi[1]) / nrow(grid) plot(out1$nmix, Grid=grid, Data=y1) ## plot true density with historgram plot(range(grid[,1]), 1.5*range(dchisq(grid[,1],df=chisqdf)), type="n", xlab=paste("Chisq ; ",N," obs",sep=""), ylab="") hist(y1, xlim=rgi, freq=FALSE, col="yellow", breaks=20, add=TRUE) lines(grid[,1], dchisq(grid[,1],df=chisqdf) / (sum(dchisq(grid[,1],df=chisqdf))*deltax), col="blue", lwd=2) } ## simulate bivariate data from the "Banana" distribution (Meng and Barnard) banana = function(A, B, C1, C2, N, keep=10, init=10) { R = init*keep + N*keep x1 = x2 = 0 bimat = matrix(double(2*N), ncol=2) for (r in 1:R) { x1 = rnorm(1,mean=(B*x2+C1) / (A*(x2^2)+1), sd=sqrt(1/(A*(x2^2)+1))) x2 = rnorm(1,mean=(B*x2+C2) / (A*(x1^2)+1), sd=sqrt(1/(A*(x1^2)+1))) if (r>init*keep && r%%keep==0) { mkeep = r/keep bimat[mkeep-init,] = c(x1,x2) } } return(bimat) } set.seed(66) nvar2 = 2 A = 0.5 B = 0 C1 = C2 = 3 y2 = banana(A=A, B=B, C1=C1, C2=C2, 1000) Data2 = list(y=y2) Prioralpha = list(Istarmin=1, Istarmax=10, power=0.8) Prior2 = list(Prioralpha=Prioralpha) Mcmc = list(R=R, keep=1, maxuniq=200) out2 = rDPGibbs(Prior=Prior2, Data=Data2, Mcmc=Mcmc) if(0){ ## plotting examples rx1 = range(y2[,1]) rx2 = range(y2[,2]) x1 = seq(from=rx1[1], to=rx1[2], length.out=50) x2 = seq(from=rx2[1], to=rx2[2], length.out=50) grid = cbind(x1,x2) plot(out2$nmix, Grid=grid, Data=y2) ## plot true bivariate density tden = matrix(double(50*50), ncol=50) for (i in 1:50) { for (j in 1:50) { tden[i,j] = exp(-0.5*(A*(x1[i]^2)*(x2[j]^2) + (x1[i]^2) + (x2[j]^2) - 2*B*x1[i]*x2[j] - 2*C1*x1[i] - 2*C2*x2[j])) }} tden = tden / sum(tden) image(x1, x2, tden, col=terrain.colors(100), xlab="", ylab="") contour(x1, x2, tden, add=TRUE, drawlabels=FALSE) title("True Density") }
This function has been deprecated. Please use rhierMnlRwMixture
instead.
rhierBinLogit
implements an MCMC algorithm for hierarchical binary logits with a normal heterogeneity distribution. This is a hybrid sampler with a RW Metropolis step for unit-level logit parameters.
rhierBinLogit
is designed for use on choice-based conjoint data with partial profiles. The Design matrix is based on differences of characteristics between two alternatives. See Appendix A of Bayesian Statistics and Marketing for details.
rhierBinLogit(Data, Prior, Mcmc)
rhierBinLogit(Data, Prior, Mcmc)
Data |
list(lgtdata, Z) |
Prior |
list(Deltabar, ADelta, nu, V) |
Mcmc |
list(R, keep, sbeta) |
with
and
is
units (or "respondents" for survey data)
= ZDelta[h,] +
Note: here ZDelta refers to Z%*%Delta
with ZDelta[h,] the th row of this product
Delta is an array
.
Data = list(lgtdata, Z)
[Z
optional]
lgtdata: |
list of lists with each cross-section unit MNL data |
lgtdata[[h]]$y: |
vector of binary outcomes (0,1) |
lgtdata[[h]]$X: |
design matrix for h'th unit |
Z: |
mat of unit chars (def: vector of ones)
|
Prior = list(Deltabar, ADelta, nu, V)
[optional]
Deltabar: |
matrix of prior means (def: 0) |
ADelta: |
prior precision matrix (def: 0.01I) |
nu: |
d.f. parameter for IW prior on normal component Sigma (def: nvar+3) |
V: |
pds location parm for IW prior on normal component Sigma (def: nuI) |
Mcmc = list(R, keep, sbeta)
[only R
required]
R: |
number of MCMC draws |
keep: |
MCMC thinning parm -- keep every keep th draw (def: 1) |
sbeta: |
scaling parm for RW Metropolis (def: 0.2) |
A list containing:
Deltadraw |
|
betadraw |
|
Vbetadraw |
|
llike |
|
reject |
|
Some experimentation with the Metropolis scaling paramter (sbeta
) may be required.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 5, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=10000} else {R=10} set.seed(66) nvar = 5 ## number of coefficients nlgt = 1000 ## number of cross-sectional units nobs = 10 ## number of observations per unit nz = 2 ## number of regressors in mixing distribution Z = matrix(c(rep(1,nlgt),runif(nlgt,min=-1,max=1)), nrow=nlgt, ncol=nz) Delta = matrix(c(-2, -1, 0, 1, 2, -1, 1, -0.5, 0.5, 0), nrow=nz, ncol=nvar) iota = matrix(1, nrow=nvar, ncol=1) Vbeta = diag(nvar) + 0.5*iota%*%t(iota) lgtdata=NULL for (i in 1:nlgt) { beta = t(Delta)%*%Z[i,] + as.vector(t(chol(Vbeta))%*%rnorm(nvar)) X = matrix(runif(nobs*nvar), nrow=nobs, ncol=nvar) prob = exp(X%*%beta) / (1+exp(X%*%beta)) unif = runif(nobs, 0, 1) y = ifelse(unif<prob, 1, 0) lgtdata[[i]] = list(y=y, X=X, beta=beta) } Data1 = list(lgtdata=lgtdata, Z=Z) Mcmc1 = list(R=R) out = rhierBinLogit(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)])) if(0){ ## plotting examples plot(out$Deltadraw,tvalues=as.vector(Delta)) plot(out$betadraw) plot(out$Vbetadraw,tvalues=as.vector(Vbeta[upper.tri(Vbeta,diag=TRUE)])) }
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=10000} else {R=10} set.seed(66) nvar = 5 ## number of coefficients nlgt = 1000 ## number of cross-sectional units nobs = 10 ## number of observations per unit nz = 2 ## number of regressors in mixing distribution Z = matrix(c(rep(1,nlgt),runif(nlgt,min=-1,max=1)), nrow=nlgt, ncol=nz) Delta = matrix(c(-2, -1, 0, 1, 2, -1, 1, -0.5, 0.5, 0), nrow=nz, ncol=nvar) iota = matrix(1, nrow=nvar, ncol=1) Vbeta = diag(nvar) + 0.5*iota%*%t(iota) lgtdata=NULL for (i in 1:nlgt) { beta = t(Delta)%*%Z[i,] + as.vector(t(chol(Vbeta))%*%rnorm(nvar)) X = matrix(runif(nobs*nvar), nrow=nobs, ncol=nvar) prob = exp(X%*%beta) / (1+exp(X%*%beta)) unif = runif(nobs, 0, 1) y = ifelse(unif<prob, 1, 0) lgtdata[[i]] = list(y=y, X=X, beta=beta) } Data1 = list(lgtdata=lgtdata, Z=Z) Mcmc1 = list(R=R) out = rhierBinLogit(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)])) if(0){ ## plotting examples plot(out$Deltadraw,tvalues=as.vector(Delta)) plot(out$betadraw) plot(out$Vbetadraw,tvalues=as.vector(Vbeta[upper.tri(Vbeta,diag=TRUE)])) }
rhierLinearMixture
implements a Gibbs Sampler for hierarchical linear models with a mixture-of-normals prior.
rhierLinearMixture(Data, Prior, Mcmc)
rhierLinearMixture(Data, Prior, Mcmc)
Data |
list(regdata, Z) |
Prior |
list(deltabar, Ad, mubar, Amu, nu, V, nu.e, ssq, ncomp) |
Mcmc |
list(R, keep, nprint) |
nreg
regression equations with nvar
as the number of vars in each equation
with
where
is the variance of
or
is an
matrix
should not include an intercept and should be centered for ease of interpretation.
The mean of each of the
nreg
s is the mean of the normal mixture.
Use
summary()
to compute this mean from the compdraw
output.
Be careful in assessing the prior parameter Amu
: 0.01 can be too small for some applications.
See chapter 5 of Rossi et al for full discussion.
Data = list(regdata, Z)
[Z
optional]
regdata: |
list of lists with and matrices for each of nreg=length(regdata) regressions |
regdata[[i]]$X: |
design matrix for equation |
regdata[[i]]$y: |
vector of observations for equation |
Z: |
matrix of unit characteristics (def: vector of ones)
|
Prior = list(deltabar, Ad, mubar, Amu, nu, V, nu.e, ssq, ncomp)
[all but ncomp
are optional]
deltabar: |
vector of prior means (def: 0) |
Ad: |
prior precision matrix for vec(Delta) (def: 0.01*I) |
mubar: |
prior mean vector for normal component mean (def: 0) |
Amu: |
prior precision for normal component mean (def: 0.01) |
nu: |
d.f. parameter for IW prior on normal component Sigma (def: nvar+3) |
V: |
PDS location parameter for IW prior on normal component Sigma (def: nu*I) |
nu.e: |
d.f. parameter for regression error variance prior (def: 3) |
ssq: |
scale parameter for regression error variance prior (def: var(y_i) ) |
a: |
Dirichlet prior parameter (def: 5) |
ncomp: |
number of components used in normal mixture |
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)
|
nmix
Detailsnmix
is a list with 3 components. Several functions in the bayesm
package that involve a Dirichlet Process or mixture-of-normals return nmix
. Across these functions, a common structure is used for nmix
in order to utilize generic summary and plotting functions.
probdraw: |
matrix that reports the probability that each draw came from a particular component |
zdraw: |
matrix that indicates which component each draw is assigned to (here, null) |
compdraw: |
A list of lists of lists. Each of the inner-most lists has 2 elemens: a vector of draws for mu and a matrix of draws for the Cholesky root of Sigma .
|
A list containing:
taudraw |
|
betadraw |
|
Deltadraw |
|
nmix |
a list containing: |
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 5, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) nreg = 300 nobs = 500 nvar = 3 nz = 2 Z = matrix(runif(nreg*nz), ncol=nz) Z = t(t(Z) - apply(Z,2,mean)) Delta = matrix(c(1,-1,2,0,1,0), ncol=nz) tau0 = 0.1 iota = c(rep(1,nobs)) ## create arguments for rmixture tcomps = NULL a = matrix(c(1,0,0,0.5773503,1.1547005,0,-0.4082483,0.4082483,1.2247449), ncol=3) tcomps[[1]] = list(mu=c(0,-1,-2), rooti=a) tcomps[[2]] = list(mu=c(0,-1,-2)*2, rooti=a) tcomps[[3]] = list(mu=c(0,-1,-2)*4, rooti=a) tpvec = c(0.4, 0.2, 0.4) ## simulated data with Z regdata = NULL betas = matrix(double(nreg*nvar), ncol=nvar) tind = double(nreg) for (reg in 1:nreg) { tempout = rmixture(1,tpvec,tcomps) betas[reg,] = Delta%*%Z[reg,] + as.vector(tempout$x) tind[reg] = tempout$z X = cbind(iota, matrix(runif(nobs*(nvar-1)),ncol=(nvar-1))) tau = tau0*runif(1,min=0.5,max=1) y = X%*%betas[reg,] + sqrt(tau)*rnorm(nobs) regdata[[reg]] = list(y=y, X=X, beta=betas[reg,], tau=tau) } ## run rhierLinearMixture Data1 = list(regdata=regdata, Z=Z) Prior1 = list(ncomp=3) Mcmc1 = list(R=R, keep=1) out1 = rhierLinearMixture(Data=Data1, Prior=Prior1, Mcmc=Mcmc1) cat("Summary of Delta draws", fill=TRUE) summary(out1$Deltadraw, tvalues=as.vector(Delta)) cat("Summary of Normal Mixture Distribution", fill=TRUE) summary(out1$nmix) ## plotting examples if(0){ plot(out1$betadraw) plot(out1$nmix) plot(out1$Deltadraw) }
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) nreg = 300 nobs = 500 nvar = 3 nz = 2 Z = matrix(runif(nreg*nz), ncol=nz) Z = t(t(Z) - apply(Z,2,mean)) Delta = matrix(c(1,-1,2,0,1,0), ncol=nz) tau0 = 0.1 iota = c(rep(1,nobs)) ## create arguments for rmixture tcomps = NULL a = matrix(c(1,0,0,0.5773503,1.1547005,0,-0.4082483,0.4082483,1.2247449), ncol=3) tcomps[[1]] = list(mu=c(0,-1,-2), rooti=a) tcomps[[2]] = list(mu=c(0,-1,-2)*2, rooti=a) tcomps[[3]] = list(mu=c(0,-1,-2)*4, rooti=a) tpvec = c(0.4, 0.2, 0.4) ## simulated data with Z regdata = NULL betas = matrix(double(nreg*nvar), ncol=nvar) tind = double(nreg) for (reg in 1:nreg) { tempout = rmixture(1,tpvec,tcomps) betas[reg,] = Delta%*%Z[reg,] + as.vector(tempout$x) tind[reg] = tempout$z X = cbind(iota, matrix(runif(nobs*(nvar-1)),ncol=(nvar-1))) tau = tau0*runif(1,min=0.5,max=1) y = X%*%betas[reg,] + sqrt(tau)*rnorm(nobs) regdata[[reg]] = list(y=y, X=X, beta=betas[reg,], tau=tau) } ## run rhierLinearMixture Data1 = list(regdata=regdata, Z=Z) Prior1 = list(ncomp=3) Mcmc1 = list(R=R, keep=1) out1 = rhierLinearMixture(Data=Data1, Prior=Prior1, Mcmc=Mcmc1) cat("Summary of Delta draws", fill=TRUE) summary(out1$Deltadraw, tvalues=as.vector(Delta)) cat("Summary of Normal Mixture Distribution", fill=TRUE) summary(out1$nmix) ## plotting examples if(0){ plot(out1$betadraw) plot(out1$nmix) plot(out1$Deltadraw) }
rhierLinearModel
implements a Gibbs Sampler for hierarchical linear models with a normal prior.
rhierLinearModel(Data, Prior, Mcmc)
rhierLinearModel(Data, Prior, Mcmc)
Data |
list(regdata, Z) |
Prior |
list(Deltabar, A, nu.e, ssq, nu, V) |
Mcmc |
list(R, keep, nprint) |
nreg
regression equations with
variables in each equation
with
nu.e*
where
is the variance of
N(Z
[i,],
)
Note: Z is the matrix
and [i,] refers to
th row of this product
given
are
;
is
;
is
.
Note: if you don't have any variables, omit
in the
Data
argument and
a vector of ones will be inserted; the matrix will be
and should
be interpreted as the mean of all unit
s.
Data = list(regdata, Z)
[Z
optional]
regdata: |
list of lists with and matrices for each of nreg=length(regdata) regressions |
regdata[[i]]$X: |
design matrix for equation |
regdata[[i]]$y: |
vector of observations for equation |
Z: |
matrix of unit characteristics (def: vector of ones)
|
Prior = list(Deltabar, A, nu.e, ssq, nu, V)
[optional]
Deltabar: |
matrix of prior means (def: 0) |
A: |
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)
|
A list containing:
betadraw |
|
taudraw |
|
Deltadraw |
|
Vbetadraw |
|
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 3, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
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) }
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) }
rhierMnlDP
is a MCMC algorithm for a hierarchical multinomial logit with a Dirichlet Process prior for the distribution of heteorogeneity. A base normal model is used so that the DP can be interpreted as allowing for a mixture of normals with as many components as there are panel units. This is a hybrid Gibbs Sampler with a RW Metropolis step for the MNL coefficients for each panel unit. This procedure can be interpreted as a Bayesian semi-parameteric method in the sense that the DP prior can accomodate heterogeniety of an unknown form.
rhierMnlDP(Data, Prior, Mcmc)
rhierMnlDP(Data, Prior, Mcmc)
Data |
list(lgtdata, Z, p) |
Prior |
list(deltabar, Ad, Prioralpha, lambda_hyper) |
Mcmc |
list(R, keep, nprint, s, w, maxunique, gridsize) |
with
and where
is
[i,] +
Note: Z is the matrix
; [i,] refers to
th row of this product
Delta is an matrix
uniform[alim[1], alimb[2]]
dim(data)-1 + exp(z)
uniform[dim(data)-1+nulim[1], nulim[2]]
uniform[vlim[1], vlim[2]]
alpha = alphamin then expected number of components = Istarmin
alpha = alphamax then expected number of components = Istarmax
should NOT include an intercept and is centered for ease of interpretation. The mean of each of the
nlgt
s is the mean of the normal mixture. Use
summary()
to compute this mean from the compdraw
output.
We parameterize the prior on such that
. The support of nu enforces a non-degenerate IW density;
.
The default choices of alim, nulim, and vlim determine the location and approximate size of candidate "atoms" or possible normal components. The defaults are sensible given a reasonable scaling of the X variables. You want to insure that alim is set for a wide enough range of values (remember a is a precision parameter) and the v is big enough to propose Sigma matrices wide enough to cover the data range.
A careful analyst should look at the posterior distribution of a, nu, v to make sure that the support is set correctly in alim, nulim, vlim. In other words, if we see the posterior bunched up at one end of these support ranges, we should widen the range and rerun.
If you want to force the procedure to use many small atoms, then set nulim to consider only large values and set vlim to consider only small scaling constants. Set alphamax to a large number. This will create a very "lumpy" density estimate somewhat like the classical Kernel density estimates. Of course, this is not advised if you have a prior belief that densities are relatively smooth.
Data = list(lgtdata, Z, p)
[Z
optional]
lgtdata: |
list of lists with each cross-section unit MNL data |
lgtdata[[i]]$y: |
vector of multinomial outcomes (1, ..., m) |
lgtdata[[i]]$X: |
design matrix for th unit |
Z: |
matrix of unit characteristics (def: vector of ones) |
p: |
number of choice alternatives |
Prior = list(deltabar, Ad, Prioralpha, lambda_hyper)
[optional]
deltabar: |
vector of prior means (def: 0) |
Ad: |
prior precision matrix for vec(D) (def: 0.01*I) |
Prioralpha: |
list(Istarmin, Istarmax, power) |
$Istarmin: |
expected number of components at lower bound of support of alpha def(1) |
$Istarmax: |
expected number of components at upper bound of support of alpha (def: min(50, 0.1*nlgt)) |
$power: |
power parameter for alpha prior (def: 0.8) |
lambda_hyper: |
list(alim, nulim, vlim) |
$alim: |
defines support of a distribution (def: c(0.01, 2) ) |
$nulim: |
defines support of nu distribution (def: c(0.001, 3) ) |
$vlim: |
defines support of v distribution (def: c(0.1, 4) )
|
Mcmc = list(R, keep, nprint, s, w, maxunique, gridsize)
[only R
required]
R: |
number of MCMC draws |
keep: |
MCMC thinning parameter -- 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) |
s: |
scaling parameter for RW Metropolis (def: 2.93/sqrt(nvar) ) |
w: |
fractional likelihood weighting parameter (def: 0.1) |
maxuniq: |
storage constraint on the number of unique components (def: 200) |
gridsize: |
number of discrete points for hyperparameter priors, (def: 20) |
nmix
Detailsnmix
is a list with 3 components. Several functions in the bayesm
package that involve a Dirichlet Process or mixture-of-normals return nmix
. Across these functions, a common structure is used for nmix
in order to utilize generic summary and plotting functions.
probdraw: |
matrix that reports the probability that each draw came from a particular component (here, a one-column matrix of 1s) |
zdraw: |
matrix that indicates which component each draw is assigned to (here, null) |
compdraw: |
A list of lists of lists. Each of the inner-most lists has 2 elemens: a vector of draws for mu and a matrix of draws for the Cholesky root of Sigma .
|
A list containing:
Deltadraw |
|
betadraw |
|
nmix |
a list containing: |
adraw |
|
vdraw |
|
nudraw |
|
Istardraw |
|
alphadraw |
|
loglike |
|
As is well known, Bayesian density estimation involves computing the predictive distribution of a "new" unit parameter, (here "n"=nlgt). This is done by averaging the normal base distribution over draws from the distribution of
given
, ...,
, alpha, lambda, data. To facilitate this, we store those draws from the predictive distribution of
in a list structure compatible with other
bayesm
routines that implement a finite mixture of normals.
Large R
values may be required (>20,000).
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 5, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=20000} else {R=10} set.seed(66) p = 3 # num of choice alterns ncoef = 3 nlgt = 300 # num of cross sectional units nz = 2 Z = matrix(runif(nz*nlgt),ncol=nz) Z = t(t(Z)-apply(Z,2,mean)) # demean Z ncomp = 3 # no of mixture components Delta=matrix(c(1,0,1,0,1,2),ncol=2) comps = NULL comps[[1]] = list(mu=c(0,-1,-2), rooti=diag(rep(2,3))) comps[[2]] = list(mu=c(0,-1,-2)*2, rooti=diag(rep(2,3))) comps[[3]] = list(mu=c(0,-1,-2)*4, rooti=diag(rep(2,3))) pvec=c(0.4, 0.2, 0.4) ## simulate from MNL model conditional on X matrix simmnlwX = function(n,X,beta) { k = length(beta) Xbeta = X%*%beta j = nrow(Xbeta) / n Xbeta = matrix(Xbeta, byrow=TRUE, ncol=j) Prob = exp(Xbeta) iota = c(rep(1,j)) denom = Prob%*%iota Prob = Prob / as.vector(denom) y = vector("double", n) ind = 1:j for (i in 1:n) { yvec = rmultinom(1, 1, Prob[i,]) y[i] = ind%*%yvec} return(list(y=y, X=X, beta=beta, prob=Prob)) } ## simulate data with a mixture of 3 normals simlgtdata = NULL ni = rep(50,300) for (i in 1:nlgt) { betai = Delta%*%Z[i,] + as.vector(rmixture(1,pvec,comps)$x) Xa = matrix(runif(ni[i]*p,min=-1.5,max=0), ncol=p) X = createX(p, na=1, nd=NULL, Xa=Xa, Xd=NULL, base=1) outa = simmnlwX(ni[i], X, betai) simlgtdata[[i]] = list(y=outa$y, X=X, beta=betai) } ## plot betas if(0){ bmat = matrix(0, nlgt, ncoef) for(i in 1:nlgt) { bmat[i,] = simlgtdata[[i]]$beta } par(mfrow = c(ncoef,1)) for(i in 1:ncoef) { hist(bmat[,i], breaks=30, col="magenta") } } ## set Data and Mcmc lists keep = 5 Mcmc1 = list(R=R, keep=keep) Data1 = list(p=p, lgtdata=simlgtdata, Z=Z) out = rhierMnlDP(Data=Data1, Mcmc=Mcmc1) cat("Summary of Delta draws", fill=TRUE) summary(out$Deltadraw, tvalues=as.vector(Delta)) ## plotting examples if(0) { plot(out$betadraw) plot(out$nmix) }
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=20000} else {R=10} set.seed(66) p = 3 # num of choice alterns ncoef = 3 nlgt = 300 # num of cross sectional units nz = 2 Z = matrix(runif(nz*nlgt),ncol=nz) Z = t(t(Z)-apply(Z,2,mean)) # demean Z ncomp = 3 # no of mixture components Delta=matrix(c(1,0,1,0,1,2),ncol=2) comps = NULL comps[[1]] = list(mu=c(0,-1,-2), rooti=diag(rep(2,3))) comps[[2]] = list(mu=c(0,-1,-2)*2, rooti=diag(rep(2,3))) comps[[3]] = list(mu=c(0,-1,-2)*4, rooti=diag(rep(2,3))) pvec=c(0.4, 0.2, 0.4) ## simulate from MNL model conditional on X matrix simmnlwX = function(n,X,beta) { k = length(beta) Xbeta = X%*%beta j = nrow(Xbeta) / n Xbeta = matrix(Xbeta, byrow=TRUE, ncol=j) Prob = exp(Xbeta) iota = c(rep(1,j)) denom = Prob%*%iota Prob = Prob / as.vector(denom) y = vector("double", n) ind = 1:j for (i in 1:n) { yvec = rmultinom(1, 1, Prob[i,]) y[i] = ind%*%yvec} return(list(y=y, X=X, beta=beta, prob=Prob)) } ## simulate data with a mixture of 3 normals simlgtdata = NULL ni = rep(50,300) for (i in 1:nlgt) { betai = Delta%*%Z[i,] + as.vector(rmixture(1,pvec,comps)$x) Xa = matrix(runif(ni[i]*p,min=-1.5,max=0), ncol=p) X = createX(p, na=1, nd=NULL, Xa=Xa, Xd=NULL, base=1) outa = simmnlwX(ni[i], X, betai) simlgtdata[[i]] = list(y=outa$y, X=X, beta=betai) } ## plot betas if(0){ bmat = matrix(0, nlgt, ncoef) for(i in 1:nlgt) { bmat[i,] = simlgtdata[[i]]$beta } par(mfrow = c(ncoef,1)) for(i in 1:ncoef) { hist(bmat[,i], breaks=30, col="magenta") } } ## set Data and Mcmc lists keep = 5 Mcmc1 = list(R=R, keep=keep) Data1 = list(p=p, lgtdata=simlgtdata, Z=Z) out = rhierMnlDP(Data=Data1, Mcmc=Mcmc1) cat("Summary of Delta draws", fill=TRUE) summary(out$Deltadraw, tvalues=as.vector(Delta)) ## plotting examples if(0) { plot(out$betadraw) plot(out$nmix) }
rhierMnlRwMixture
is a MCMC algorithm for a hierarchical multinomial logit with a mixture of normals heterogeneity distribution. This is a hybrid Gibbs Sampler with a RW Metropolis step for the MNL coefficients for each panel unit.
rhierMnlRwMixture(Data, Prior, Mcmc)
rhierMnlRwMixture(Data, Prior, Mcmc)
Data |
list(lgtdata, Z, p) |
Prior |
list(a, deltabar, Ad, mubar, Amu, nu, V, a, ncomp, SignRes) |
Mcmc |
list(R, keep, nprint, s, w) |
with
length(lgtdata)
and where
is
=
[i,] +
Note: Z is the matrix Z *
and [i,] refers to
th row of this product
Delta is an array
with
multinomial(pvec)
dirichlet(a)
Note: should NOT include an intercept and is centered for ease of interpretation.
The mean of each of the
nlgt
s is the mean of the normal mixture.
Use
summary()
to compute this mean from the compdraw
output.
Be careful in assessing prior parameter Amu
: 0.01 is too small for many applications.
See chapter 5 of Rossi et al for full discussion.
Data = list(lgtdata, Z, p)
[Z
optional]
lgtdata: |
list of nlgt=length(lgtdata) lists with each cross-section unit MNL data |
lgtdata[[i]]$y: |
vector of multinomial outcomes (1, ..., p) |
lgtdata[[i]]$X: |
design matrix for th unit |
Z: |
matrix of unit chars (def: vector of ones) |
p: |
number of choice alternatives |
Prior = list(a, deltabar, Ad, mubar, Amu, nu, V, a, ncomp, SignRes)
[all but ncomp
are optional]
a: |
vector of Dirichlet prior parameters (def: rep(5,ncomp) ) |
deltabar: |
vector of prior means (def: 0) |
Ad: |
prior precision matrix for vec(D) (def: 0.01*I) |
mubar: |
prior mean vector for normal component mean (def: 0 if unrestricted; 2 if restricted) |
Amu: |
prior precision for normal component mean (def: 0.01 if unrestricted; 0.1 if restricted) |
nu: |
d.f. parameter for IW prior on normal component Sigma (def: nvar+3 if unrestricted; nvar+15 if restricted) |
V: |
PDS location parameter for IW prior on normal component Sigma (def: nu*I if unrestricted; nu*D if restricted with d_pp = 4 if unrestricted and d_pp = 0.01 if restricted) |
ncomp: |
number of components used in normal mixture |
SignRes: |
vector of sign restrictions on the coefficient estimates (def: rep(0,nvar) )
|
Mcmc = list(R, keep, nprint, s, w)
[only R
required]
R: |
number of MCMC draws |
keep: |
MCMC thinning parameter -- 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) |
s: |
scaling parameter for RW Metropolis (def: 2.93/sqrt(nvar) ) |
w: |
fractional likelihood weighting parameter (def: 0.1) |
If has a sign restriction:
To use sign restrictions on the coefficients, SignRes
must be an vector containing values of either 0, -1, or 1. The value 0 means there is no sign restriction, -1 ensures that the coefficient is negative, and 1 ensures that the coefficient is positive. For example, if
SignRes = c(0,1,-1)
, the first coefficient is unconstrained, the second will be positive, and the third will be negative.
The sign restriction is implemented such that if the the 'th
has a non-zero sign restriction (i.e., it is constrained), we have
.
The sign restrictions (if used) will be reflected in the betadraw
output. However, the unconstrained mixture components are available in nmix
. Important: Note that draws from nmix
are distributed according to the mixture of normals but not the coefficients in betadraw
.
Care should be taken when selecting priors on any sign restricted coefficients. See related vignette for additional information.
nmix
Detailsnmix
is a list with 3 components. Several functions in the bayesm
package that involve a Dirichlet Process or mixture-of-normals return nmix
. Across these functions, a common structure is used for nmix
in order to utilize generic summary and plotting functions.
probdraw: |
matrix that reports the probability that each draw came from a particular component |
zdraw: |
matrix that indicates which component each draw is assigned to (here, null) |
compdraw: |
A list of lists of lists. Each of the inner-most lists has 2 elemens: a vector of draws for mu and a matrix of draws for the Cholesky root of Sigma .
|
A list containing:
Deltadraw |
|
betadraw |
|
nmix |
a list containing: |
loglike |
|
SignRes |
|
Note: as of version 2.0-2 of bayesm
, the fractional weight parameter has been changed to a weight between 0 and 1.
is the fractional weight on the normalized pooled likelihood. This differs from what is in Rossi et al chapter 5, i.e.
Large R
values may be required (>20,000).
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 5, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=10000} else {R=10} set.seed(66) p = 3 # num of choice alterns ncoef = 3 nlgt = 300 # num of cross sectional units nz = 2 Z = matrix(runif(nz*nlgt),ncol=nz) Z = t(t(Z) - apply(Z,2,mean)) # demean Z ncomp = 3 # num of mixture components Delta = matrix(c(1,0,1,0,1,2),ncol=2) comps=NULL comps[[1]] = list(mu=c(0,-1,-2), rooti=diag(rep(1,3))) comps[[2]] = list(mu=c(0,-1,-2)*2, rooti=diag(rep(1,3))) comps[[3]] = list(mu=c(0,-1,-2)*4, rooti=diag(rep(1,3))) pvec = c(0.4, 0.2, 0.4) ## simulate from MNL model conditional on X matrix simmnlwX= function(n,X,beta) { k = length(beta) Xbeta = X%*%beta j = nrow(Xbeta) / n Xbeta = matrix(Xbeta, byrow=TRUE, ncol=j) Prob = exp(Xbeta) iota = c(rep(1,j)) denom = Prob%*%iota Prob = Prob/as.vector(denom) y = vector("double",n) ind = 1:j for (i in 1:n) { yvec = rmultinom(1, 1, Prob[i,]) y[i] = ind%*%yvec } return(list(y=y, X=X, beta=beta, prob=Prob)) } ## simulate data simlgtdata = NULL ni = rep(50, 300) for (i in 1:nlgt) { betai = Delta%*%Z[i,] + as.vector(rmixture(1,pvec,comps)$x) Xa = matrix(runif(ni[i]*p,min=-1.5,max=0), ncol=p) X = createX(p, na=1, nd=NULL, Xa=Xa, Xd=NULL, base=1) outa = simmnlwX(ni[i], X, betai) simlgtdata[[i]] = list(y=outa$y, X=X, beta=betai) } ## plot betas if(0){ bmat = matrix(0, nlgt, ncoef) for(i in 1:nlgt) {bmat[i,] = simlgtdata[[i]]$beta} par(mfrow = c(ncoef,1)) for(i in 1:ncoef) { hist(bmat[,i], breaks=30, col="magenta") } } ## set parms for priors and Z Prior1 = list(ncomp=5) keep = 5 Mcmc1 = list(R=R, keep=keep) Data1 = list(p=p, lgtdata=simlgtdata, Z=Z) ## fit model without sign constraints out1 = rhierMnlRwMixture(Data=Data1, Prior=Prior1, Mcmc=Mcmc1) cat("Summary of Delta draws", fill=TRUE) summary(out1$Deltadraw, tvalues=as.vector(Delta)) cat("Summary of Normal Mixture Distribution", fill=TRUE) summary(out1$nmix) ## plotting examples if(0) { plot(out1$betadraw) plot(out1$nmix) } ## fit model with constraint that beta_i2 < 0 forall i Prior2 = list(ncomp=5, SignRes=c(0,-1,0)) out2 = rhierMnlRwMixture(Data=Data1, Prior=Prior2, Mcmc=Mcmc1) cat("Summary of Delta draws", fill=TRUE) summary(out2$Deltadraw, tvalues=as.vector(Delta)) cat("Summary of Normal Mixture Distribution", fill=TRUE) summary(out2$nmix) ## plotting examples if(0) { plot(out2$betadraw) plot(out2$nmix) }
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=10000} else {R=10} set.seed(66) p = 3 # num of choice alterns ncoef = 3 nlgt = 300 # num of cross sectional units nz = 2 Z = matrix(runif(nz*nlgt),ncol=nz) Z = t(t(Z) - apply(Z,2,mean)) # demean Z ncomp = 3 # num of mixture components Delta = matrix(c(1,0,1,0,1,2),ncol=2) comps=NULL comps[[1]] = list(mu=c(0,-1,-2), rooti=diag(rep(1,3))) comps[[2]] = list(mu=c(0,-1,-2)*2, rooti=diag(rep(1,3))) comps[[3]] = list(mu=c(0,-1,-2)*4, rooti=diag(rep(1,3))) pvec = c(0.4, 0.2, 0.4) ## simulate from MNL model conditional on X matrix simmnlwX= function(n,X,beta) { k = length(beta) Xbeta = X%*%beta j = nrow(Xbeta) / n Xbeta = matrix(Xbeta, byrow=TRUE, ncol=j) Prob = exp(Xbeta) iota = c(rep(1,j)) denom = Prob%*%iota Prob = Prob/as.vector(denom) y = vector("double",n) ind = 1:j for (i in 1:n) { yvec = rmultinom(1, 1, Prob[i,]) y[i] = ind%*%yvec } return(list(y=y, X=X, beta=beta, prob=Prob)) } ## simulate data simlgtdata = NULL ni = rep(50, 300) for (i in 1:nlgt) { betai = Delta%*%Z[i,] + as.vector(rmixture(1,pvec,comps)$x) Xa = matrix(runif(ni[i]*p,min=-1.5,max=0), ncol=p) X = createX(p, na=1, nd=NULL, Xa=Xa, Xd=NULL, base=1) outa = simmnlwX(ni[i], X, betai) simlgtdata[[i]] = list(y=outa$y, X=X, beta=betai) } ## plot betas if(0){ bmat = matrix(0, nlgt, ncoef) for(i in 1:nlgt) {bmat[i,] = simlgtdata[[i]]$beta} par(mfrow = c(ncoef,1)) for(i in 1:ncoef) { hist(bmat[,i], breaks=30, col="magenta") } } ## set parms for priors and Z Prior1 = list(ncomp=5) keep = 5 Mcmc1 = list(R=R, keep=keep) Data1 = list(p=p, lgtdata=simlgtdata, Z=Z) ## fit model without sign constraints out1 = rhierMnlRwMixture(Data=Data1, Prior=Prior1, Mcmc=Mcmc1) cat("Summary of Delta draws", fill=TRUE) summary(out1$Deltadraw, tvalues=as.vector(Delta)) cat("Summary of Normal Mixture Distribution", fill=TRUE) summary(out1$nmix) ## plotting examples if(0) { plot(out1$betadraw) plot(out1$nmix) } ## fit model with constraint that beta_i2 < 0 forall i Prior2 = list(ncomp=5, SignRes=c(0,-1,0)) out2 = rhierMnlRwMixture(Data=Data1, Prior=Prior2, Mcmc=Mcmc1) cat("Summary of Delta draws", fill=TRUE) summary(out2$Deltadraw, tvalues=as.vector(Delta)) cat("Summary of Normal Mixture Distribution", fill=TRUE) summary(out2$nmix) ## plotting examples if(0) { plot(out2$betadraw) plot(out2$nmix) }
rhierNegbinRw
implements an MCMC algorithm for the hierarchical Negative Binomial (NBD) regression model. Metropolis steps for each unit-level set of regression parameters are automatically tuned by optimization. Over-dispersion parameter (alpha) is common across units.
rhierNegbinRw(Data, Prior, Mcmc)
rhierNegbinRw(Data, Prior, Mcmc)
Data |
list(regdata, Z) |
Prior |
list(Deltabar, Adelta, nu, V, a, b) |
Mcmc |
list(R, keep, nprint, s_beta, s_alpha, alpha, c, Vbeta0, Delta0) |
NBD(mean=
, over-dispersion=alpha)
(unless
Mcmc$alpha
specified)
Note: prior mean of , variance
Data = list(regdata, Z)
[Z
optional]
regdata: |
list of lists with data on each of nreg units |
regdata[[i]]$X: |
matrix of variables |
regdata[[i]]$y: |
vector of count responses |
Z: |
matrix of unit characteristics (def: vector of ones)
|
Prior = list(Deltabar, Adelta, nu, V, a, b)
[optional]
Deltabar: |
prior mean matrix (def: 0) |
Adelta: |
PDS prior precision matrix (def: 0.01*I) |
nu: |
d.f. parameter for Inverted Wishart prior (def: nvar+3) |
V: |
location matrix of Inverted Wishart prior (def: nu*I) |
a: |
Gamma prior parameter (def: 0.5) |
b: |
Gamma prior parameter (def: 0.1) |
Mcmc = list(R, keep, nprint, s_beta, s_alpha, alpha, c, Vbeta0, Delta0)
[only R
required]
R: |
number of MCMC draws |
keep: |
MCMC thinning parameter -- 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) |
s_beta: |
scaling for beta | alpha RW inc cov (def: 2.93/sqrt(nvar) ) |
s_alpha: |
scaling for alpha | beta RW inc cov (def: 2.93) |
alpha: |
over-dispersion parameter (def: alpha ~ Gamma(a,b)) |
c: |
fractional likelihood weighting parm (def: 2) |
Vbeta0: |
starting value for Vbeta (def: I) |
Delta0: |
starting value for Delta (def: 0) |
A list containing:
llike |
|
betadraw |
|
alphadraw |
|
acceptrbeta |
acceptance rate of the beta draws |
acceptralpha |
acceptance rate of the alpha draws |
The NBD regression encompasses Poisson regression in the sense that as alpha goes to infinity the NBD distribution tends to the Poisson.
For "small" values of alpha, the dependent variable can be extremely variable so that a large number of observations may be required to obtain precise inferences.
For ease of interpretation, we recommend demeaning variables.
Sridhar Narayanan (Stanford GSB) and Peter Rossi (Anderson School, UCLA), [email protected].
For further discussion, see Chapter 5, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) # Simulate from the Negative Binomial Regression simnegbin = function(X, beta, alpha) { lambda = exp(X%*%beta) y = NULL for (j in 1:length(lambda)) {y = c(y, rnbinom(1, mu=lambda[j], size=alpha)) } return(y) } nreg = 100 # Number of cross sectional units T = 50 # Number of observations per unit nobs = nreg*T nvar = 2 # Number of X variables nz = 2 # Number of Z variables ## Construct the Z matrix Z = cbind(rep(1,nreg), rnorm(nreg,mean=1,sd=0.125)) Delta = cbind(c(4,2), c(0.1,-1)) alpha = 5 Vbeta = rbind(c(2,1), c(1,2)) ## Construct the regdata (containing X) simnegbindata = NULL for (i in 1:nreg) { betai = as.vector(Z[i,]%*%Delta) + chol(Vbeta)%*%rnorm(nvar) X = cbind(rep(1,T),rnorm(T,mean=2,sd=0.25)) simnegbindata[[i]] = list(y=simnegbin(X,betai,alpha), X=X, beta=betai) } Beta = NULL for (i in 1:nreg) {Beta = rbind(Beta,matrix(simnegbindata[[i]]$beta,nrow=1))} Data1 = list(regdata=simnegbindata, Z=Z) Mcmc1 = list(R=R) out = rhierNegbinRw(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)])) cat("Summary of alpha draws", fill=TRUE) summary(out$alpha, tvalues=alpha) ## plotting examples if(0){ plot(out$betadraw) plot(out$alpha,tvalues=alpha) plot(out$Deltadraw,tvalues=as.vector(Delta)) }
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) # Simulate from the Negative Binomial Regression simnegbin = function(X, beta, alpha) { lambda = exp(X%*%beta) y = NULL for (j in 1:length(lambda)) {y = c(y, rnbinom(1, mu=lambda[j], size=alpha)) } return(y) } nreg = 100 # Number of cross sectional units T = 50 # Number of observations per unit nobs = nreg*T nvar = 2 # Number of X variables nz = 2 # Number of Z variables ## Construct the Z matrix Z = cbind(rep(1,nreg), rnorm(nreg,mean=1,sd=0.125)) Delta = cbind(c(4,2), c(0.1,-1)) alpha = 5 Vbeta = rbind(c(2,1), c(1,2)) ## Construct the regdata (containing X) simnegbindata = NULL for (i in 1:nreg) { betai = as.vector(Z[i,]%*%Delta) + chol(Vbeta)%*%rnorm(nvar) X = cbind(rep(1,T),rnorm(T,mean=2,sd=0.25)) simnegbindata[[i]] = list(y=simnegbin(X,betai,alpha), X=X, beta=betai) } Beta = NULL for (i in 1:nreg) {Beta = rbind(Beta,matrix(simnegbindata[[i]]$beta,nrow=1))} Data1 = list(regdata=simnegbindata, Z=Z) Mcmc1 = list(R=R) out = rhierNegbinRw(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)])) cat("Summary of alpha draws", fill=TRUE) summary(out$alpha, tvalues=alpha) ## plotting examples if(0){ plot(out$betadraw) plot(out$alpha,tvalues=alpha) plot(out$Deltadraw,tvalues=as.vector(Delta)) }
rivDP
is a Gibbs Sampler for a linear structural equation with an arbitrary number of instruments. rivDP
uses a mixture-of-normals for the structural and reduced form equations implemented with a Dirichlet Process prior.
rivDP(Data, Prior, Mcmc)
rivDP(Data, Prior, Mcmc)
Data |
list(y, x, w, z) |
Prior |
list(md, Ad, mbg, Abg, lambda, Prioralpha, lambda_hyper) |
Mcmc |
list(R, keep, nprint, maxuniq, SCALE, gridsize) |
where
represents
Note: Error terms have non-zero means.
DO NOT include intercepts in the or
matrices.
This is different from
rivGibbs
which requires intercepts to be included explicitly.
where and
are set using the arguments in the reference below.
It is highly recommended that you use the default values for the hyperparameters of the prior on alpha.
is the natural conjugate prior for
:
and
These parameters are collected together in the list .
It is highly recommended that you use the default settings for these hyper-parameters.
uniform[alim[1], alimb[2]]
dim(data)-1 + exp(z)
uniform[dim(data)-1+nulim[1], nulim[2]]
uniform[vlim[1], vlim[2]]
Data = list(y, x, w, z)
y: |
vector of obs on LHS variable in structural equation |
x: |
vector of obs on "endogenous" variable in structural equation |
w: |
matrix of obs on "exogenous" variables in the structural equation |
z: |
matrix of obs on instruments
|
Prior = list(md, Ad, mbg, Abg, lambda, Prioralpha, lambda_hyper)
[optional]
md: |
-length prior mean of delta (def: 0) |
Ad: |
PDS prior precision matrix for prior on delta (def: 0.01*I) |
mbg: |
-length prior mean vector for prior on beta,gamma (def: 0) |
Abg: |
PDS prior precision matrix for prior on beta,gamma (def: 0.01*I) |
Prioralpha: |
list(Istarmin, Istarmax, power) |
$Istarmin: |
is expected number of components at lower bound of support of alpha (def: 1) |
$Istarmax: |
is expected number of components at upper bound of support of alpha (def: floor(0.1*length(y)) ) |
$power: |
is the power parameter for alpha prior (def: 0.8) |
lambda_hyper: |
list(alim, nulim, vlim) |
$alim: |
defines support of a distribution (def: c(0.01, 10) ) |
$nulim: |
defines support of nu distribution (def: c(0.01, 3) ) |
$vlim: |
defines support of v distribution (def: c(0.1, 4) )
|
Mcmc = list(R, keep, nprint, maxuniq, SCALE, gridsize)
[only R
required]
R: |
number of MCMC draws |
keep: |
MCMC thinning parameter: keep every keepth draw (def: 1) |
nprint: |
print the estimated time remaining for every nprint'th draw (def: 100, set to 0 for no print) |
maxuniq: |
storage constraint on the number of unique components (def: 200) |
SCALE: |
scale data (def: TRUE ) |
gridsize: |
gridsize parameter for alpha draws (def: 20) |
nmix
Detailsnmix
is a list with 3 components. Several functions in the bayesm
package that involve a Dirichlet Process or mixture-of-normals return nmix
. Across these functions, a common structure is used for nmix
in order to utilize generic summary and plotting functions.
probdraw: |
matrix that reports the probability that each draw came from a particular component (here, a one-column matrix of 1s) |
zdraw: |
matrix that indicates which component each draw is assigned to (here, null) |
compdraw: |
A list of lists of lists. Each of the inner-most lists has 2 elemens: a vector of draws for mu and a matrix of draws for the Cholesky root of Sigma .
|
A list containing:
deltadraw |
|
betadraw |
|
alphadraw |
|
Istardraw |
|
gammadraw |
|
nmix |
a list containing: |
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see "A Semi-Parametric Bayesian Approach to the Instrumental Variable Problem," by Conley, Hansen, McCulloch and Rossi, Journal of Econometrics (2008).
See also, Chapter 4, Bayesian Non- and Semi-parametric Methods and Applications by Peter Rossi.
rivGibbs
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) ## simulate scaled log-normal errors and run k = 10 delta = 1.5 Sigma = matrix(c(1, 0.6, 0.6, 1), ncol=2) N = 1000 tbeta = 4 scalefactor = 0.6 root = chol(scalefactor*Sigma) mu = c(1,1) ## compute interquartile ranges ninterq = qnorm(0.75) - qnorm(0.25) error = matrix(rnorm(100000*2), ncol=2)%*%root error = t(t(error)+mu) Err = t(t(exp(error))-exp(mu+0.5*scalefactor*diag(Sigma))) lnNinterq = quantile(Err[,1], prob=0.75) - quantile(Err[,1], prob=0.25) ## simulate data error = matrix(rnorm(N*2), ncol=2)%*%root error = t(t(error)+mu) Err = t(t(exp(error))-exp(mu+0.5*scalefactor*diag(Sigma))) ## scale appropriately Err[,1] = Err[,1]*ninterq/lnNinterq Err[,2] = Err[,2]*ninterq/lnNinterq z = matrix(runif(k*N), ncol=k) x = z%*%(delta*c(rep(1,k))) + Err[,1] y = x*tbeta + Err[,2] ## specify data input and mcmc parameters Data = list(); Data$z = z Data$x = x Data$y = y Mcmc = list() Mcmc$maxuniq = 100 Mcmc$R = R end = Mcmc$R out = rivDP(Data=Data, Mcmc=Mcmc) cat("Summary of Beta draws", fill=TRUE) summary(out$betadraw, tvalues=tbeta) ## plotting examples if(0){ plot(out$betadraw, tvalues=tbeta) plot(out$nmix) # plot "fitted" density of the errors }
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) ## simulate scaled log-normal errors and run k = 10 delta = 1.5 Sigma = matrix(c(1, 0.6, 0.6, 1), ncol=2) N = 1000 tbeta = 4 scalefactor = 0.6 root = chol(scalefactor*Sigma) mu = c(1,1) ## compute interquartile ranges ninterq = qnorm(0.75) - qnorm(0.25) error = matrix(rnorm(100000*2), ncol=2)%*%root error = t(t(error)+mu) Err = t(t(exp(error))-exp(mu+0.5*scalefactor*diag(Sigma))) lnNinterq = quantile(Err[,1], prob=0.75) - quantile(Err[,1], prob=0.25) ## simulate data error = matrix(rnorm(N*2), ncol=2)%*%root error = t(t(error)+mu) Err = t(t(exp(error))-exp(mu+0.5*scalefactor*diag(Sigma))) ## scale appropriately Err[,1] = Err[,1]*ninterq/lnNinterq Err[,2] = Err[,2]*ninterq/lnNinterq z = matrix(runif(k*N), ncol=k) x = z%*%(delta*c(rep(1,k))) + Err[,1] y = x*tbeta + Err[,2] ## specify data input and mcmc parameters Data = list(); Data$z = z Data$x = x Data$y = y Mcmc = list() Mcmc$maxuniq = 100 Mcmc$R = R end = Mcmc$R out = rivDP(Data=Data, Mcmc=Mcmc) cat("Summary of Beta draws", fill=TRUE) summary(out$betadraw, tvalues=tbeta) ## plotting examples if(0){ plot(out$betadraw, tvalues=tbeta) plot(out$nmix) # plot "fitted" density of the errors }
rivGibbs
is a Gibbs Sampler for a linear structural equation with an arbitrary number of instruments.
rivGibbs(Data, Prior, Mcmc)
rivGibbs(Data, Prior, Mcmc)
Data |
list(y, x, w, z) |
Prior |
list(md, Ad, mbg, Abg, nu, V) |
Mcmc |
list(R, keep, nprint) |
Note: if intercepts are desired in either equation, include vector of ones in or
Data = list(y, x, w, z)
y: |
vector of obs on LHS variable in structural equation |
x: |
vector of obs on "endogenous" variable in structural equation |
w: |
matrix of obs on "exogenous" variables in the structural equation |
z: |
matrix of obs on instruments
|
Prior = list(md, Ad, mbg, Abg, nu, V)
[optional]
md: |
-length prior mean of delta (def: 0) |
Ad: |
PDS prior precision matrix for prior on delta (def: 0.01*I) |
mbg: |
-length prior mean vector for prior on beta,gamma (def: 0) |
Abg: |
PDS prior precision matrix for prior on beta,gamma (def: 0.01*I) |
nu: |
d.f. parameter for Inverted Wishart prior on Sigma (def: 5) |
V: |
location matrix for Inverted Wishart prior on Sigma (def: nu*I)
|
Mcmc = list(R, keep, nprint)
[only R
required]
R: |
number of MCMC draws |
keep: |
MCMC thinning parameter: 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)
|
A list containing:
deltadraw |
|
betadraw |
|
gammadraw |
|
Sigmadraw |
|
Rob McCulloch and Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 5, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) simIV = function(delta, beta, Sigma, n, z, w, gamma) { eps = matrix(rnorm(2*n),ncol=2) %*% chol(Sigma) x = z%*%delta + eps[,1] y = beta*x + eps[,2] + w%*%gamma list(x=as.vector(x), y=as.vector(y)) } n = 200 p=1 # number of instruments z = cbind(rep(1,n), matrix(runif(n*p),ncol=p)) w = matrix(1,n,1) rho = 0.8 Sigma = matrix(c(1,rho,rho,1), ncol=2) delta = c(1,4) beta = 0.5 gamma = c(1) simiv = simIV(delta, beta, Sigma, n, z, w, gamma) Data1 = list(); Data1$z = z; Data1$w=w; Data1$x=simiv$x; Data1$y=simiv$y Mcmc1=list(); Mcmc1$R = R; Mcmc1$keep=1 out = rivGibbs(Data=Data1, Mcmc=Mcmc1) cat("Summary of Beta draws", fill=TRUE) summary(out$betadraw, tvalues=beta) cat("Summary of Sigma draws", fill=TRUE) summary(out$Sigmadraw, tvalues=as.vector(Sigma[upper.tri(Sigma,diag=TRUE)])) ## plotting examples if(0){plot(out$betadraw)}
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) simIV = function(delta, beta, Sigma, n, z, w, gamma) { eps = matrix(rnorm(2*n),ncol=2) %*% chol(Sigma) x = z%*%delta + eps[,1] y = beta*x + eps[,2] + w%*%gamma list(x=as.vector(x), y=as.vector(y)) } n = 200 p=1 # number of instruments z = cbind(rep(1,n), matrix(runif(n*p),ncol=p)) w = matrix(1,n,1) rho = 0.8 Sigma = matrix(c(1,rho,rho,1), ncol=2) delta = c(1,4) beta = 0.5 gamma = c(1) simiv = simIV(delta, beta, Sigma, n, z, w, gamma) Data1 = list(); Data1$z = z; Data1$w=w; Data1$x=simiv$x; Data1$y=simiv$y Mcmc1=list(); Mcmc1$R = R; Mcmc1$keep=1 out = rivGibbs(Data=Data1, Mcmc=Mcmc1) cat("Summary of Beta draws", fill=TRUE) summary(out$betadraw, tvalues=beta) cat("Summary of Sigma draws", fill=TRUE) summary(out$Sigmadraw, tvalues=as.vector(Sigma[upper.tri(Sigma,diag=TRUE)])) ## plotting examples if(0){plot(out$betadraw)}
rmixGibbs
makes one draw using the Gibbs Sampler for a mixture of multivariate normals. rmixGibbs
is not designed to be called directly. Instead, use rnmixGibbs
wrapper function.
rmixGibbs(y, Bbar, A, nu, V, a, p, z)
rmixGibbs(y, Bbar, A, nu, V, a, p, z)
y |
data array where rows are obs |
Bbar |
prior mean for mean vector of each norm comp |
A |
prior precision parameter |
nu |
prior d.f. parm |
V |
prior location matrix for covariance prior |
a |
Dirichlet prior parms |
p |
prior prob of each mixture component |
z |
component identities for each observation – "indicators" |
a list containing:
p |
draw of mixture probabilities |
z |
draw of indicators of each component |
comps |
new draw of normal component parameters |
This routine is a utility routine that does not check the input arguments for proper dimensions and type.
Rob McCulloch (Arizona State University) and Peter Rossi (Anderson School, UCLA), [email protected].
For further discussion, see Chapter 5 Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
rmixture
simulates iid draws from a Multivariate Mixture of Normals
rmixture(n, pvec, comps)
rmixture(n, pvec, comps)
n |
number of observations |
pvec |
|
comps |
list of mixture component parameters |
comps
is a list of length ncomp
with ncomp = length(pvec)
. comps[[j]][[1]]
is mean vector for the th component.
comps[[j]][[2]]
is the inverse of the cholesky root of for
th component
A list containing:
x: |
an |
z: |
an |
This routine is a utility routine that does not check the input arguments for proper dimensions and type.
Peter Rossi, Anderson School, UCLA, [email protected].
rmnlIndepMetrop
implements Independence Metropolis algorithm for the multinomial logit (MNL) model.
rmnlIndepMetrop(Data, Prior, Mcmc)
rmnlIndepMetrop(Data, Prior, Mcmc)
Data |
list(y, X, p) |
Prior |
list(A, betabar) |
Mcmc |
list(R, keep, nprint, nu) |
y MNL(X,
)
Data = list(y, X, p)
y: |
vector of multinomial outcomes (1, ..., p) |
X: |
matrix |
p: |
number of alternatives |
Prior = list(A, betabar)
[optional]
A: |
prior precision matrix (def: 0.01*I) |
betabar: |
prior mean (def: 0)
|
Mcmc = list(R, keep, nprint, nu)
[only R
required]
R: |
number of MCMC draws |
keep: |
MCMC thinning parameter -- 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) |
nu: |
d.f. parameter for independent t density (def: 6) |
A list containing:
betadraw |
|
loglike |
|
acceptr |
acceptance rate of Metropolis draws |
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 3, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) simmnl = function(p, n, beta) { ## note: create X array with 2 alt.spec vars k = length(beta) X1 = matrix(runif(n*p,min=-1,max=1), ncol=p) X2 = matrix(runif(n*p,min=-1,max=1), ncol=p) X = createX(p, na=2, nd=NULL, Xd=NULL, Xa=cbind(X1,X2), base=1) Xbeta = X%*%beta ## now do probs p = nrow(Xbeta) / n Xbeta = matrix(Xbeta, byrow=TRUE, ncol=p) Prob = exp(Xbeta) iota = c(rep(1,p)) denom = Prob%*%iota Prob = Prob / as.vector(denom) ## draw y y = vector("double",n) ind = 1:p for (i in 1:n) { yvec = rmultinom(1, 1, Prob[i,]) y[i] = ind%*%yvec } return(list(y=y, X=X, beta=beta, prob=Prob)) } n = 200 p = 3 beta = c(1, -1, 1.5, 0.5) simout = simmnl(p,n,beta) Data1 = list(y=simout$y, X=simout$X, p=p) Mcmc1 = list(R=R, keep=1) out = rmnlIndepMetrop(Data=Data1, Mcmc=Mcmc1) cat("Summary of beta draws", fill=TRUE) summary(out$betadraw, tvalues=beta) ## plotting examples if(0){plot(out$betadraw)}
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) simmnl = function(p, n, beta) { ## note: create X array with 2 alt.spec vars k = length(beta) X1 = matrix(runif(n*p,min=-1,max=1), ncol=p) X2 = matrix(runif(n*p,min=-1,max=1), ncol=p) X = createX(p, na=2, nd=NULL, Xd=NULL, Xa=cbind(X1,X2), base=1) Xbeta = X%*%beta ## now do probs p = nrow(Xbeta) / n Xbeta = matrix(Xbeta, byrow=TRUE, ncol=p) Prob = exp(Xbeta) iota = c(rep(1,p)) denom = Prob%*%iota Prob = Prob / as.vector(denom) ## draw y y = vector("double",n) ind = 1:p for (i in 1:n) { yvec = rmultinom(1, 1, Prob[i,]) y[i] = ind%*%yvec } return(list(y=y, X=X, beta=beta, prob=Prob)) } n = 200 p = 3 beta = c(1, -1, 1.5, 0.5) simout = simmnl(p,n,beta) Data1 = list(y=simout$y, X=simout$X, p=p) Mcmc1 = list(R=R, keep=1) out = rmnlIndepMetrop(Data=Data1, Mcmc=Mcmc1) cat("Summary of beta draws", fill=TRUE) summary(out$betadraw, tvalues=beta) ## plotting examples if(0){plot(out$betadraw)}
rmnpGibbs
implements the McCulloch/Rossi Gibbs Sampler for the multinomial probit model.
rmnpGibbs(Data, Prior, Mcmc)
rmnpGibbs(Data, Prior, Mcmc)
Data |
list(y, X, p) |
Prior |
list(betabar, A, nu, V) |
Mcmc |
list(R, keep, nprint, beta0, sigma0) |
with
.
Note:
and
are
.
if
for
and
where
means elements of
other than the
th.
, if all
is not identified. However,
/sqrt(
) and
/
are identified. See reference or example below for details.
Data = list(y, X, p)
y: |
vector of multinomial outcomes (1, ..., p) |
X: |
design matrix. To make matrix use createX with DIFF=TRUE |
p: |
number of alternatives |
Prior = list(betabar, A, nu, V)
[optional]
betabar: |
prior mean (def: 0) |
A: |
prior precision matrix (def: 0.01*I) |
nu: |
d.f. parameter for Inverted Wishart prior (def: (p-1)+3) |
V: |
PDS location parameter for Inverted Wishart prior (def: nu*I) |
Mcmc = list(R, keep, nprint, beta0, sigma0)
[only R
required]
R: |
number of MCMC draws |
keep: |
MCMC thinning parameter -- 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) |
beta0: |
initial value for beta (def: 0) |
sigma0: |
initial value for sigma (def: I) |
A list containing:
betadraw |
|
sigmadraw |
|
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 4, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) simmnp = function(X, p, n, beta, sigma) { indmax = function(x) {which(max(x)==x)} Xbeta = X%*%beta w = as.vector(crossprod(chol(sigma),matrix(rnorm((p-1)*n),ncol=n))) + Xbeta w = matrix(w, ncol=(p-1), byrow=TRUE) maxw = apply(w, 1, max) y = apply(w, 1, indmax) y = ifelse(maxw < 0, p, y) return(list(y=y, X=X, beta=beta, sigma=sigma)) } p = 3 n = 500 beta = c(-1,1,1,2) Sigma = matrix(c(1, 0.5, 0.5, 1), ncol=2) k = length(beta) X1 = matrix(runif(n*p,min=0,max=2),ncol=p) X2 = matrix(runif(n*p,min=0,max=2),ncol=p) X = createX(p, na=2, nd=NULL, Xa=cbind(X1,X2), Xd=NULL, DIFF=TRUE, base=p) simout = simmnp(X,p,500,beta,Sigma) Data1 = list(p=p, y=simout$y, X=simout$X) Mcmc1 = list(R=R, keep=1) out = rmnpGibbs(Data=Data1, Mcmc=Mcmc1) cat(" Summary of Betadraws ", fill=TRUE) betatilde = out$betadraw / sqrt(out$sigmadraw[,1]) attributes(betatilde)$class = "bayesm.mat" summary(betatilde, tvalues=beta) cat(" Summary of Sigmadraws ", fill=TRUE) sigmadraw = out$sigmadraw / out$sigmadraw[,1] attributes(sigmadraw)$class = "bayesm.var" summary(sigmadraw, tvalues=as.vector(Sigma[upper.tri(Sigma,diag=TRUE)])) ## plotting examples if(0){plot(betatilde,tvalues=beta)}
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) simmnp = function(X, p, n, beta, sigma) { indmax = function(x) {which(max(x)==x)} Xbeta = X%*%beta w = as.vector(crossprod(chol(sigma),matrix(rnorm((p-1)*n),ncol=n))) + Xbeta w = matrix(w, ncol=(p-1), byrow=TRUE) maxw = apply(w, 1, max) y = apply(w, 1, indmax) y = ifelse(maxw < 0, p, y) return(list(y=y, X=X, beta=beta, sigma=sigma)) } p = 3 n = 500 beta = c(-1,1,1,2) Sigma = matrix(c(1, 0.5, 0.5, 1), ncol=2) k = length(beta) X1 = matrix(runif(n*p,min=0,max=2),ncol=p) X2 = matrix(runif(n*p,min=0,max=2),ncol=p) X = createX(p, na=2, nd=NULL, Xa=cbind(X1,X2), Xd=NULL, DIFF=TRUE, base=p) simout = simmnp(X,p,500,beta,Sigma) Data1 = list(p=p, y=simout$y, X=simout$X) Mcmc1 = list(R=R, keep=1) out = rmnpGibbs(Data=Data1, Mcmc=Mcmc1) cat(" Summary of Betadraws ", fill=TRUE) betatilde = out$betadraw / sqrt(out$sigmadraw[,1]) attributes(betatilde)$class = "bayesm.mat" summary(betatilde, tvalues=beta) cat(" Summary of Sigmadraws ", fill=TRUE) sigmadraw = out$sigmadraw / out$sigmadraw[,1] attributes(sigmadraw)$class = "bayesm.var" summary(sigmadraw, tvalues=as.vector(Sigma[upper.tri(Sigma,diag=TRUE)])) ## plotting examples if(0){plot(betatilde,tvalues=beta)}
rmultireg
draws from the posterior of a Multivariate Regression model with a natural conjugate prior.
rmultireg(Y, X, Bbar, A, nu, V)
rmultireg(Y, X, Bbar, A, nu, V)
Y |
|
X |
|
Bbar |
|
A |
|
nu |
d.f. parameter for Sigma |
V |
|
Model: with
is
matrix of coefficients;
is
covariance matrix.
Priors: |
;
IW(nu, V)
A list of the components of a draw from the posterior
B |
draw of regression coefficient matrix |
Sigma |
draw of Sigma |
This routine is a utility routine that does not check the input arguments for proper dimensions and type.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 2, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) n =200 m = 2 X = cbind(rep(1,n),runif(n)) k = ncol(X) B = matrix(c(1,2,-1,3), ncol=m) Sigma = matrix(c(1, 0.5, 0.5, 1), ncol=m) RSigma = chol(Sigma) Y = X%*%B + matrix(rnorm(m*n),ncol=m)%*%RSigma betabar = rep(0,k*m) Bbar = matrix(betabar, ncol=m) A = diag(rep(0.01,k)) nu = 3 V = nu*diag(m) betadraw = matrix(double(R*k*m), ncol=k*m) Sigmadraw = matrix(double(R*m*m), ncol=m*m) for (rep in 1:R) { out = rmultireg(Y, X, Bbar, A, nu, V) betadraw[rep,] = out$B Sigmadraw[rep,] = out$Sigma } cat(" Betadraws ", fill=TRUE) mat = apply(betadraw, 2, quantile, probs=c(0.01, 0.05, 0.5, 0.95, 0.99)) mat = rbind(as.vector(B),mat) rownames(mat)[1] = "beta" print(mat) cat(" Sigma draws", fill=TRUE) mat = apply(Sigmadraw, 2 ,quantile, probs=c(0.01, 0.05, 0.5, 0.95, 0.99)) mat = rbind(as.vector(Sigma),mat); rownames(mat)[1]="Sigma" print(mat)
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) n =200 m = 2 X = cbind(rep(1,n),runif(n)) k = ncol(X) B = matrix(c(1,2,-1,3), ncol=m) Sigma = matrix(c(1, 0.5, 0.5, 1), ncol=m) RSigma = chol(Sigma) Y = X%*%B + matrix(rnorm(m*n),ncol=m)%*%RSigma betabar = rep(0,k*m) Bbar = matrix(betabar, ncol=m) A = diag(rep(0.01,k)) nu = 3 V = nu*diag(m) betadraw = matrix(double(R*k*m), ncol=k*m) Sigmadraw = matrix(double(R*m*m), ncol=m*m) for (rep in 1:R) { out = rmultireg(Y, X, Bbar, A, nu, V) betadraw[rep,] = out$B Sigmadraw[rep,] = out$Sigma } cat(" Betadraws ", fill=TRUE) mat = apply(betadraw, 2, quantile, probs=c(0.01, 0.05, 0.5, 0.95, 0.99)) mat = rbind(as.vector(B),mat) rownames(mat)[1] = "beta" print(mat) cat(" Sigma draws", fill=TRUE) mat = apply(Sigmadraw, 2 ,quantile, probs=c(0.01, 0.05, 0.5, 0.95, 0.99)) mat = rbind(as.vector(Sigma),mat); rownames(mat)[1]="Sigma" print(mat)
rmvpGibbs
implements the Edwards/Allenby Gibbs Sampler for the multivariate probit model.
rmvpGibbs(Data, Prior, Mcmc)
rmvpGibbs(Data, Prior, Mcmc)
Data |
list(y, X, p) |
Prior |
list(betabar, A, nu, V) |
Mcmc |
list(R, keep, nprint, beta0 ,sigma0) |
with
N(0,
). Note:
is
.
if
, else
.
beta and Sigma are not identifed. Correlation matrix and the betas divided by the appropriate standard deviation are. See reference or example below for details.
To make matrix use
createX
Data = list(y, X, p)
X: |
Design Matrix |
y: |
vector of 0/1 outcomes |
p: |
dimension of multivariate probit |
Prior = list(betabar, A, nu, V)
[optional]
betabar: |
prior mean (def: 0) |
A: |
prior precision matrix (def: 0.01*I) |
nu: |
d.f. parameter for Inverted Wishart prior (def: (p-1)+3) |
V: |
PDS location parameter for Inverted Wishart prior (def: nu*I) |
Mcmc = list(R, keep, nprint, beta0 ,sigma0)
[only R
required]
R: |
number of MCMC draws |
keep: |
MCMC thinning parameter -- 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) |
beta0: |
initial value for beta |
sigma0: |
initial value for sigma |
A list containing:
betadraw |
|
sigmadraw |
|
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 4, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) simmvp = function(X, p, n, beta, sigma) { w = as.vector(crossprod(chol(sigma),matrix(rnorm(p*n),ncol=n))) + X%*%beta y = ifelse(w<0, 0, 1) return(list(y=y, X=X, beta=beta, sigma=sigma)) } p = 3 n = 500 beta = c(-2,0,2) Sigma = matrix(c(1, 0.5, 0.5, 0.5, 1, 0.5, 0.5, 0.5, 1), ncol=3) k = length(beta) I2 = diag(rep(1,p)) xadd = rbind(I2) for(i in 2:n) { xadd=rbind(xadd,I2) } X = xadd simout = simmvp(X,p,500,beta,Sigma) Data1 = list(p=p, y=simout$y, X=simout$X) Mcmc1 = list(R=R, keep=1) out = rmvpGibbs(Data=Data1, Mcmc=Mcmc1) ind = seq(from=0, by=p, length=k) inda = 1:3 ind = ind + inda cat(" Betadraws ", fill=TRUE) betatilde = out$betadraw / sqrt(out$sigmadraw[,ind]) attributes(betatilde)$class = "bayesm.mat" summary(betatilde, tvalues=beta/sqrt(diag(Sigma))) rdraw = matrix(double((R)*p*p), ncol=p*p) rdraw = t(apply(out$sigmadraw, 1, nmat)) attributes(rdraw)$class = "bayesm.var" tvalue = nmat(as.vector(Sigma)) dim(tvalue) = c(p,p) tvalue = as.vector(tvalue[upper.tri(tvalue,diag=TRUE)]) cat(" Draws of Correlation Matrix ", fill=TRUE) summary(rdraw, tvalues=tvalue) ## plotting examples if(0){plot(betatilde, tvalues=beta/sqrt(diag(Sigma)))}
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) simmvp = function(X, p, n, beta, sigma) { w = as.vector(crossprod(chol(sigma),matrix(rnorm(p*n),ncol=n))) + X%*%beta y = ifelse(w<0, 0, 1) return(list(y=y, X=X, beta=beta, sigma=sigma)) } p = 3 n = 500 beta = c(-2,0,2) Sigma = matrix(c(1, 0.5, 0.5, 0.5, 1, 0.5, 0.5, 0.5, 1), ncol=3) k = length(beta) I2 = diag(rep(1,p)) xadd = rbind(I2) for(i in 2:n) { xadd=rbind(xadd,I2) } X = xadd simout = simmvp(X,p,500,beta,Sigma) Data1 = list(p=p, y=simout$y, X=simout$X) Mcmc1 = list(R=R, keep=1) out = rmvpGibbs(Data=Data1, Mcmc=Mcmc1) ind = seq(from=0, by=p, length=k) inda = 1:3 ind = ind + inda cat(" Betadraws ", fill=TRUE) betatilde = out$betadraw / sqrt(out$sigmadraw[,ind]) attributes(betatilde)$class = "bayesm.mat" summary(betatilde, tvalues=beta/sqrt(diag(Sigma))) rdraw = matrix(double((R)*p*p), ncol=p*p) rdraw = t(apply(out$sigmadraw, 1, nmat)) attributes(rdraw)$class = "bayesm.var" tvalue = nmat(as.vector(Sigma)) dim(tvalue) = c(p,p) tvalue = as.vector(tvalue[upper.tri(tvalue,diag=TRUE)]) cat(" Draws of Correlation Matrix ", fill=TRUE) summary(rdraw, tvalues=tvalue) ## plotting examples if(0){plot(betatilde, tvalues=beta/sqrt(diag(Sigma)))}
rmvst
draws from a multivariate student-t distribution.
rmvst(nu, mu, root)
rmvst(nu, mu, root)
nu |
d.f. parameter |
mu |
mean vector |
root |
Upper Tri Cholesky Root of Sigma |
length(mu) draw vector
This routine is a utility routine that does not check the input arguments for proper dimensions and type.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
set.seed(66) rmvst(nu=5, mu=c(rep(0,2)), root=chol(matrix(c(2,1,1,2), ncol=2)))
set.seed(66) rmvst(nu=5, mu=c(rep(0,2)), root=chol(matrix(c(2,1,1,2), ncol=2)))
rnegbinRw
implements a Random Walk Metropolis Algorithm for the Negative Binomial (NBD) regression model where and
are drawn with two different random walks.
rnegbinRw(Data, Prior, Mcmc)
rnegbinRw(Data, Prior, Mcmc)
Data |
list(y, X) |
Prior |
list(betabar, A, a, b) |
Mcmc |
list(R, keep, nprint, s_beta, s_alpha, beta0, alpha) |
(unless
Mcmc$alpha
specified)
Note: prior mean of ,
Data = list(y, X)
y: |
vector of counts ( ) |
X: |
design matrix
|
Prior = list(betabar, A, a, b)
[optional]
betabar: |
prior mean (def: 0) |
A: |
PDS prior precision matrix (def: 0.01*I) |
a: |
Gamma prior parameter (not used if Mcmc$alpha specified) (def: 0.5) |
b: |
Gamma prior parameter (not used if Mcmc$alpha specified) (def: 0.1)
|
Mcmc = list(R, keep, nprint, s_beta, s_alpha, beta0, alpha)
[only R
required]
R: |
number of MCMC draws |
keep: |
MCMC thinning parameter -- 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) |
s_beta: |
scaling for beta | alpha RW inc cov matrix (def: 2.93/sqrt(k) ) |
s_alpha: |
scaling for alpha | beta RW inc cov matrix (def: 2.93) |
alpha: |
over-dispersion parameter (def: alpha ~ Gamma(a,b)) |
A list containing:
betadraw |
|
alphadraw |
|
llike |
|
acceptrbeta |
acceptance rate of the beta draws |
acceptralpha |
acceptance rate of the alpha draws |
The NBD regression encompasses Poisson regression in the sense that as alpha goes to infinity the NBD distribution tends toward the Poisson. For "small" values of alpha, the dependent variable can be extremely variable so that a large number of observations may be required to obtain precise inferences.
Sridhar Narayanan (Stanford GSB) and Peter Rossi (Anderson School, UCLA), [email protected].
For further discussion, see Bayesian Statistics and Marketing by Rossi, Allenby, McCulloch.
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=1000} else {R=10} set.seed(66) simnegbin = function(X, beta, alpha) { # Simulate from the Negative Binomial Regression lambda = exp(X%*%beta) y = NULL for (j in 1:length(lambda)) { y = c(y, rnbinom(1, mu=lambda[j], size=alpha)) } return(y) } nobs = 500 nvar = 2 # Number of X variables alpha = 5 Vbeta = diag(nvar)*0.01 # Construct the regdata (containing X) simnegbindata = NULL beta = c(0.6, 0.2) X = cbind(rep(1,nobs), rnorm(nobs,mean=2,sd=0.5)) simnegbindata = list(y=simnegbin(X,beta,alpha), X=X, beta=beta) Data1 = simnegbindata Mcmc1 = list(R=R) out = rnegbinRw(Data=Data1, Mcmc=list(R=R)) cat("Summary of alpha/beta draw", fill=TRUE) summary(out$alphadraw, tvalues=alpha) summary(out$betadraw, tvalues=beta) ## plotting examples if(0){plot(out$betadraw)}
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=1000} else {R=10} set.seed(66) simnegbin = function(X, beta, alpha) { # Simulate from the Negative Binomial Regression lambda = exp(X%*%beta) y = NULL for (j in 1:length(lambda)) { y = c(y, rnbinom(1, mu=lambda[j], size=alpha)) } return(y) } nobs = 500 nvar = 2 # Number of X variables alpha = 5 Vbeta = diag(nvar)*0.01 # Construct the regdata (containing X) simnegbindata = NULL beta = c(0.6, 0.2) X = cbind(rep(1,nobs), rnorm(nobs,mean=2,sd=0.5)) simnegbindata = list(y=simnegbin(X,beta,alpha), X=X, beta=beta) Data1 = simnegbindata Mcmc1 = list(R=R) out = rnegbinRw(Data=Data1, Mcmc=list(R=R)) cat("Summary of alpha/beta draw", fill=TRUE) summary(out$alphadraw, tvalues=alpha) summary(out$betadraw, tvalues=beta) ## plotting examples if(0){plot(out$betadraw)}
rnmixGibbs
implements a Gibbs Sampler for normal mixtures.
rnmixGibbs(Data, Prior, Mcmc)
rnmixGibbs(Data, Prior, Mcmc)
Data |
list(y) |
Prior |
list(Mubar, A, nu, V, a, ncomp) |
Mcmc |
list(R, keep, nprint, Loglike) |
ind iid multinomial(p) with
an
vector of probs
with
Note: this is the natural conjugate prior – a special case of multivariate regression
Dirchlet(a)
Data = list(y)
y: |
matrix of data (rows are obs)
|
Prior = list(Mubar, A, nu, V, a, ncomp)
[only ncomp
required]
Mubar: |
vector with prior mean of normal component means (def: 0) |
A: |
precision parameter for prior on mean of normal component (def: 0.01) |
nu: |
d.f. parameter for prior on Sigma (normal component cov matrix) (def: k+3) |
V: |
location matrix of IW prior on Sigma (def: nu*I) |
a: |
vector of Dirichlet prior parameters (def: rep(5,ncomp) ) |
ncomp: |
number of normal components to be included |
Mcmc = list(R, keep, nprint, Loglike)
[only R
required]
R: |
number of MCMC draws |
keep: |
MCMC thinning parameter -- 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) |
LogLike: |
logical flag for whether to compute the log-likelihood (def: FALSE )
|
nmix
Detailsnmix
is a list with 3 components. Several functions in the bayesm
package that involve a Dirichlet Process or mixture-of-normals return nmix
. Across these functions, a common structure is used for nmix
in order to utilize generic summary and plotting functions.
probdraw: |
matrix that reports the probability that each draw came from a particular component |
zdraw: |
matrix that indicates which component each draw is assigned to |
compdraw: |
A list of lists of lists. Each of the inner-most lists has 2 elemens: a vector of draws for mu and a matrix of draws for the Cholesky root of Sigma .
|
A list containing:
ll |
|
nmix |
a list containing: |
In this model, the component normal parameters are not-identified due to label-switching. However, the fitted mixture of normals density is identified as it is invariant to label-switching. See chapter 5 of Rossi et al below for details.
Use eMixMargDen
or momMix
to compute posterior expectation or distribution of various identified parameters.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 3, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
rmixture
, rmixGibbs
,eMixMargDen
, momMix
,
mixDen
, mixDenBi
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) dim = 5 k = 3 # dimension of simulated data and number of "true" components sigma = matrix(rep(0.5,dim^2), nrow=dim) diag(sigma) = 1 sigfac = c(1,1,1) mufac = c(1,2,3) compsmv = list() for(i in 1:k) compsmv[[i]] = list(mu=mufac[i]*1:dim, sigma=sigfac[i]*sigma) # change to "rooti" scale comps = list() for(i in 1:k) comps[[i]] = list(mu=compsmv[[i]][[1]], rooti=solve(chol(compsmv[[i]][[2]]))) pvec = (1:k) / sum(1:k) nobs = 500 dm = rmixture(nobs, pvec, comps) Data1 = list(y=dm$x) ncomp = 9 Prior1 = list(ncomp=ncomp) Mcmc1 = list(R=R, keep=1) out = rnmixGibbs(Data=Data1, Prior=Prior1, Mcmc=Mcmc1) cat("Summary of Normal Mixture Distribution", fill=TRUE) summary(out$nmix) tmom = momMix(matrix(pvec,nrow=1), list(comps)) mat = rbind(tmom$mu, tmom$sd) cat(" True Mean/Std Dev", fill=TRUE) print(mat) ## plotting examples if(0){plot(out$nmix,Data=dm$x)}
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) dim = 5 k = 3 # dimension of simulated data and number of "true" components sigma = matrix(rep(0.5,dim^2), nrow=dim) diag(sigma) = 1 sigfac = c(1,1,1) mufac = c(1,2,3) compsmv = list() for(i in 1:k) compsmv[[i]] = list(mu=mufac[i]*1:dim, sigma=sigfac[i]*sigma) # change to "rooti" scale comps = list() for(i in 1:k) comps[[i]] = list(mu=compsmv[[i]][[1]], rooti=solve(chol(compsmv[[i]][[2]]))) pvec = (1:k) / sum(1:k) nobs = 500 dm = rmixture(nobs, pvec, comps) Data1 = list(y=dm$x) ncomp = 9 Prior1 = list(ncomp=ncomp) Mcmc1 = list(R=R, keep=1) out = rnmixGibbs(Data=Data1, Prior=Prior1, Mcmc=Mcmc1) cat("Summary of Normal Mixture Distribution", fill=TRUE) summary(out$nmix) tmom = momMix(matrix(pvec,nrow=1), list(comps)) mat = rbind(tmom$mu, tmom$sd) cat(" True Mean/Std Dev", fill=TRUE) print(mat) ## plotting examples if(0){plot(out$nmix,Data=dm$x)}
rordprobitGibbs
implements a Gibbs Sampler for the ordered probit model with a RW Metropolis step for the cut-offs.
rordprobitGibbs(Data, Prior, Mcmc)
rordprobitGibbs(Data, Prior, Mcmc)
Data |
list(y, X, k) |
Prior |
list(betabar, A, dstarbar, Ad) |
Mcmc |
list(R, keep, nprint, s) |
with
if c[k]
c[k+1] with
cutoffs = {c[1], , c[k+1]}
Be careful in assessing prior parameter Ad
: 0.1 is too small for many applications.
Data = list(y, X, k)
y: |
vector of observations, ( ) |
X: |
Design Matrix |
k: |
the largest possible value of y |
Prior = list(betabar, A, dstarbar, Ad)
[optional]
betabar: |
prior mean (def: 0) |
A: |
prior precision matrix (def: 0.01*I) |
dstarbar: |
prior mean, where (def: 0) |
Ad: |
prior precision matrix (def: I)
|
Mcmc = list(R, keep, nprint, s)
[only R
required]
R: |
number of MCMC draws |
keep: |
MCMC thinning parameter -- 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) |
s: |
scaling parameter for RW Metropolis (def: 2.93/sqrt(p) )
|
A list containing:
betadraw |
|
cutdraw |
|
dstardraw |
|
accept |
acceptance rate of Metropolis draws for cut-offs |
set c[1] = -100 and c[k+1] = 100. c[2] is set to 0 for identification.
The relationship between cut-offs and dstar is:
c[3] = exp(dstar[1]),
c[4] = c[3] + exp(dstar[2]), ...,
c[k] = c[k-1] + exp(dstar[k-2])
Peter Rossi, Anderson School, UCLA, [email protected].
Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) ## simulate data for ordered probit model simordprobit=function(X, betas, cutoff){ z = X%*%betas + rnorm(nobs) y = cut(z, br = cutoff, right=TRUE, include.lowest = TRUE, labels = FALSE) return(list(y = y, X = X, k=(length(cutoff)-1), betas= betas, cutoff=cutoff )) } nobs = 300 X = cbind(rep(1,nobs),runif(nobs, min=0, max=5),runif(nobs,min=0, max=5)) k = 5 betas = c(0.5, 1, -0.5) cutoff = c(-100, 0, 1.0, 1.8, 3.2, 100) simout = simordprobit(X, betas, cutoff) Data=list(X=simout$X, y=simout$y, k=k) ## set Mcmc for ordered probit model Mcmc = list(R=R) out = rordprobitGibbs(Data=Data, Mcmc=Mcmc) cat(" ", fill=TRUE) cat("acceptance rate= ", accept=out$accept, fill=TRUE) ## outputs of betadraw and cut-off draws cat(" Summary of betadraws", fill=TRUE) summary(out$betadraw, tvalues=betas) cat(" Summary of cut-off draws", fill=TRUE) summary(out$cutdraw, tvalues=cutoff[2:k]) ## plotting examples if(0){plot(out$cutdraw)}
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) ## simulate data for ordered probit model simordprobit=function(X, betas, cutoff){ z = X%*%betas + rnorm(nobs) y = cut(z, br = cutoff, right=TRUE, include.lowest = TRUE, labels = FALSE) return(list(y = y, X = X, k=(length(cutoff)-1), betas= betas, cutoff=cutoff )) } nobs = 300 X = cbind(rep(1,nobs),runif(nobs, min=0, max=5),runif(nobs,min=0, max=5)) k = 5 betas = c(0.5, 1, -0.5) cutoff = c(-100, 0, 1.0, 1.8, 3.2, 100) simout = simordprobit(X, betas, cutoff) Data=list(X=simout$X, y=simout$y, k=k) ## set Mcmc for ordered probit model Mcmc = list(R=R) out = rordprobitGibbs(Data=Data, Mcmc=Mcmc) cat(" ", fill=TRUE) cat("acceptance rate= ", accept=out$accept, fill=TRUE) ## outputs of betadraw and cut-off draws cat(" Summary of betadraws", fill=TRUE) summary(out$betadraw, tvalues=betas) cat(" Summary of cut-off draws", fill=TRUE) summary(out$cutdraw, tvalues=cutoff[2:k]) ## plotting examples if(0){plot(out$cutdraw)}
rscaleUsage
implements an MCMC algorithm for multivariate ordinal data with scale usage heterogeniety.
rscaleUsage(Data, Prior, Mcmc)
rscaleUsage(Data, Prior, Mcmc)
Data |
list(x, k) |
Prior |
list(nu, V, mubar, Am, gs, Lambdanu, LambdaV) |
Mcmc |
list(R, keep, nprint, ndghk, e, y, mu, Sigma, sigma, tau, Lambda) |
=
nrow(x)
individuals respond to =
ncol(x)
questions;
all questions are on a scale for respondent
and question
,
if
where
and
with
unif on a grid
It is highly recommended that the user choose the default prior settings. If you wish to change prior settings and/or the grids used, please carefully read the case study listed in the reference below.
Data = list(x, k)
x: |
matrix of discrete responses |
k: |
number of discrete rating scale options |
Prior = list(nu, V, mubar, Am, gs, Lambdanu, LambdaV)
[optional]
nu: |
d.f. parameter for Sigma prior (def: p + 3) |
V: |
scale location matrix for Sigma prior (def: nu*I) |
mubar: |
vector of prior means (def: rep(k/2,p) ) |
Am: |
prior precision matrix (def: 0.01*I) |
gs: |
grid size for sigma (def: 100) |
Lambdanu: |
d.f. parameter for Lambda prior (def: 20) |
LambdaV: |
scale location matrix for Lambda prior (def: (Lambdanu - 3)*Lambda) |
Mcmc = list(R, keep, nprint, ndghk, e, y, mu, Sigma, sigma, tau, Lambda)
[only R
required]
R: |
number of MCMC draws (def: 1000) |
keep: |
MCMC thinning parameter -- 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) |
ndghk: |
number of draws for a GHK integration (def: 100) |
e: |
initial value (def: 0) |
y: |
initial values (def: x) |
mu: |
initial values (def: apply(y,2,mean) , a p-length vector) |
Sigma: |
initial value (def: var(y) ) |
sigma: |
initial values (def: rep(1,n) ) |
tau: |
initial values (def: rep(0,n) ) |
Lambda: |
initial values (def: matrix(c(4,0,0,.5),ncol=2) )
|
A list containing:
Sigmadraw |
|
mudraw |
|
taudraw |
|
sigmadraw |
|
Lambdadraw |
|
edraw |
|
,
are identified from the scale usage patterns in the
questions asked per respondent (# cols of
). Do not attempt to use this on datasets with only a small number of total questions.
Rob McCulloch (Arizona State University) and Peter Rossi (Anderson School, UCLA), [email protected].
For further discussion, see Case Study 3 on Overcoming Scale Usage Heterogeneity, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=1000} else {R=5} set.seed(66) data(customerSat) surveydat = list(k=10, x=as.matrix(customerSat)) out = rscaleUsage(Data=surveydat, Mcmc=list(R=R)) summary(out$mudraw)
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=1000} else {R=5} set.seed(66) data(customerSat) surveydat = list(k=10, x=as.matrix(customerSat)) out = rscaleUsage(Data=surveydat, Mcmc=list(R=R)) summary(out$mudraw)
rsurGibbs
implements a Gibbs Sampler to draw from the posterior of the Seemingly Unrelated Regression (SUR) Model of Zellner.
rsurGibbs(Data, Prior, Mcmc)
rsurGibbs(Data, Prior, Mcmc)
Data |
list(regdata) |
Prior |
list(betabar, A, nu, V) |
Mcmc |
list(R, keep) |
with
for
regressions
()'
with
Can be written as a stacked model: where
is a
vector and
=
length(beta)
= sum(length(beta_i))
Note: must have the same number of observations () in each equation but can have a different number of
variables (
) for each equation where
.
Data = list(regdata)
regdata: |
list of lists, regdata[[i]] = list(y=y_i, X=X_i) , where y_i is and X_i is
|
Prior = list(betabar, A, nu, V)
[optional]
betabar: |
prior mean (def: 0) |
A: |
prior precision matrix (def: 0.01*I) |
nu: |
d.f. parameter for Inverted Wishart prior (def: m+3) |
V: |
scale parameter for Inverted Wishart prior (def: nu*I)
|
Mcmc = list(R, keep)
[only R
required]
R:
|
number of MCMC draws |
keep:
|
MCMC thinning parameter -- 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)
|
A list containing:
betadraw |
|
Sigmadraw |
|
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 3, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=1000} else {R=10} set.seed(66) ## simulate data from SUR beta1 = c(1,2) beta2 = c(1,-1,-2) nobs = 100 nreg = 2 iota = c(rep(1, nobs)) X1 = cbind(iota, runif(nobs)) X2 = cbind(iota, runif(nobs), runif(nobs)) Sigma = matrix(c(0.5, 0.2, 0.2, 0.5), ncol=2) U = chol(Sigma) E = matrix(rnorm(2*nobs),ncol=2)%*%U y1 = X1%*%beta1 + E[,1] y2 = X2%*%beta2 + E[,2] ## run Gibbs Sampler regdata = NULL regdata[[1]] = list(y=y1, X=X1) regdata[[2]] = list(y=y2, X=X2) out = rsurGibbs(Data=list(regdata=regdata), Mcmc=list(R=R)) cat("Summary of beta draws", fill=TRUE) summary(out$betadraw, tvalues=c(beta1,beta2)) cat("Summary of Sigmadraws", fill=TRUE) summary(out$Sigmadraw, tvalues=as.vector(Sigma[upper.tri(Sigma,diag=TRUE)])) ## plotting examples if(0){plot(out$betadraw, tvalues=c(beta1,beta2))}
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=1000} else {R=10} set.seed(66) ## simulate data from SUR beta1 = c(1,2) beta2 = c(1,-1,-2) nobs = 100 nreg = 2 iota = c(rep(1, nobs)) X1 = cbind(iota, runif(nobs)) X2 = cbind(iota, runif(nobs), runif(nobs)) Sigma = matrix(c(0.5, 0.2, 0.2, 0.5), ncol=2) U = chol(Sigma) E = matrix(rnorm(2*nobs),ncol=2)%*%U y1 = X1%*%beta1 + E[,1] y2 = X2%*%beta2 + E[,2] ## run Gibbs Sampler regdata = NULL regdata[[1]] = list(y=y1, X=X1) regdata[[2]] = list(y=y2, X=X2) out = rsurGibbs(Data=list(regdata=regdata), Mcmc=list(R=R)) cat("Summary of beta draws", fill=TRUE) summary(out$betadraw, tvalues=c(beta1,beta2)) cat("Summary of Sigmadraws", fill=TRUE) summary(out$Sigmadraw, tvalues=as.vector(Sigma[upper.tri(Sigma,diag=TRUE)])) ## plotting examples if(0){plot(out$betadraw, tvalues=c(beta1,beta2))}
rtrun
draws from a truncated univariate normal distribution.
rtrun(mu, sigma, a, b)
rtrun(mu, sigma, a, b)
mu |
mean |
sigma |
standard deviation |
a |
lower bound |
b |
upper bound |
Note that due to the vectorization of the rnorm
and qnorm
commands in R, all arguments can be vectors of equal length. This makes the inverse CDF method the most efficient to use in R.
Draw (possibly a vector)
This routine is a utility routine that does not check the input arguments for proper dimensions and type.
**Note also that rtrun
is currently affected by the numerical accuracy of the inverse CDF function when trunctation points are far out in the tails of the distribution, where “far out” means and/or
. A fix will be implemented in a future version of
bayesm
.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 2, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
set.seed(66) rtrun(mu=c(rep(0,10)), sigma=c(rep(1,10)), a=c(rep(0,10)), b=c(rep(2,10)))
set.seed(66) rtrun(mu=c(rep(0,10)), sigma=c(rep(1,10)), a=c(rep(0,10)), b=c(rep(2,10)))
runireg
implements an iid sampler to draw from the posterior of a univariate regression with a conjugate prior.
runireg(Data, Prior, Mcmc)
runireg(Data, Prior, Mcmc)
Data |
list(y, X) |
Prior |
list(betabar, A, nu, ssq) |
Mcmc |
list(R, keep, nprint) |
with
Data = list(y, X)
y: |
vector of observations |
X: |
design matrix
|
Prior = list(betabar, A, nu, ssq)
[optional]
betabar: |
prior mean (def: 0) |
A: |
prior precision matrix (def: 0.01*I) |
nu: |
d.f. parameter for Inverted Chi-square prior (def: 3) |
ssq: |
scale parameter for Inverted Chi-square prior (def: var(y) )
|
Mcmc = list(R, keep, nprint)
[only R
required]
R: |
number of draws |
keep: |
thinning parameter -- 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)
|
A list containing:
betadraw |
|
sigmasqdraw |
|
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 2, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) n = 200 X = cbind(rep(1,n), runif(n)) beta = c(1,2) sigsq = 0.25 y = X%*%beta + rnorm(n,sd=sqrt(sigsq)) out = runireg(Data=list(y=y,X=X), Mcmc=list(R=R)) cat("Summary of beta and Sigmasq draws", fill=TRUE) summary(out$betadraw, tvalues=beta) summary(out$sigmasqdraw, tvalues=sigsq) ## plotting examples if(0){plot(out$betadraw)}
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=2000} else {R=10} set.seed(66) n = 200 X = cbind(rep(1,n), runif(n)) beta = c(1,2) sigsq = 0.25 y = X%*%beta + rnorm(n,sd=sqrt(sigsq)) out = runireg(Data=list(y=y,X=X), Mcmc=list(R=R)) cat("Summary of beta and Sigmasq draws", fill=TRUE) summary(out$betadraw, tvalues=beta) summary(out$sigmasqdraw, tvalues=sigsq) ## plotting examples if(0){plot(out$betadraw)}
runiregGibbs
implements a Gibbs Sampler to draw from posterior of a univariate regression with a conditionally conjugate prior.
runiregGibbs(Data, Prior, Mcmc)
runiregGibbs(Data, Prior, Mcmc)
Data |
list(y, X) |
Prior |
list(betabar, A, nu, ssq) |
Mcmc |
list(sigmasq, R, keep, nprint) |
with
Data = list(y, X)
y: |
vector of observations |
X: |
design matrix
|
Prior = list(betabar, A, nu, ssq)
[optional]
betabar: |
prior mean (def: 0) |
A: |
prior precision matrix (def: 0.01*I) |
nu: |
d.f. parameter for Inverted Chi-square prior (def: 3) |
ssq: |
scale parameter for Inverted Chi-square prior (def: var(y) )
|
Mcmc = list(sigmasq, R, keep, nprint)
[only R
required]
sigmasq: |
value for for first Gibbs sampler draw of | |
R: |
number of MCMC draws |
keep: |
MCMC thinning parameter -- 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)
|
A list containing:
betadraw |
|
sigmasqdraw |
|
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 3, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=1000} else {R=10} set.seed(66) n = 200 X = cbind(rep(1,n), runif(n)) beta = c(1,2) sigsq = 0.25 y = X%*%beta + rnorm(n,sd=sqrt(sigsq)) out = runiregGibbs(Data=list(y=y, X=X), Mcmc=list(R=R)) cat("Summary of beta and Sigmasq draws", fill=TRUE) summary(out$betadraw, tvalues=beta) summary(out$sigmasqdraw, tvalues=sigsq) ## plotting examples if(0){plot(out$betadraw)}
if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=1000} else {R=10} set.seed(66) n = 200 X = cbind(rep(1,n), runif(n)) beta = c(1,2) sigsq = 0.25 y = X%*%beta + rnorm(n,sd=sqrt(sigsq)) out = runiregGibbs(Data=list(y=y, X=X), Mcmc=list(R=R)) cat("Summary of beta and Sigmasq draws", fill=TRUE) summary(out$betadraw, tvalues=beta) summary(out$sigmasqdraw, tvalues=sigsq) ## plotting examples if(0){plot(out$betadraw)}
rwishart
draws from the Wishart and Inverted Wishart distributions.
rwishart(nu, V)
rwishart(nu, V)
nu |
d.f. parameter |
V |
pds location matrix |
In the parameterization used here,
with
.
If you want to use an Inverted Wishart prior, you must invert the location matrix
before calling rwishart
, e.g.
IW(nu ,V);
.
A list containing:
W: |
Wishart draw |
IW: |
Inverted Wishart draw |
C: |
Upper tri root of W |
CI: |
inv(C), |
This routine is a utility routine that does not check the input arguments for proper dimensions and type.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 2, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
set.seed(66) rwishart(5,diag(3))$IW
set.seed(66) rwishart(5,diag(3))$IW
Data from Simmons Survey. Brands used in last year for those respondents who report consuming scotch.
data(Scotch)
data(Scotch)
A data frame with 2218 observations on 21 brand variables.
All variables are numeric vectors that are coded 1 if consumed in last year, 0 if not.
Edwards, Yancy and Greg Allenby (2003), "Multivariate Analysis of Multiple Response Data," Journal of Marketing Research 40, 321–334.
Chapter 4, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
data(Scotch) cat(" Frequencies of Brands", fill=TRUE) mat = apply(as.matrix(Scotch), 2, mean) print(mat) ## use Scotch data to run Multivariate Probit Model if(0) { y = as.matrix(Scotch) p = ncol(y) n = nrow(y) dimnames(y) = NULL y = as.vector(t(y)) y = as.integer(y) I_p = diag(p) X = rep(I_p,n) X = matrix(X, nrow=p) X = t(X) R = 2000 Data = list(p=p, X=X, y=y) Mcmc = list(R=R) set.seed(66) out = rmvpGibbs(Data=Data, Mcmc=Mcmc) ind = (0:(p-1))*p + (1:p) cat(" Betadraws ", fill=TRUE) mat = apply(out$betadraw/sqrt(out$sigmadraw[,ind]), 2 , quantile, probs=c(0.01, 0.05, 0.5, 0.95, 0.99)) attributes(mat)$class = "bayesm.mat" summary(mat) rdraw = matrix(double((R)*p*p), ncol=p*p) rdraw = t(apply(out$sigmadraw, 1, nmat)) attributes(rdraw)$class = "bayesm.var" cat(" Draws of Correlation Matrix ", fill=TRUE) summary(rdraw) }
data(Scotch) cat(" Frequencies of Brands", fill=TRUE) mat = apply(as.matrix(Scotch), 2, mean) print(mat) ## use Scotch data to run Multivariate Probit Model if(0) { y = as.matrix(Scotch) p = ncol(y) n = nrow(y) dimnames(y) = NULL y = as.vector(t(y)) y = as.integer(y) I_p = diag(p) X = rep(I_p,n) X = matrix(X, nrow=p) X = t(X) R = 2000 Data = list(p=p, X=X, y=y) Mcmc = list(R=R) set.seed(66) out = rmvpGibbs(Data=Data, Mcmc=Mcmc) ind = (0:(p-1))*p + (1:p) cat(" Betadraws ", fill=TRUE) mat = apply(out$betadraw/sqrt(out$sigmadraw[,ind]), 2 , quantile, probs=c(0.01, 0.05, 0.5, 0.95, 0.99)) attributes(mat)$class = "bayesm.mat" summary(mat) rdraw = matrix(double((R)*p*p), ncol=p*p) rdraw = t(apply(out$sigmadraw, 1, nmat)) attributes(rdraw)$class = "bayesm.var" cat(" Draws of Correlation Matrix ", fill=TRUE) summary(rdraw) }
simnhlogit
simulates from the non-homothetic logit model.
simnhlogit(theta, lnprices, Xexpend)
simnhlogit(theta, lnprices, Xexpend)
theta |
coefficient vector |
lnprices |
|
Xexpend |
|
For details on parameterization, see llnhlogit
.
A list containing:
y |
|
Xexpend |
expenditure variables |
lnprices |
price array |
theta |
coefficients |
prob |
|
This routine is a utility routine that does not check the input arguments for proper dimensions and type.
Peter Rossi, Anderson School, UCLA, [email protected].
For further discussion, see Chapter 4, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
N = 1000 p = 3 k = 1 theta = c(rep(1,p), seq(from=-1,to=1,length=p), rep(2,k), 0.5) lnprices = matrix(runif(N*p), ncol=p) Xexpend = matrix(runif(N*k), ncol=k) simdata = simnhlogit(theta, lnprices, Xexpend)
N = 1000 p = 3 k = 1 theta = c(rep(1,p), seq(from=-1,to=1,length=p), rep(2,k), 0.5) lnprices = matrix(runif(N*p), ncol=p) Xexpend = matrix(runif(N*k), ncol=k) simdata = simnhlogit(theta, lnprices, Xexpend)
summary.bayesm.mat
is an S3 method to summarize marginal distributions given an array of draws
## S3 method for class 'bayesm.mat' summary(object, names, burnin = trunc(0.1 * nrow(X)), tvalues, QUANTILES = TRUE, TRAILER = TRUE,...)
## S3 method for class 'bayesm.mat' summary(object, names, burnin = trunc(0.1 * nrow(X)), tvalues, QUANTILES = TRUE, TRAILER = TRUE,...)
object |
|
names |
optional character vector of names for the columns of |
burnin |
number of draws to burn-in (def: |
tvalues |
optional vector of "true" values for use in simulation examples |
QUANTILES |
logical for should quantiles be displayed (def: |
TRAILER |
logical for should a trailer be displayed (def: |
... |
optional arguments for generic function |
Typically, summary.bayesm.nmix
will be invoked by a call to the generic summary function as in summary(object)
where object is of class bayesm.mat
. Mean, Std Dev, Numerical Standard error (of estimate of posterior mean), relative numerical efficiency (see numEff
), and effective sample size are displayed. If QUANTILES=TRUE
, quantiles of marginal distirbutions in the columns of are displayed.
summary.bayesm.mat
is also exported for direct use as a standard function, as in summary.bayesm.mat(matrix)
.
summary.bayesm.mat(matrix)
returns (invisibly) the array of the various summary statistics for further use. To assess this array usestats=summary(Drawmat)
.
Peter Rossi, Anderson School, UCLA, [email protected].
summary.bayesm.var
, summary.bayesm.nmix
## Not run: out=rmnpGibbs(Data,Prior,Mcmc); summary(out$betadraw)
## Not run: out=rmnpGibbs(Data,Prior,Mcmc); summary(out$betadraw)
summary.bayesm.nmix
is an S3 method to display summaries of the distribution implied
by draws of Normal Mixture Components. Posterior means and variance-covariance matrices are
displayed.
Note: 1st and 2nd moments may not be very interpretable for mixtures of normals. This summary function can take a minute or so. The current implementation is not efficient.
## S3 method for class 'bayesm.nmix' summary(object, names, burnin=trunc(0.1*nrow(probdraw)), ...)
## S3 method for class 'bayesm.nmix' summary(object, names, burnin=trunc(0.1*nrow(probdraw)), ...)
object |
an object of class |
names |
optional character vector of names fo reach dimension of the density |
burnin |
number of draws to burn-in (def: |
... |
parms to send to summary |
An object of class bayesm.nmix
is a list of three components:
a matrix of rows by dim of normal mix of mixture prob draws
not used
list of list of lists with draws of mixture comp parms
Peter Rossi, Anderson School, UCLA, [email protected].
summary.bayesm.mat
, summary.bayesm.var
## Not run: out=rnmix(Data,Prior,Mcmc); summary(out)
## Not run: out=rnmix(Data,Prior,Mcmc); summary(out)
summary.bayesm.var
is an S3 method to summarize marginal distributions given an array of draws
## S3 method for class 'bayesm.var' summary(object, names, burnin = trunc(0.1 * nrow(Vard)), tvalues, QUANTILES = FALSE , ...)
## S3 method for class 'bayesm.var' summary(object, names, burnin = trunc(0.1 * nrow(Vard)), tvalues, QUANTILES = FALSE , ...)
object |
|
names |
optional character vector of names for the columns of |
burnin |
number of draws to burn-in (def: |
tvalues |
optional vector of "true" values for use in simulation examples |
QUANTILES |
logical for should quantiles be displayed (def: |
... |
optional arguments for generic function |
Typically, summary.bayesm.var
will be invoked by a call to the generic summary function as in summary(object)
where object
is of class bayesm.var
. Mean, Std Dev, Numerical Standard error (of estimate of posterior mean), relative numerical efficiency (see numEff
), and effective sample size are displayed. If QUANTILES=TRUE
, quantiles of marginal distirbutions in the columns of Vard
are displayed.
Vard
is an array of draws of a covariance matrix stored as vectors. Each row is a different draw.
The posterior mean of the vector of standard deviations and the correlation matrix are also displayed
Peter Rossi, Anderson School, UCLA, [email protected].
summary.bayesm.mat
, summary.bayesm.nmix
## Not run: out=rmnpGibbs(Data,Prior,Mcmc); summary(out$sigmadraw)
## Not run: out=rmnpGibbs(Data,Prior,Mcmc); summary(out$sigmadraw)
Volume of canned tuna sales as well as a measure of display activity, log price, and log wholesale price. Weekly data aggregated to the chain level. This data is extracted from the Dominick's Finer Foods database maintained by the Kilts Center for Marketing at the University of Chicago's Booth School of Business. Brands are seven of the top 10 UPCs in the canned tuna product category.
data(tuna)
data(tuna)
A data frame with 338 observations on 30 variables.
...$WEEK |
a numeric vector |
...$MOVE# |
unit sales of brand # |
...$NSALE# |
a measure of display activity of brand # |
...$LPRICE# |
log of price of brand # |
...$LWHPRIC# |
log of wholesale price of brand # |
...$FULLCUST |
total customers visits |
The brands are:
1. | Star Kist 6 oz. |
2. | Chicken of the Sea 6 oz. |
3. | Bumble Bee Solid 6.12 oz. |
4. | Bumble Bee Chunk 6.12 oz. |
5. | Geisha 6 oz. |
6. | Bumble Bee Large Cans. |
7. | HH Chunk Lite 6.5 oz. |
Chevalier, Judith, Anil Kashyap, and Peter Rossi (2003), "Why Don't Prices Rise During Periods of Peak Demand? Evidence from Scanner Data," The American Economic Review , 93(1), 15–37.
Chapter 7, Bayesian Statistics and Marketing by Rossi, Allenby, and McCulloch.
data(tuna) cat(" Quantiles of sales", fill=TRUE) mat = apply(as.matrix(tuna[,2:5]), 2, quantile) print(mat) ## example of processing for use with rivGibbs if(0) { data(tuna) t = dim(tuna)[1] customers = tuna[,30] sales = tuna[,2:8] lnprice = tuna[,16:22] lnwhPrice = tuna[,23:29] share = sales/mean(customers) shareout = as.vector(1-rowSums(share)) lnprob = log(share/shareout) ## create w matrix I1 = as.matrix(rep(1,t)) I0 = as.matrix(rep(0,t)) intercept = rep(I1,4) brand1 = rbind(I1, I0, I0, I0) brand2 = rbind(I0, I1, I0, I0) brand3 = rbind(I0, I0, I1, I0) w = cbind(intercept, brand1, brand2, brand3) ## choose brand 1 to 4 y = as.vector(as.matrix(lnprob[,1:4])) X = as.vector(as.matrix(lnprice[,1:4])) lnwhPrice = as.vector(as.matrix(lnwhPrice[1:4])) z = cbind(w, lnwhPrice) Data = list(z=z, w=w, x=X, y=y) Mcmc = list(R=R, keep=1) set.seed(66) out = rivGibbs(Data=Data, Mcmc=Mcmc) cat(" betadraws ", fill=TRUE) summary(out$betadraw) ## plotting examples if(0){plot(out$betadraw)} }
data(tuna) cat(" Quantiles of sales", fill=TRUE) mat = apply(as.matrix(tuna[,2:5]), 2, quantile) print(mat) ## example of processing for use with rivGibbs if(0) { data(tuna) t = dim(tuna)[1] customers = tuna[,30] sales = tuna[,2:8] lnprice = tuna[,16:22] lnwhPrice = tuna[,23:29] share = sales/mean(customers) shareout = as.vector(1-rowSums(share)) lnprob = log(share/shareout) ## create w matrix I1 = as.matrix(rep(1,t)) I0 = as.matrix(rep(0,t)) intercept = rep(I1,4) brand1 = rbind(I1, I0, I0, I0) brand2 = rbind(I0, I1, I0, I0) brand3 = rbind(I0, I0, I1, I0) w = cbind(intercept, brand1, brand2, brand3) ## choose brand 1 to 4 y = as.vector(as.matrix(lnprob[,1:4])) X = as.vector(as.matrix(lnprice[,1:4])) lnwhPrice = as.vector(as.matrix(lnwhPrice[1:4])) z = cbind(w, lnwhPrice) Data = list(z=z, w=w, x=X, y=y) Mcmc = list(R=R, keep=1) set.seed(66) out = rivGibbs(Data=Data, Mcmc=Mcmc) cat(" betadraws ", fill=TRUE) summary(out$betadraw) ## plotting examples if(0){plot(out$betadraw)} }