如何使用for循环更新协方差矩阵中的对角线?

如何使用for循环更新协方差矩阵中的对角线?,r,for-loop,matrix,correlation,R,For Loop,Matrix,Correlation,我已经使用mvrnorms为两个变量创建了模拟数据,我想在一个循环中关联这些变量0、.5、.7和.9。但每次运行for循环时,我只能关联.9处的值,而不能关联任何其他关联条件 library(MASS) #library I needed to create simulated data with mvrnorms num_iter <- 75 N <- 30 # setting my sample size mu <- c(50.

我已经使用mvrnorms为两个变量创建了模拟数据,我想在一个循环中关联这些变量0、.5、.7和.9。但每次运行for循环时,我只能关联.9处的值,而不能关联任何其他关联条件

library(MASS) #library I needed to create simulated data with mvrnorms

num_iter <- 75
N <- 30                       # setting my sample size
mu <- c(50.5, 10.5)           # setting the std
R <- c(0,.5,.7,.9)            # this vector defines the different correlation conditions I will add

# saving files
dir.create("simulated1data") # This creates a directory to store files

# performing 75 iterations and so there should be 75 data files in the folder I made
for(i in 1:num_iter){
  for(j in 1:4){
    cov <- matrix(c(1,R[j],R[j],1),2,2)
    x <- mvrnorm(N,mu,cov)
    write.table(x, file=paste("simulated1data/simdata_",i,"_",j,".txt",sep="")) # writing to separate txt file
  }
}

library(MASS)#我需要用mvrnorms创建模拟数据的库

num_iter要分配
R
的值,请事先创建
cov
矩阵,并使用逻辑索引矩阵
imat

第一个代码块如问题中所示

library(MASS) #library I needed to create simulated data with mvrnorms

num_iter <- 75
N <- 30                       # setting my sample size
mu <- c(50.5, 10.5)           # setting the std
R <- c(0, 0.5, 0.7, 0.9)      # this vector defines the different correlation conditions I will add
最后,双
for
循环

# performing 75 iterations and so there should be 75 data files in the folder I made
for(i in 1:num_iter){
  for(j in 1:4){
    cov[imat] <- R[j]
    x <- mvrnorm(N, mu, cov)
    flname <- paste0("simdata_", i, "_", j, ".txt")
    flname <- file.path(dirsimdata, flname)
    write.table(x, file = flname) # writing to separate txt file
  }
}
#执行75次迭代,因此我创建的文件夹中应该有75个数据文件
for(1:num_iter中的i){
对于(1:4中的j){

cov[imat]我在你的代码中没有发现任何错误。你错误地将
mu
识别为标准偏差,但它是每个变量的平均值,
R
是协方差而不是相关性。你在协方差矩阵中将每个变量的标准偏差设置为
1
。如果我设置
num\iter
# index matrix used to assign values from R
imat <- matrix(c(FALSE, TRUE, TRUE, FALSE), nrow = 2)
# start with all 1's
cov <- matrix(1, nrow = 2, ncol = 2)
# performing 75 iterations and so there should be 75 data files in the folder I made
for(i in 1:num_iter){
  for(j in 1:4){
    cov[imat] <- R[j]
    x <- mvrnorm(N, mu, cov)
    flname <- paste0("simdata_", i, "_", j, ".txt")
    flname <- file.path(dirsimdata, flname)
    write.table(x, file = flname) # writing to separate txt file
  }
}
cor(read.table("simulated1data/simdata_1_1.txt"))
#          V1       V2
# V1 1.000000 0.204011
# V2 0.204011 1.000000
cor(read.table("simulated1data/simdata_1_2.txt"))
#           V1        V2
# V1 1.0000000 0.2706851
# V2 0.2706851 1.0000000
cor(read.table("simulated1data/simdata_1_3.txt"))
#           V1        V2
# V1 1.0000000 0.6727047
# V2 0.6727047 1.0000000
cor(read.table("simulated1data/simdata_1_4.txt"))
#           V1        V2
# V1 1.0000000 0.9306898
# V2 0.9306898 1.0000000
cor(read.table("simulated1data/simdata_2_1.txt"))
#            V1         V2
# V1 1.00000000 0.06184222
# V2 0.06184222 1.00000000
cor(read.table("simulated1data/simdata_2_2.txt"))
#           V1        V2
# V1 1.0000000 0.3686962
# V2 0.3686962 1.0000000
cor(read.table("simulated1data/simdata_2_3.txt"))
#           V1        V2
# V1 1.0000000 0.7660853
# V2 0.7660853 1.0000000
cor(read.table("simulated1data/simdata_2_4.txt"))
#           V1        V2
# V1 1.0000000 0.8589621
# V2 0.8589621 1.0000000