R: Bayesian Semiparametric Conditional Density Estimation using...

DPcdensity {DPpackage} | R Documentation

## Bayesian Semiparametric Conditional Density Estimation using a DPM of normals

### Description

This function generates a posterior density sample for a Bayesian density regression model with continuous predictors using a Dirichlet process mixture of normals model.

### Usage

    DPcdensity(y,x,xpred,ngrid=100,grid=NULL,compute.band=FALSE,
               type.band="PD",prior,mcmc,state,status,
               data=sys.frame(sys.parent()),work.dir=NULL)

### Arguments

`y` | a vector giving the data from which the density estimate is to be computed.
`x` | a vector or matrix giving the continuous predictors of dimension `nrec` times `nx`.
`xpred` | a vector or matrix giving the values of the continuous predictors used for prediction.
`ngrid` | number of grid points where the conditional density estimate is evaluated. The default is 100.
`grid` | vector of grid points where the conditional density estimate is evaluated. The default value is NULL and the grid is chosen according to the range of the data.
`compute.band` | logical variable indicating whether the credible band for the conditional density and mean function must be computed.
`type.band` | string indication the type of credible band to be computed; if equal to "HPD" or "PD" then the 95 percent pointwise HPD or PD band is computed, respectively.
`prior` | a list giving the prior information. The list includes the following parameter: `a0` and `b0` giving the hyperparameters for prior distribution of the precision parameter of the Dirichlet process prior, `alpha` giving the value of the precision parameter (it must be specified if `a0` is missing, see details below), `nu2` and `psiinv2` giving the hyperparameters of the inverted Wishart prior distribution for the scale matrix, `Psi1`, of the inverted Wishart part of the baseline distribution, `tau1` and `tau2` giving the hyperparameters for the gamma prior distribution of the scale parameter `k0` of the normal part of the baseline distribution, `m2` and `s2` giving the mean and the covariance of the normal prior for the mean, `m1`, of the normal component of the baseline distribution, respectively, `nu1` and `psiinv1` (it must be specified if `nu2` is missing, see details below) giving the hyperparameters of the inverted Wishart part of the baseline distribution and, `m1` giving the mean of the normal part of the baseline distribution (it must be specified if `m2` is missing, see details below) and, `k0` giving the scale parameter of the normal part of the baseline distribution (it must be specified if `tau1` is missing, see details below). Notice that the dimension of the baseline measure includes the predictor and the response, i.e., `nx+1`.
`mcmc` | a list giving the MCMC parameters. The list must include the following integers: `nburn` giving the number of burn-in scans, `nskip` giving the thinning interval, `nsave` giving the total number of scans to be saved, and `ndisplay` giving the number of saved scans to be displayed on screen (the function reports on the screen when every `ndisplay` iterations have been carried out).
`state` | a list giving the current value of the parameters. This list is used if the current analysis is the continuation of a previous analysis.
`status` | a logical variable indicating whether this run is new (`TRUE`) or the continuation of a previous analysis (`FALSE`). In the latter case the current value of the parameters must be specified in the object `state`.
`data` | data frame.
`work.dir` | working directory.

### Details

This generic function fits a Dirichlet process mixture of normal model (Escobar and West, 1995) for the conditional density estimation f(y|x) as proposed by Muller, Erkanli and West (1996). They proposed to specify a Dirichlet process mixture of normals for the joint distribution of the response and predictors. Although in the original paper these authors focussed on the mean regression function, their method can be used to model the conditional density of the response giving the predictors in a semiparametric way. Indeed, their method is essentially a locally weighted mixture of normal regression models with weigths predictor-dependent.

Let yi and Xi be the response and the vector of predictors, respectively. Further, let Zi=(yi,Xi). The model for the joint distribution of the response and predictors is as follows:

Zi | mui, Sigmai ~ N(mui,Sigmai), i=1,…,n

(mui,Sigmai) | G ~ G

G | alpha, G0 ~ DP(alpha G0)

where, the baseline distribution is the conjugate normal-inverted-Wishart,

