Published on :
Statistics INTERNAL_CREATION

Random Effects Model with PROC MCMC

This code is also available in: Deutsch Español Français
Awaiting validation
This SAS© script illustrates the use of the MCMC procedure to fit a random effects model. It first creates a 'heights' dataset using `datalines` with family, gender, and height information. Then, a new 'input' dataset is created from 'heights' to encode gender into a binary numerical variable ('gf'). PROC MCMC is then used to fit a normal model where height is modeled as a function of gender and a family-specific random effect. Prior distributions are specified for the model parameters (intercept, gender coefficient, variances). The script enables and disables ODS graphics for MCMC trace visualization.
Data Analysis

Type : INTERNAL_CREATION


Raw data is directly integrated into the script via a `datalines` statement, forming the 'heights' dataset. This is then transformed into 'input' for MCMC analysis.

1 Code Block
DATA STEP Data
Explanation :
This DATA STEP block initializes and populates the 'heights' dataset directly from the data provided in the script via the `datalines` statement. It defines three variables: 'Family' (numeric), 'G' (gender, character), and 'Height' (numeric).
Copied!
1DATA heights;
2 INPUT Family G$ Height;
3 DATALINES;
41 F 67
51 F 66
61 F 64
71 M 71
81 M 72
92 F 63
102 F 63
112 F 67
122 M 69
132 M 68
142 M 70
153 F 63
163 M 64
174 F 67
184 F 66
194 M 67
204 M 67
214 M 69
22;
23 
2 Code Block
DATA STEP Data
Explanation :
This DATA STEP block creates a new dataset named 'input' by reading 'heights'. It transforms the categorical variable 'G' (gender) into a numerical indicator variable 'gf' (1 for Female, 0 for Male) and removes the original 'G' variable for subsequent analysis.
Copied!
1DATA INPUT;
2 SET heights;
3 IF g eq 'F' THEN gf = 1;
4 ELSE gf = 0;
5 drop g;
6RUN;
7 
3 Code Block
ODS Statement
Explanation :
Activates the ODS (Output Delivery System) graphics system to allow for the generation of high-resolution graphs by SAS procedures.
Copied!
1ods graphics on;
2 
4 Code Block
PROC MCMC
Explanation :
This MCMC (Monte Carlo Markov Chain) procedure fits a random effects model to the 'input' dataset. It generates an output dataset 'postout' containing the post-MCMC samples. Model parameters are initialized, and prior distributions are specified for the intercept (b0), gender coefficient (b1), residual variance (s2), and random effects variance (s2g). A random effect 'gamma' is defined for each 'family'. The model specifies that 'height' follows a normal distribution with a mean 'mu' (a function of fixed and random effects) and a variance 's2'. The number of iterations (nmc) and the random seed are fixed, and trace plots are requested.
Copied!
1PROC MCMC DATA=INPUT outpost=postout nmc=50000 seed=7893 plots=trace;
2 ods select Parameters REparameters PostSumInt tracepanel;
3 parms b0 0 b1 0 s2 1 s2g 1;
4 
5 prior b: ~ normal(0, var = 10000);
6 prior s: ~ igamma(0.01, scale = 0.01);
7 random gamma ~ normal(0, var = s2g) subject=family monitor=(gamma);
8 mu = b0 + b1 * gf + gamma;
9 model height ~ normal(mu, var = s2);
10RUN;
11 
5 Code Block
ODS Statement
Explanation :
Deactivates the ODS graphics system, thus stopping the generation of graphics.
Copied!
1ods graphics off;
2 
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, NAME: MCMCGS3, TITLE: Getting Started Example 3 for PROC MCMC, PRODUCT: STAT, SYSTEM: ALL, KEYS: random-effects model, PROCS: MCMC, DATA:, REF: PROC MCMC, GETTING STARTED EXAMPLE 3, MISC: