/* From Hayes (2005). Statistical Methods for Communication Science */. /* Mahwah, NJ: Lawrence Erlbaum Associates */. /* This macro will simulate sampling from a bivariate population with */. /* correlation rho. After the macro is activated, the syntax is */. /* RSAMP SIZE = n SAMPLES = reps RHO = corr */. /* where n is the size of each sample, reps is the number of samples */. /* of n you want to take, and corr is the population */. /* correlation between the two variables. Each sample is stored in a */. /* row of the active data matrix */. DEFINE RSAMP (size = !tokens(1)/samples = !tokens(1)/rho = !cmdend). preserve. SET MXLOOP = !samples. set seed = random. MATRIX. compute rpop = !rho. compute reps = !samples. compute n = !size. compute res = make(reps,1,1). compute ones = make(n,1,1). loop i = 1 to reps. compute x = sqrt(-2*ln(uniform(n,1)))&*cos((2*3.14159265358979)*uniform(n,1)). compute y = rpop*x+sqrt(1-(rpop*rpop))*sqrt(-2*ln(uniform(n,1)))&* cos((2*3.14159265358979)*uniform(n,1)). compute xy={x,y}. compute xyc = xy-(ones*(csum(xy)&/n)). compute vcov = (1/(n-1))*(t(xyc)*xyc). compute var = diag(vcov). compute r = mdiag(1/sqrt(var))*vcov*mdiag(1/sqrt(var)). compute res(i,1) = r(2,1). end loop. compute mat = {n;reps;rpop}. print mat/title = "Simulation Characteristics"/rlabels = "n" "Samples" "rho" /format = F10.4. save res/outfile = */variables = r. end matrix. restore. GRAPH/HISTOGRAM=r. !END DEFINE.