Example of Multivariate Distribution Usage

This code is also available in: Deutsch Español Français
Difficulty Level
Beginner
Published on :
The script begins by using `PROC IML` to simulate 100 observations from a bivariate normal distribution with a specified mean and covariance matrix. The simulated data is then saved to a SAS© dataset named `inputdata`. Next, `PROC MCMC` is used to fit a Bayesian multivariate normal model to this data. Prior distributions (multivariate normal for the mean `mu` and inverse Wishart for the covariance matrix `Sigma`) are specified. The procedure executes 3000 iterations of the Markov Chain Monte Carlo (MCMC) to sample the posterior distributions of the model parameters.
Data Analysis

Type : CREATION_INTERNE


The `inputdata` is internally generated by `PROC IML` from a simulated multivariate normal distribution.

1 Code Block
PROC IML Data
Explanation :
This `PROC IML` block initializes a random number generator (`call randseed(1)`), simulates 100 observations (`N = 100`) from a bivariate normal distribution with a mean {1 2} and a specific covariance matrix, then calculates and displays the sample means and covariance matrices. Finally, it creates a SAS dataset named `inputdata` from the simulated data with columns 'x1' and 'x2'.
Copied!
1title 'An Example that Uses Multivariate Distributions';
2PROC IML;
3 N = 100;
4 Mean = {1 2};
5 Cov = {2.4 3, 3 8.1};
6 call randseed(1);
7 x = RANDNORMAL( N, Mean, Cov );
8 
9 SampleMean = x[:];
10 n = nrow(x);
11 y = x - repeat( SampleMean, n );
12 SampleCov = y`*y / (n-1);
13 PRINT SampleMean Mean, SampleCov Cov;
14 
15 cname = {"x1", "x2"};
16 create inputdata from x [colname = cname];
17 append from x;
18 close inputdata;
19QUIT;
2 Code Block
PROC MCMC
Explanation :
This `PROC MCMC` block fits a multivariate normal model to the previously generated `inputdata`. It specifies the data variables (`x1`, `x2`), the parameters to be estimated (`mu` and `Sigma`), and defines prior distributions for these parameters: a multivariate normal distribution for `mu` (with mean `mu0` and covariance matrix `Sigma0` of large variance) and an inverse Wishart distribution for `Sigma` (with 2 degrees of freedom and scale matrix `S` which is the identity matrix). The procedure executes 3000 iterations of the Markov Chain Monte Carlo (MCMC) for Bayesian inference of the parameters, and the output is limited to summary statistics and credible intervals (`ods select PostSumInt`).
Copied!
1PROC MCMC DATA=inputdata seed=17 nmc=3000 diag=none;
2 ods select PostSumInt;
3 array DATA[2] x1 x2;
4 array mu[2];
5 array Sigma[2,2];
6 array mu0[2] (0 0);
7 array Sigma0[2,2] (100 0 0 100);
8 array S[2,2] (1 0 0 1);
9 parm mu Sigma;
10 prior mu ~ mvn(mu0, Sigma0);
11 prior Sigma ~ iwish(2, S);
12 model DATA ~ mvn(mu, Sigma);
13RUN;
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


Related Documentation

Aucune documentation spécifique pour cette catégorie.