dev_allowance <- 0.15 #Deviation in r allowed
within_limit <- FALSE #Initiate
count <- 0 #Loop count
nvar <- 10 #number of variables to simulate
nobs = 50 #number of observations to simulate
#define correlation matrix
M = matrix(c(1., .0, .0, .0, .0, .0, .0, .0, .0, .0,
.0, 1., .0, .0, .0, .0, .0, .0, .0, .0,
.0, .0, 1., .8, .0, .0, .0, .0, .0, .0,
.0, .0, .8, 1., .0, .0, .0, .0, .0, .0,
.0, .0, .0, .0, 1., .2, .0, .0, .0, .0,
.0, .0, .0, .0, .2, 1., .0, .0, .0, .0,
.0, .0, .0, .0, .0, .0, 1., .8, .0, .0,
.0, .0, .0, .0, .0, .0, .8, 1., .0, .0,
.0, .0, .0, .0, .0, .0, .0, .0, 1., .2,
.0, .0, .0, .0, .0, .0, .0, .0, .2, 1.), nrow=nvar, ncol=nvar)
L = chol(M) # Cholesky decomposition
#Loop while not within limit
while (!within_limit) {
# Generate random variables
r = t(L) %*% matrix(rnorm(nvars*nobs), nrow=nvars, ncol=nobs)
r = t(r)
# Check if within limit
within_limit <- all(abs(cor(r) - M) < dev_allowance)
# Count loop
count <- count + 1
}
cat(paste0("run count: ", count))
我试图用定义的相关性模拟大约10个随机正态变量。同时,我希望模拟变量的相关性在一个以定义的相关性为中心的特定范围内。
但是运行时间是不可接受的,如果不是无限长的话。
目前,我想nobs=50
和nobs=200
。虽然我计划设置dev_allowance=0.05
,但我现在所拥有的是dev_allowance
小于约1时可能需要一分多钟。 0.1 nobs=50
和约。{ nobs=200
为0.08。不敢尝试较小的dev_allowance
...
如果我坚持使用当前的参数方案,是否有解决方法?
答案 0 :(得分:0)
嗯...在我脑海中输入这个问题的中途:
sim_nvar <- matrix(rnorm(nobs), ncol=nobs)
for (i in 2:nvar) {
within_limit <- FALSE
while (!within_limit) {
#Generate random variables
sim_var <- t(L)[i, 1:i] %*% rbind(sim_nvar, matrix(rnorm(nobs), ncol=nobs))
sim_var <- t(rbind(sim_nvar, sim_var))
#Check if within limit
within_limit <- all(abs(cor(sim_var) - M[1:i, 1:i]) < dev_allowance)
}
sim_nvar <- t(sim_var)
}
sim_nvar <- t(sim_nvar)
all(abs(cor(sim_nvar) - M) < dev_allowance)
[1] TRUE
对我来说似乎没问题。但如果我以这种方式分离模拟,有没有任何缺陷?或者这是最好的方式呢?