[R] Simple simulation in R

Thomas Petzoldt thpe at hhbio.wasser.tu-dresden.de
Tue Aug 26 18:02:06 CEST 2003


> Hello all
> 
> I have a feeling this is very simple......but I am not sure how to do
> it
> 
> My boss has two variables, one is an average of 4 numbers, the other is
> an average of 3 of those numbers i.e
> 
> var1 = (X1 + X2 + X3 + X4)/4
> var2 = (X1 + X2 + X3)/3

Hello Peter, try the following:

####################################
nsim  <- 100
nsamp <- 100

r <- numeric(nsim)

for (i in 1:nsim) {
   dat <- matrix(rnorm(4 * nsamp), ncol=4)
   v1 <- rowSums(dat)/4
   v2 <- rowSums(dat[,1:3])/3
   r[i] <- cor(v1,v2)
}

plot(sort(r))
####################################

Hope it helps!

Thomas P.




More information about the R-help mailing list