Learn R Programming

DPpackage (version 1.1-0)

DPdensity: Semiparametric Bayesian density estimation using a DPM of normals

Description

This function generates a posterior density sample for a Dirichlet process mixture of normals model.

Usage

DPdensity(y,ngrid=1000,grid=NULL,prior,mcmc,state,status,method="neal",
          data=sys.frame(sys.parent()),na.action=na.fail)

Arguments

y
a vector or matrix giving the data from which the density estimate is to be computed.
ngrid
number of grid points where the density estimate is evaluated. This is only used if dimension of y is lower or equal than 2. The default value is 1000.
grid
matrix of dimension ngrid*nvar of grid points where the density estimate is evaluated. This is only used if dimension of y is lower or equal than 2. The default value is NULL a
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
method
the method to be used. See Details.
data
data frame.
na.action
a function that indicates what should happen when the data contain NAs. The default action (na.fail) causes DPdensity to print an error message and terminate if there are

Value

  • An object of class DPdensity 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 function DPrandom can be used to extract the posterior mean of the subject-specific means and covariance matrices. The MCMC samples of the parameters and the errors in the model are stored in the object thetasave and randsave, respectively. Both objects are 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.

Details

This generic function fits a Dirichlet process mixture of normal model for density estimation (Escobar and West, 1995): $$y_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 MacEachern and Muller (1998) and Neal (2000) are adopted. Specifically, the no-gaps algorithm of MacEachern and Muller (1998), "no-gaps", and the algorithm 8 with $m=1$ of Neal (2000), "neal", are considered in the DPdensity function. The default method is the algorithm 8 of Neal.

References

Escobar, M.D. and West, M. (1995) Bayesian Density Estimation and Inference Using Mixtures. Journal of the American Statistical Association, 90: 577-588. MacEachern, S. N. and Muller, P. (1998) Estimating mixture of Dirichlet Process Models. Journal of Computational and Graphical Statistics, 7 (2): 223-338. Neal, R. M. (2000). Markov Chain sampling methods for Dirichlet process mixture models. Journal of Computational and Graphical Statistics, 9: 249-265.

See Also

DPrandom, PTdensity, BDPdensity

Examples

Run this code
####################################
    # Univariate example
    ####################################

    # Data
      data(galaxy)
      galaxy <- data.frame(galaxy,speeds=galaxy$speed/1000) 
      attach(galaxy)

    # Initial state
      state <- NULL

    # MCMC parameters

      nburn <- 1000
      nsave <- 10000
      nskip <- 10
      ndisplay <- 100
      mcmc <- list(nburn=nburn,nsave=nsave,nskip=nskip,ndisplay=ndisplay)

    # Example of Prior information 1
    # Fixing alpha, m1, and Psi1

      prior1 <- list(alpha=1,m1=rep(0,1),psiinv1=diag(0.5,1),nu1=4,tau1=1,tau2=100)


    # Example of Prior information 2
    # Fixing alpha and m1

      prior2 <- list(alpha=1,m1=rep(0,1),psiinv2=solve(diag(0.5,1)),nu1=4,nu2=4,
                     tau1=1,tau2=100)


    # Example of Prior information 3
    # Fixing only alpha

      prior3 <- list(alpha=1,m2=rep(0,1),s2=diag(100000,1),
                   psiinv2=solve(diag(0.5,1)),
                   nu1=4,nu2=4,tau1=1,tau2=100)


    # Example of Prior information 4
    # Everything is random

      prior4 <- list(a0=2,b0=1,m2=rep(0,1),s2=diag(100000,1),
                   psiinv2=solve(diag(0.5,1)),
                   nu1=4,nu2=4,tau1=1,tau2=100)

    # Fit the models

      fit1.1 <- DPdensity(y=speeds,prior=prior1,mcmc=mcmc,state=state,status=TRUE)
      fit1.2 <- DPdensity(y=speeds,prior=prior2,mcmc=mcmc,state=state,status=TRUE)
      fit1.3 <- DPdensity(y=speeds,prior=prior3,mcmc=mcmc,state=state,status=TRUE)
      fit1.4 <- DPdensity(y=speeds,prior=prior4,mcmc=mcmc,state=state,status=TRUE)

    # Posterior means
      fit1.1
      fit1.2
      fit1.3
      fit1.4

    # Plot the estimated density
      plot(fit1.1,ask=FALSE)
      plot(fit1.2,ask=FALSE)
      plot(fit1.3,ask=FALSE)
      plot(fit1.4,ask=FALSE)

    # Extracting the density estimate
      cbind(fit1.1$x1,fit1.1$dens)
      cbind(fit1.2$x1,fit1.2$dens)
      cbind(fit1.3$x1,fit1.3$dens)
      cbind(fit1.4$x1,fit1.4$dens)
      
    # Plot the parameters (only prior 2 for illustration)
    # (to see the plots gradually set ask=TRUE)
      plot(fit1.2,ask=FALSE,output="param")

    # Plot the a specific parameters 
    # (to see the plots gradually set ask=TRUE)
      plot(fit1.2,ask=FALSE,output="param",param="psi1-speeds",nfigr=1,nfigc=2)

    # Extracting the posterior mean of the specific means and covariance matrices 
    # (only prior 2 for illustration)
      DPrandom(fit1.2) 

    # Ploting predictive information about the specific means and covariance matrices 
    # with HPD and Credibility intervals
    # (only prior 2 for illustration)
    # (to see the plots gradually set ask=TRUE)
      plot(DPrandom(fit1.2,predictive=TRUE),ask=FALSE)
      plot(DPrandom(fit1.2,predictive=TRUE),ask=FALSE,hpd=FALSE)

    # Ploting information about all the specific means and covariance matrices 
    # with HPD and Credibility intervals
    # (only prior 2 for illustration)
    # (to see the plots gradually set ask=TRUE)
      plot(DPrandom(fit1.2),ask=FALSE,hpd=FALSE)




    ####################################
    # Bivariate example
    ####################################

    # Data
      data(airquality)
      attach(airquality)

      ozone <- Ozone**(1/3)
      radiation <- Solar.R

    # Prior information

      s2 <- matrix(c(10000,0,0,1),ncol=2)
      m2 <- c(180,3)
      psiinv2 <- solve(matrix(c(10000,0,0,1),ncol=2))
     
      prior <- list(a0=1,b0=1/5,nu1=4,nu2=4,s2=s2,
                    m2=m2,psiinv2=psiinv2,tau1=0.01,tau2=0.01)

    # Initial state
      state <- NULL

    # MCMC parameters

      nburn <- 5000
      nsave <- 10000
      nskip <- 10
      ndisplay <- 1000
      mcmc <- list(nburn=nburn,nsave=nsave,nskip=nskip,ndisplay=ndisplay)

    # Fit the model
      fit1 <- DPdensity(y=cbind(radiation,ozone),prior=prior,mcmc=mcmc,
                        state=state,status=TRUE,na.action=na.omit)

    # Plot the estimated density
      plot(fit1)

    # Extracting the density estimate
      fit1$x1
      fit1$x2
      fit1$dens

Run the code above in your browser using DataLab