Published on :
Statistic CREATION_INTERNE

Multivariate Normal Random-Effects Model

This code is also available in: Deutsch Español Français
Awaiting validation
This script illustrates the use of the MCMC procedure for the analysis of a hierarchical model. The data, internally generated, track the evolution of the weight of 30 rats over five periods. The model assumes that the weight growth for each rat follows a straight line (slope and intercept), but that these parameters (random effects) are specific to each rat and come from a common multivariate normal distribution. The procedure estimates the parameters of this group distribution as well as the residual variance of the model.
Data Analysis

Type : CREATION_INTERNE


The 'rats' dataset is generated via a DATALINES statement. The code transforms the data from a wide format (multiple measurements per row) to a long format (one measurement per row with corresponding 'subject' and 'age' variables).

1 Code Block
DATA STEP Data
Explanation :
This data block reads rat weights from 'datalines'. It uses an array (ARRAY) to map measurement days and calculations on the automatic variable `_n_` to assign a subject ID (`subject`) and age (`age`) to each measurement. The 'input weight @code_sas_json/8_SAS_Intro_ReadFile_MultiCol_@@.json;' statement is used to read multiple weight observations from a single data line, thus creating a dataset in long format.
Copied!
1title 'Multivariate Normal Random-Effects Model';
2DATA rats;
3 array days[5] (8 15 22 29 36);
4 INPUT weight @code_sas_json/8_SAS_Intro_ReadFile_MultiCol_@@.json;
5 subject = ceil(_n_/5);
6 index = mod(_n_-1, 5) + 1;
7 age = days[index];
8 drop index days:;
9 DATALINES;
10151 199 246 283 320 145 199 249 293 354
11147 214 263 312 328 155 200 237 272 297
12135 188 230 280 323 159 210 252 298 331
13141 189 231 275 305 159 201 248 297 338
14177 236 285 350 376 134 182 220 260 296
15160 208 261 313 352 143 188 220 273 314
16154 200 244 289 325 171 221 270 326 358
17163 216 242 281 312 160 207 248 288 324
18142 187 234 280 316 156 203 243 283 317
19157 212 259 307 336 152 203 246 286 321
20154 205 253 298 334 139 190 225 267 302
21146 191 229 272 302 157 211 250 285 323
22132 185 237 286 331 160 207 257 303 345
23169 216 261 295 333 157 205 248 289 316
24137 180 219 258 291 153 200 244 286 324
25;
26RUN;
2 Code Block
PROC MCMC
Explanation :
This block applies the MCMC procedure to perform Bayesian analysis. It defines model parameters, including hyperparameters (theta_c, Sig_c, var_y) and their prior distributions (PRIOR). The RANDOM statement specifies that the alpha and beta parameters (grouped in the 'theta' array) are random effects per subject, following a multivariate normal distribution. The MODEL statement defines the likelihood model for weight ('weight'). The procedure generates 10000 samples from the posterior distribution of the parameters, which are stored in the 'postout' table.
Copied!
1PROC MCMC DATA=rats nmc=10000 outpost=postout
2 seed=17 init=random;
3 ods select Parameters REParameters PostSumInt;
4 array theta[2] alpha beta;
5 array theta_c[2];
6 array Sig_c[2,2];
7 array mu0[2] (0 0);
8 array Sig0[2,2] (1000 0 0 1000);
9 array S[2,2] (0.02 0 0 20);
10 
11 parms theta_c Sig_c {121 0 0 0.26} var_y;
12 prior theta_c ~ mvn(mu0, Sig0);
13 prior Sig_c ~ iwish(2, S);
14 prior var_y ~ igamma(0.01, scale=0.01);
15 
16 random theta ~ mvn(theta_c, Sig_c) subject=subject
17 monitor=(alpha_9 beta_9 alpha_25 beta_25);
18 mu = alpha + beta * age;
19 model weight ~ normal(mu, var=var_y);
20RUN;
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