Date: Thu, 4 Mar 1999 09:22:12 -0500
Reply-To: "Alderton, David NPRST" <NPRST12@BUPERS.NAVY.MIL>
Sender: "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU>
From: "Alderton, David NPRST" <NPRST12@BUPERS.NAVY.MIL>
Subject: generate multivariate normal data
Content-Type: text/plain; charset="iso-8859-1"
Hi folks, I've been off SAS-L for 8 months or so because of a job change and
relocation. Glad to see the list is still very active.
I was reworking some old code I had taken from a message by Bob Hammer (17
Nov 88!), on generating multivariate normal observations. Below is the
essence of the program which seems to work adequately. However, given its
age and the changes in SAS, I wanted to ask the list about the best way to
generate multivariate normal data and/or multivariate data from other
distributions as well.
Here's what I have. Any comments or suggestions?
/*===============================================================*/
/*Simulate data with a set correlational structure */
/* first try based on modfication of... */
/*Originally taken from a 1994 SAS-L message, attributed to */
/* */
/* Author: Robert M. Hamer, Ph.D., Department of Biostatistics, */
/* Virginia Commonwealth University, Richmond, VA USA */
/* Date: 17NOV88 */
/* */
/*===============================================================*/
%let n=1000; * n is the number of observations;
%let k=3; * k is the number of variables;
data normals(keep=v1-v&k); * generate 1000 3 var iid;
array vararray{*} v1-v&k; * standard normal obs;
do iobs=1 to &n;
do ivar=1 to &k;
vararray{ivar}=rannor(0); * to replicate, set 0 to a fixed value;
end;
output;
end;
run;
data corrs; * read correlation matrix for mvn;
input c1-c&k;
cards;
1 .2 .3
.2 1 .4
.3 .4 1
;
run;
proc iml;
use normals; * v contains iid std normals;
read all into v;
use corrs; * corrs contains desired corr matrix;
read all into corrs;
call eigen(m,e,corrs);
factor=e*diag(sqrt(m)); * multiplier is e-vecs * sqrt e-vals;
newmat=v*factor`; * newmat is mvn with desired corr matrix;
create simdata from newmat; * output to sas dataset;
append from newmat;
quit;
proc corr data=simdata; * run proc corr to verify;
title "Correlations for simulated data with &k vars and &n observations";
run;
David L. Alderton, Ph.D.
Navy Personnel Research and Development Center and
Navy Personnel Research, Studies, and Technology (PERS-1)
NPRDC Millington Office
5720 Integrity Drive
Millington, TN 38054-5026
DSN prefix: 882
Phone: 901/874-4640
FAX: 901/874-2720
Email: Alderton@NPRDC.Navy.mil
|