turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- General Programming
- /
- Using dgeneral in proc MCMC

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-31-2014 03:16 PM

**Hi all,**

**I'm trying to learn how to use dgeneral to specify a new distribution for a discrete variable. I thought I would use as a start example 54.1 Simulating Samples From a Known Density. The SAS code is as follow and work as expected :**

/*******************************************************************************/

title 'Simulating Samples from a Normal Density Example 54.1';

data x;

run;

ods graphics on;

proc mcmc data=x outpost=simout seed=23 nmc=10000 maxtune=0

nbi=0 statistics=(summary interval) diagnostics=none;

ods exclude nobs parameters;

parm alpha 0;

prior alpha ~ normal(0, sd=1);

model general(0);

run;

/*******************************************************************************/

**Then I tried defining the normal(0,1) distribution of example 54.1 by myself to replicate the results with the following SAS code (again works correctly) :**

/*******************************************************************************/

title 'Simulating Samples from a Normal Density with general function';

data x;

run;

ods graphics on;

proc mcmc data=x outpost=simout seed=23 nmc=10000 maxtune=0

nbi=0 statistics=(summary interval) diagnostics=none;

ods exclude nobs parameters;

parm alpha 0;

lnorm = -(alpha**2)/2 ;

prior alpha ~ general(lnorm);

model general(0);

run;

/*******************************************************************************/

**Ok so now my goal is to specify the distribution of a discrete variable so I altered the above code to simulate a bernoulli variable using the pre-built "binary" function and seems to work as expected :**

/*******************************************************************************/

title 'Simulating Samples from a Bernoulli pmf';

data x;

run;

ods graphics on;

proc mcmc data=x outpost=simout seed=23 nmc=10000 maxtune=0

nbi=0 statistics=(summary interval) diagnostics=none;

ods exclude nobs parameters;

parm Y 0;

prior Y ~ binary(p=0.6);

model general(0);

run;

/*******************************************************************************/

**Finally, like for the normal variable simulation presented above, I wanted to replicate the bernoulli simulation by specifying the distribution myself with the dgeneral function. **

/*******************************************************************************/

title 'Simulating Samples from a Bernoulli pmf with dgeneral';

data x;

run;

ods graphics on;

proc mcmc data=x outpost=simout seed=23 nmc=10000 maxtune=0

nbi=0 statistics=(summary interval) diagnostics=none;

ods exclude nobs parameters;

parm Y 0;

*p = 0.6 ;

lb = Y*log(0.6) + (1-Y)*log(0.4) ;

prior Y ~ DGENERAL(lb);

model general(0);

run;

/*******************************************************************************/

**However, I get results that are comlpetely off. I would appreicate if someone could point out where do I fail in my attempt at simulating a bernoulli variable with dgeneral.**

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to IanS

08-01-2014 08:26 AM

Not an expert on this AT ALL, but the examples I saw using DGENERAL in a prior did not look like a function, but rather, really discrete--placing probability masses on specific points. Maybe someone with more experience than me will step in, but I think you would maybe get better results with:

prior Y ~ DGENERAL(lp, lower=0, upper=1);

which would put equal prior mass at the extremes. Since DGENERAL steps by one (I don't see how to change that), to get a finer grid, you would need to follow the example in Example 59.2 Box-Cox Transformation, where, to get lambda on -2 to 2 with a finer grid, they use DGENERAL on alpha ranging from -200 to 200, and have a functional relationship. This is all enclosed in a BEGINNODATA/ENDNODATA block. Hope this helps some.

Steve Denham

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to SteveDenham

12-08-2014 11:57 AM

Hi Steve,

Thank you for your input. It turned out to be very useful! It appears that if I follow example 59.2 by providing some lower and upper bounds as argument to the DGENERAL function, then the bernoulli example I posted will achieve the desired result. I would still be curious to read about someone who might happen to know what are the default lower and upper bounds if we do not provide any?

title 'Simulating Samples from a Bernoulli pmf with dgeneral';

data x;

run;

ods graphics on;

proc mcmc data=x outpost=simout seed=23 nmc=10000 maxtune=0

nbi=0 statistics=(summary interval) diagnostics=none;

ods exclude nobs parameters;

parm Y 0;

*p = 0.6 ;

lb = Y*log(0.6) + (1-Y)*log(0.4) ;

prior Y ~ DGENERAL(lb, **lower=0, upper=1**);

model general(0);

run;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to IanS

12-08-2014 12:30 PM

I don't see anything in the SAS/STAT13.2 documentation that gives any guidance on this, other than Example 59.2.(61.2 in the SAS/STAT13.2 documentation). I would guess that an error or warning statement would appear in the log if no lower or upper values for the discrete distribution were specified.

Steve Denham

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to SteveDenham

12-08-2014 02:13 PM

Steve, running the 4th example I've attached in the opening post does not produce any warning or error messages in the log file, despite not returning results that are expected for a Bernoulli trial.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to IanS

12-08-2014 01:37 PM

I'm sure that you have a reason for simulating by using PROC MCMC (like this is just part of a larger analysis), but I do want to point out that many simulation studies use the DATA step to simulate data. In addition to a dozen standard discrete distributions and many complex distributions that you can build from them, the DATA step provides the "Table" distribution where you can specify the probabilities of selecting each of k categories. This enables you to simulate any discrete distribution with finitely many categories. See

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rick_SAS

12-08-2014 02:10 PM

Rick, you got it right. I'm actually trying to program a bayesian multivariate normal mixture model within proc MCMC (Also called latent profile model). The examples above were just to show the issue I was running in. I actually saw your thread on mixture models using proc FMM (scrabble example). However at the time, I was under the impression that FMM would model univariate mixtures while I'm trying to model a multivariate mixtures.

Thank you!