【问题标题】:Random generated matrices in RR中的随机生成矩阵
【发布时间】:2017-03-26 11:13:49
【问题描述】:

我想在 R 中创建两个矩阵,这样矩阵 x 的元素应该是任何分布中的随机元素,然后我计算这个 2*2 矩阵的 colSums 和 rowSums。然后,我想从任何分布,使得第一个子集 2*2 元素是随机的,然后第三行和第三列是行和列元素的总和,然后第四行和第四列是随机的,使得 rowSums 和 colSums 为 3:4等于第一个矩阵 x 的 colSums 和 row Sums。如何在 R 中创建两个具有这些属性的随机矩阵?

     x=matrix(c(100, 50, 0, 250), nrow=2)
     csum1=colSums(x)
     rsum1=rowSums(x)

     y=matrix(c(15, 60, 75, 25,60, 25, 85, 215, 75, 85, 160, 240, 
     75, 165, 240, 0), ncol=4) 1
     csum2=colSums(y[3:4,c(-3,-4)])
     rsum2=rowSums(y[c(-3,-4),3:4])

    > x
     [,1] [,2]
 [1,]  100    0
 [2,]   50  250
    > y
     [,1] [,2] [,3] [,4]
[1,]   15   60   75   75
[2,]   60   25   85  165
[3,]   75   85  160  240
[4,]   25  215  240    0

【问题讨论】:

  • 如何达到 240 年代?由于您在矩阵 x 中没有第三行/列,我不明白它们来自哪里

标签: r


【解决方案1】:

我正在尝试使用正态分布中的随机数。使用 MASS 包中的 mvrnorm() 进行相同操作。?mvrnorm 了解更多信息

library(MASS)

mu1 = 2  #mean for first distribution
mu2 = 3  #mean for second distribution
covm = matrix(c(2,1,1,1), ncol = 2) # covariance matrix

set.seed(1000)
x = mvrnorm(n = 2, mu = c(2,3), Sigma = covm)
colsumx = colSums(x)
rowsumx = rowSums(x)


set.seed(2000)
y = mvrnorm(n = 2, mu = c(2,3), Sigma = matrix(c(2,1,1,1), ncol = 2))
y = cbind(y, rowSums(y))
y = rbind(y, colSums(y))
y = rbind(y, c(colsumx - y[3,1:2], sum(colsumx) - y[3,3]))
y = cbind(y, c(rowsumx - y[1:2,3], sum(rowsumx) - y[3,3], 0))

# > x
#          [,1]     [,2]
# [1,] 2.626924 3.357580
# [2,] 3.867469 3.689616
# > y
#             [,1]     [,2]      [,3]       [,4]
# [1,]  3.46444151 3.258343  6.722785 -0.7382805
# [2,]  3.05148893 2.384737  5.436226  2.1208596
# [3,]  6.51593044 5.643080 12.159011  1.3825791
# [4,] -0.02153748 1.404117  1.382579  0.0000000

【讨论】:

    【解决方案2】:

    我认为您只需要生成两个2x2 随机矩阵,然后就可以通过它们获得所有其他值:

    set.seed(1234) # just for reproducibility
    
    # generate the two 2x2 matrices with your desired distribution
    # (e.g. here I'm generating integers using the uniform distribution)
    x <- matrix(as.integer(runif(n = 4,min=10,max=100)),nrow=2)
    y22 <- matrix(as.integer(runif(n = 4,min=10,max=100)),nrow=2)
    
    # compute col and row sums of first matrix
    csumX=colSums(x)
    rsumX=rowSums(x)
    
    # initialize the 4x4 matrix with NAs and set the upper-left 2x2 corner
    y <- matrix(NA,nrow=4,ncol=4)
    y[1:2,1:2] <- y22
    
    # define the values of 3rd row and colum as sum of other values
    # (exluding elements on the 4th row and column)
    y[1:2,3] <- colSums(y[1:2,1:2])
    y[3,1:2] <- rowSums(y[1:2,1:2])
    y[3,3] <- sum(y[1:2,1:2]) # by definition y[3,3] == y[3,1]+y[3,2] == y[1,3]+y[2,3]
    
    # define first two elements of 4th row and column
    y[4,1] <- csumX[1] - y[3,1]
    y[4,2] <- csumX[2] - y[3,2]
    
    y[1,4] <- rsumX[1] - y[1,3]
    y[2,4] <- rsumX[2] - y[2,3]
    
    # complete 3rd row and column values
    y[4,3] <- y[4,1] + y[4,2] # these two will be equal by definition
    y[3,4] <- y[1,4] + y[2,4]
    
    # element y[4,4] (always zero ?)
    y[4,4] <- 0
    

    结果:

    > x
         [,1] [,2]
    [1,]   20   64
    [2,]   66   66
    > y
         [,1] [,2] [,3] [,4]
    [1,]   87   10  154  -70
    [2,]   67   30   40   92
    [3,]   97   97  194   22
    [4,]  -11   33   22    0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-14
      • 1970-01-01
      相关资源
      最近更新 更多