I simulated my data successfully in R by applying the R codes below. However, I need to simulate errors separately for each different Y outcome by using covariance method in R.
I don't know how to use covariance to simulate errors in R.
Could you help me about this problem?
Thanks
library(MASS)
## var1(Verb.comp), var2(word.Id), var3(word.att) ~ N(0, 1)
## r_12 = 0.5, r_23 = 0.6, r_13 = 0.5
covmat <- matrix(c(1, 0.5, 0.5,
0.5, 1, 0.7,
0.5, 0.7, 1), ncol = 3, byrow = TRUE)
## this is what my variance-covariance matrix looks like
covmat
## simulate data
df <- mvrnorm(1000, mu = c(0, 0, 0), Sigma = covmat)
head(df)
## estimate pm-corelations
cor(df)
evalA <- matrix(c(1,1,1,0,1,1,0,0,1),3,3)
head(evalA)
head(mydat <- as.matrix(df)%*%evalA)
colnames(mydat) <- c("VC", "WI", "WA")
head(mydat)
err<-rnorm(1000,0,1)
head(err)
Y.vc <- 0.8 + 1*mydat[,'VC'] + 0.54*mydat[,'WI']+0.51*mydat[,'WA'] + err
Y.wi <- 0.8 + 0.54*mydat[,'VC'] + 1*mydat[,'WI']+0.75*mydat[,'WA'] + err
Y.wa <- 0.8 + 0.51mydat[,'VC']+ 0.75*mydat[,'WI']+1*mydat[,'WA'] + err