Learn R Programming

DPpackage (version 1.1-0)

DPcdensity: 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,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 density estimate is evaluated.
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 p
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
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 specifie
data
data frame.
work.dir
working directory.

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:
  • nclusteran integer giving the number of clusters.
  • muclusa matrix of dimension (nobservations+2)*(nvariables) giving the means of the clusters (only the first ncluster are considered to start the chain).
  • sigmaclusa 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).
  • ssan interger vector defining to which of the ncluster clusters each observation belongs.
  • alphagiving the value of the precision parameter.
  • m1giving the mean of the normal components of the baseline distribution.
  • k0giving the scale parameter of the normal part of the baseline distribution.
  • psi1giving the scale matrix of the inverted-Wishart part of the baseline distribution.
  • zgiving 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.

Details

This generic function fits a Dirichlet process mixture of normal model (Escobar and West, 1995) for the conditional density estimation $f(y \mid 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 $y_i$ and $X_i$ be the response and the vector of predictors, respectively. Further, let $Z_i=(y_i,X_i)$. The model for the joint distribution of the response and predictors is as follows: $$Z_i | \mu_i, \Sigma_i \sim N(\mu_i,\Sigma_i), i=1,\ldots,n$$ $$(\mu_i,\Sigma_i) | G \sim G$$ $$G | \alpha, G_0 \sim DP(\alpha G_0)$$ where, the baseline distribution is the conjugate normal-inverted-Wishart, $$G_0 = N(\mu| m_1, (1/k_0) \Sigma) IW (\Sigma | \nu_1, \psi_1)$$ To complete the model specification, independent hyperpriors are assumed (optional), $$\alpha | a_0, b_0 \sim Gamma(a_0,b_0)$$ $$m_1 | m_2, s_2 \sim N(m_2,s_2)$$ $$k_0 | \tau_1, \tau_2 \sim Gamma(\tau_1/2,\tau_2/2)$$ $$\psi_1 | \nu_2, \psi_2 \sim IW(\nu_2,\psi_2)$$ Note that the inverted-Wishart prior is parametrized such that if $A \sim IW_q(\nu, \psi)$ then $E(A)= \psi^{-1}/(\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, $G_0$, 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.

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

Run this code
########################################################## 
    # 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

      prior<-list(a0=10,
                  b0=1,
                  nu1=4,
                  nu2=4,
                  s2=diag(10000,2),
                  m2=rep(0,2),
                  psiinv2=diag(10000,2),
                  tau1=2.01,
                  tau2=0.01)

    # Initial state
      state <- NULL

    # MCMC parameters

      mcmc <- list(nburn=5000,
                   nsave=10000,
                   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)


    # 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)

Run the code above in your browser using DataLab