在R for-loops中从较小的矩阵逐步创建一个大矩阵 [英] Stepwise creation of one big matrix from smaller matrices in R for-loops

查看:269
本文介绍了在R for-loops中从较小的矩阵逐步创建一个大矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

  beta < -  c(1,2,3)
X1 < - 矩阵(c(1,1,1,1,
0,1,0,1,
0,0,1,1),
nrow = 4,

Z1 < - 矩阵(c(1,1,1,1,
0,1,0,1),
nrow = 4 ,



Z2 < - 矩阵(c(1,1,1,1,
0,1,0,1),
(0,0),Sigma =矩阵(c(0,0),n = 4,
ncol = 2)

库(MASS)
S1 mvrnorm (10,4,3,2),ncol = 2))
S2 <-mvrnorm(40,mu = c(0,0),Sigma =矩阵(c(10,4,4,2) (1):dim(S1)[1(1),...,ncol = 2))

z< - list()
y< - list ]){
for(i in 1:dim(S2)[1]){
z [[i]] < - X1%*%beta + Z1%*%S1 [j,] + Z2%*%S2 [i,] +矩阵(rnorm(4,mean = 0,sd = 0.27),nrow = 4)
Z < - unname(do.call(rbind,z))
}
y [[j]] < - Z
Y < - unname(do.call(rbind,y))
}



X1 是一个 4x3 Z1 Z2 4x2 矩阵。所以每一次 X1%*%beta + X2%*%S1 [j,] + X2%*%S2 [i,] +矩阵(rnorm(4,mean = 0,sd = sigma) = 4)被调用,它输出一个 4x1 矩阵。到目前为止,我将所有这些值存储在内部和外部循环中的两个列表中,然后调用 rbind()将它们转换为矩阵。有没有办法直接将它们存储在矩阵?

解决方案

如果您依赖应用函数以及向量回收。我把你的方程式分解成几部分。 (我希望我能准确地解释它)!

pre $ code $ Mb < - as.vector(X1%*%beta)
1 b1 b1 M2 < - 应用(S1,1,函数(x)Z1%*%x)

M2 < (x))

as.vector(Mout) + rnorm(length(Mout),mean = 0,sd = 0.27)

因为随机数是在矩阵乘法之后添加(即不涉及任何计算),您可以将它们放在最后。



另外请注意你不能将较小的矩阵添加到较大的矩阵,但是如果你先把它作为一个矢量,那么R就会根据需要回收它。所以当Mb(长度为4的矢量)被添加到4行n列的矩阵时,它会循环n次。

I have the following code:

beta <- c(1, 2, 3)
X1 <- matrix(c(1, 1, 1, 1,
               0, 1, 0, 1,
               0, 0, 1, 1),
             nrow = 4,
             ncol = 3)

Z1 <- matrix(c(1, 1, 1, 1,
               0, 1, 0, 1),
             nrow = 4,
             ncol = 2)

Z2 <- matrix(c(1, 1, 1, 1,
               0, 1, 0, 1),
             nrow = 4,
             ncol = 2)

library(MASS)
    S1 <- mvrnorm(70, mu = c(0,0), Sigma = matrix(c(10, 3, 3, 2), ncol = 2))
    S2 <- mvrnorm(40, mu = c(0,0), Sigma = matrix(c(10, 4, 4, 2), ncol = 2))

z <- list()
y <- list()

for(j in 1:dim(S1)[1]){
    for(i in 1:dim(S2)[1]){
        z[[i]] <- X1 %*% beta+Z1 %*% S1[j,]+Z2 %*% S2[i,]+matrix(rnorm(4, mean = 0 , sd = 0.27), nrow = 4)
        Z <- unname(do.call(rbind, z))
    }
    y[[j]] <- Z
    Y <- unname(do.call(rbind, y))
}

X1 is a 4x3, Z1 and Z2 are 4x2 matrices. So everytime X1 %*% beta+X2 %*% S1[j,]+X2 %*% S2[i,]+matrix(rnorm(4, mean = 0 , sd = sigma), nrow = 4) is called it outputs a 4x1 matrix. So far I store all these values in the inner and outer loop in two lists and then call rbind() to transform them into a matrix. Is there a way to directly store them in matrices?

解决方案

You can avoid using lists if you rely on the apply functions and on vector recycling. I broke down your equation into its parts. (I hope I interpreted it accurately!)

Mb <- as.vector(X1 %*% beta)

M1 <- apply(S1,1,function(x) Z1 %*% x )

M2 <- apply(S2,1,function(x) Z2 %*% x ) + Mb

Mout <- apply(M1,2,function(x) M2 + as.vector(x))

as.vector(Mout) + rnorm(length(Mout), mean = 0 , sd = 0.27)

because the random numbers are added after the matrix multiplication (ie are not involved in any calculation), you can just put them in on the end.

Also note that you can't add a smaller matrix to a larger one, but if you make it a vector first then R will recycle it as necessary. So when Mb (a vector of length 4) is added to a matrix with 4 rows and n columns, it is recycled n times.

这篇关于在R for-loops中从较小的矩阵逐步创建一个大矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