Re: S-plus multivariate normal distribution
Nick - Here is an adapted snippet of some code I have used recently.
Implementation of multivariate normals is pretty easy in S+, but it can be
an annoying pain in the tuckus to take the lower half of the covariance
matrix from NONMEM and create the symmetrical matrix that S is expecting
(especially when you have a big matrix). There is some code, admittedly
awkward, that will take NONMEM's half matrix (prettied up with some
commas) and create a symmetrical matrix. In this case I am using a
correlation matrix, but you'll get the idea.
Regards, Jeff
nsim<-100
### A correlation matrix
a<-c(1.00,
0.81,1.00,
0.32,0.21,1.00)
a<-matrix(a, ncol=1, byrow=T)
ndim <- 3
sel<-0
# Will make symetric matrix from the lower triangle called b
b<-matrix(0,ncol=ndim,nrow=ndim)
for (i in seq(ndim)) {
for (j in seq(i)) {
sel <- sel+1
b[i,j]<-a[sel]}}
for (i in seq(ndim-1)) {
for (j in seq(ndim-i)) {
sel <- j+i
b[i,sel]<-b[sel,i]}}
stuff<-rmvnorm(nsim, mean=c(0,0,0), cov=b, sd=c(0.15, 0.30,0.5))
# make a plot
splom(~stuff)
#-##-##-##-##-##-##-##-##-##-##-##-##-##-##-##-###-#
"Nick Holford" <[EMAIL PROTECTED]>
Sent by: [EMAIL PROTECTED]
13-Sep-2007 06:30
To
"nmusers" <[email protected]>
cc
Subject
[NMusers] S-plus multivariate normal distribution
Hi,
Can someone give me some clues on how to sample from a multivariate normal
distribution using S-plus?
Some working code examples would be very helpful.
Thanks,
Nick
--
Nick Holford, Dept Pharmacology & Clinical Pharmacology
University of Auckland, 85 Park Rd, Private Bag 92019, Auckland, New
Zealand
[EMAIL PROTECTED] tel:+64(9)373-7599x86730 fax:+64(9)373-7090
www.health.auckland.ac.nz/pharmacology/staff/nholford