G0 = N(mu| m1, (1/k0) Sigma) IW (Sigma | nu1, psi1)

To complete the model specification, independent hyperpriors are assumed (optional),

alpha | a0, b0 ~ Gamma(a0,b0)

m1 | m2, s2 ~ N(m2,s2)

k0 | tau1, tau2 ~ Gamma(tau1/2,tau2/2)

psi1 | nu2, psi2 ~ IW(nu2,psi2)

Note that the inverted-Wishart prior is parametrized such that if A ~ IWq(nu, psi) then E(A)= psiinv/(nu-q-1).

To let part of the baseline distribution fixed at a particular value, set the corresponding hyperparameters of the prior distributions to NULL in the hyperprior specification of the model.

Although the baseline distribution, G0, is a conjugate prior in this model specification, the algorithms with auxiliary parameters described in Neal (2000) is adopted. Specifically, the algorithm 8 with m=1 of Neal (2000) is considered in the `DPcdensity` function.

### Value

An object of class `DPcdensity` representing the DP mixture of normals model fit. Generic functions such as `print`, `summary`, and `plot` have methods to show the results of the fit. The results include the baseline parameters, `alpha`, and the number of clusters.

The MCMC samples of the parameters and the errors in the model are stored in the object `thetasave`. The object is included in the list `save.state` and are matrices which can be analyzed directly by functions provided by the coda package.

The list `state` in the output object contains the current value of the parameters necessary to restart the analysis. If you want to specify different starting values to run multiple chains set `status=TRUE` and create the list state based on this starting values. In this case the list `state` must include the following objects:

`ncluster` | an integer giving the number of clusters.
`muclus` | a matrix of dimension (nobservations+2)*(nvariables) giving the means of the clusters (only the first `ncluster` are considered to start the chain).
`sigmaclus` | a matrix of dimension (nobservations+2)*( (nvariables)*((nvariables)+1)/2) giving the lower matrix of the covariance matrix of the clusters (only the first `ncluster` are considered to start the chain).
`ss` | an interger vector defining to which of the `ncluster` clusters each observation belongs.
`alpha` | giving the value of the precision parameter.
`m1` | giving the mean of the normal components of the baseline distribution.
`k0` | giving the scale parameter of the normal part of the baseline distribution.
`psi1` | giving the scale matrix of the inverted-Wishart part of the baseline distribution.
`z` | giving the matrix of response and predictors. This must be included if missing data (response and/or predictors) are present. Those are imputed during the MCMC.

### Author(s)

Alejandro Jara <atjara@uc.cl>

Tim Hanson <hansont@stat.sc.edu>

### References

Escobar, M.D. and West, M. (1995) Bayesian Density Estimation and Inference Using Mixtures. Journal of the American Statistical Association, 90: 577-588.

Muller, P., Erkanli, A. and West, M. (1996) Bayesian curve fitting using multivariate normal mixtures. Biometrika, 83: 67-79.

Neal, R. M. (2000). Markov Chain sampling methods for Dirichlet process mixture models. Journal of Computational and Graphical Statistics, 9: 249-265.

### See Also

`DPdensity`, `PTdensity`, `BDPdensity`

