Published on :

Introduction to PROC MCMC Example

This code is also available in: Deutsch Español Français
The script performs a simple linear regression analysis using a Bayesian approach via PROC MCMC. It defines prior distributions for the regression coefficients (beta0, beta1) and the error variance (sigma2), then models the 'weight' variable as a function of 'height'. Markov Chain Monte Carlo sampling is configured with 10000 iterations and a thinning rate of 2. The MCMC chain results are saved in a dataset called 'classout'. Graphical output is enabled during the procedure's execution.
Data Analysis

Type : SASHELP


The source data comes from the built-in 'SASHELP.CLASS' dataset, which is a standard SAS dataset available in the SAS environment.

1 Code Block
ODS
Explanation :
Activates the Output Delivery System (ODS) for graphical output, allowing PROC MCMC to generate visualizations of the analysis results.
Copied!
1ods graphics on;
2 Code Block
PROC MCMC
Explanation :
This block executes the PROC MCMC procedure for a Bayesian regression analysis. It uses 'sashelp.class' as input data and creates an output dataset 'classout' containing the MCMC samples. Parameters 'beta0', 'beta1', and 'sigma2' are declared with initial values. Prior distributions (normal for betas, inverse gamma for sigma2) are specified. The linear mean 'mu' is defined as a function of 'height', and the model specifies that 'weight' follows a normal distribution with this mean and variance 'sigma2'. The 'nmc', 'thin', and 'seed' options control MCMC sampling.
Copied!
1PROC MCMC DATA=sashelp.class outpost=classout nmc=10000 thin=2 seed=246810;
2 parms beta0 0 beta1 0;
3 parms sigma2 1;
4 prior beta0 beta1 ~ normal(mean = 0, var = 1e6);
5 prior sigma2 ~ igamma(shape = 3/10, scale = 10/3);
6 mu = beta0 + beta1*height;
7 model weight ~ n(mu, var = sigma2);
8RUN;
3 Code Block
ODS
Explanation :
Deactivates graphical output via the Output Delivery System (ODS), ending the generation of graphics after the PROC MCMC execution.
Copied!
1ods graphics off;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : S A S S A M P L E L I B R A R Y