nobs <- 500 # Observations per data set nsim <- 1000 # Number of data sets for (i in 1:nsim) { # generate two "latent" variables # data from a chisquare distribution is generated and normalized x1 <- (rchisq(nobs, df=1) - 1)/sqrt(2) x2 <- (rchisq(nobs, df=1) - 1)/sqrt(2) # then the required variances and covariance are induced covadj <- matrix(c(0.085,0.093,0.093,0.907), ncol=2) x.matrix <- cbind(x1,x2)%*%chol(covadj) # finally two vectors are extracted, for convenience f1 <- x.matrix[,1] f2 <- x.matrix[,2] # generate y from latent variables # here again we use a chisquare instead of a normal distribution y <- 1.074*f1 + .109*f2 + 0.624*f1*f2 + (rchisq(nobs, df=1)-1)*sqrt(0.871)/sqrt(2) + 1.554 # generate measurement variables for latent variables ex1 <- 1*f1 + (rchisq(nobs, df=1)-1)*sqrt(0.207)/sqrt(2) + 1.146 ex2 <- 1.086*f1 + (rchisq(nobs, df=1)-1)*sqrt(0.203)/sqrt(2) + 1.122 ex3 <- 1.624*f1 + (rchisq(nobs, df=1)-1)*sqrt(0.553)/sqrt(2) + 1.393 ex4 <- 1.262*f1 + (rchisq(nobs, df=1)-1)*sqrt(0.298)/sqrt(2) + 1.203 in1 <- 1*f2 + (rchisq(nobs, df=1)-1)*sqrt(0.945)/sqrt(2) + 2.286 in2 <- 0.747*f2 + (rchisq(nobs, df=1)-1)*sqrt(0.860)/sqrt(2) + 1.956 in3 <- 0.888*f2 + (rchisq(nobs, df=1)-1)*sqrt(0.865)/sqrt(2) + 1.983 in4 <- 0.675*f2 + (rchisq(nobs, df=1)-1)*sqrt(0.631)/sqrt(2) + 1.636 # package the observed variables as a matrix obs.matrix <- cbind(ex1,ex2,ex3,ex4,in1,in2,in3,in4,y) print(i) # to give the user live feedback # create one data set write.table(obs.matrix, file=paste("z:/mplus/mvchii/mvchii",i,".dat",sep=""), row.names=FALSE, col.names=FALSE) } # create a list of the data sets, for MPlus input write.table(paste("mvchii",1:nsim,".dat", sep=""), file="z:/MPlus/MVChiI/mvchiilist.dat", quote=FALSE, col.names=FALSE, row.names=FALSE)