### Examples

    ## Not run:

        ##########################################################
        # Simulated data:
        # Here we replicate the results reported with
        # simulated data by Dunson, Pillai and Park (2007,
        # JRSS Ser. B, 69: 163-183, pag 177) where a different
        # approach is proposed.
        ##########################################################

          dtrue <- function(grid,x)
          {
              exp(-2*x)*dnorm(grid,mean=x,sd=sqrt(0.01))+
              (1-exp(-2*x))*dnorm(grid,mean=x^4,sd=sqrt(0.04))
          }

          nobs <- 500
          x <- runif(nobs)
          y1 <- x + rnorm(nobs, 0, sqrt(0.01))
          y2 <- x^4 + rnorm(nobs, 0, sqrt(0.04))

          u <- runif(nobs)
          prob <- exp(-2*x)
          y <- ifelse(u<prob,y1,y2)

        # Prior information
          w <- cbind(y,x)
          wbar <- apply(w,2,mean)
          wcov <- var(w)

          prior <- list(a0=10,
                        b0=1,
                        nu1=4,
                        nu2=4,
                        s2=0.5*wcov,
                        m2=wbar,
                        psiinv2=2*solve(wcov),
                        tau1=6.01,
                        tau2=2.01)

        # Initial state
          state <- NULL

        # MCMC parameters

          mcmc <- list(nburn=5000,
                       nsave=5000,
                       nskip=3,
                       ndisplay=100)

        # fitting the model
          xpred <- c(0.00,0.05,0.10,0.15,0.20,0.25,
                     0.30,0.35,0.40,0.45,0.49,0.55,
                     0.60,0.65,0.70,0.75,0.80,0.85,
                     0.88,0.95,1.00)

          fit <- DPcdensity(y=y,x=x,xpred=xpred,ngrid=100,
                            prior=prior,
                            mcmc=mcmc,
                            state=state,
                            status=TRUE,
                            compute.band=TRUE,type.band="PD")

        # true model and estimates
          par(mfrow=c(2,3))

          plot(fit$grid,fit$densp.h[3,],lwd=1,type="l",lty=2,
               main="x=0.10",xlab="values",ylab="density",ylim=c(0,4))
          lines(fit$grid,fit$densp.l[3,],lwd=1,type="l",lty=2)
          lines(fit$grid,fit$densp.m[3,],lwd=2,type="l",lty=1)
          lines(fit$grid,dtrue(fit$grid,xpred[3]),lwd=2,
                type="l",lty=1,col="red")

          plot(fit$grid,fit$densp.h[6,],lwd=1,type="l",lty=2,
               main="x=0.25",xlab="values",ylab="density",ylim=c(0,4))
          lines(fit$grid,fit$densp.l[6,],lwd=1,type="l",lty=2)
          lines(fit$grid,fit$densp.m[6,],lwd=2,type="l",lty=1)
          lines(fit$grid,dtrue(fit$grid,xpred[6]),lwd=2,
                type="l",lty=1,col="red")

          plot(fit$grid,fit$densp.h[11,],lwd=1,type="l",lty=2,
               main="x=0.49",xlab="values",ylab="density",ylim=c(0,4))
          lines(fit$grid,fit$densp.l[11,],lwd=1,type="l",lty=2)
          lines(fit$grid,fit$densp.m[11,],lwd=2,type="l",lty=1)
          lines(fit$grid,dtrue(fit$grid,xpred[11]),lwd=2,type="l",
                lty=1,col="red")

          plot(fit$grid,fit$densp.h[16,],lwd=1,type="l",lty=2,
               main="x=0.75",xlab="values",ylab="density",ylim=c(0,4))
          lines(fit$grid,fit$densp.l[16,],lwd=1,type="l",lty=2)
          lines(fit$grid,fit$densp.m[16,],lwd=2,type="l",lty=1)
          lines(fit$grid,dtrue(fit$grid,xpred[16]),lwd=2,type="l",
                lty=1,col="red")

          plot(fit$grid,fit$densp.h[19,],lwd=1,type="l",lty=2,
               main="x=0.75",xlab="values",ylab="density",ylim=c(0,4))
          lines(fit$grid,fit$densp.l[19,],lwd=1,type="l",lty=2)
          lines(fit$grid,fit$densp.m[19,],lwd=2,type="l",lty=1)
          lines(fit$grid,dtrue(fit$grid,xpred[19]),lwd=2,type="l",
                lty=1,col="red")

        # data and mean function
          plot(x,y,xlab="x",ylab="y",main="")
          lines(xpred,fit$meanfp.m,type="l",lwd=2,lty=1)
          lines(xpred,fit$meanfp.l,type="l",lwd=2,lty=2)
          lines(xpred,fit$meanfp.h,type="l",lwd=2,lty=2)

    ## End(Not run)

[Package DPpackage version 1.1-7.4 Index]