1
votes

Below is a 6x2 matrix.

 1.178214   0.1723376
 1.121873   0.1982651
 1.120752   0.2470480
 1.121873   0.3284342
 1.165325   0.5079993
 1.128625   1.0000000

I am trying to generate two separate lower triangular matrices using the above 6x2 matrix.

The expected first lower triangular matrix is below

w1         w2         w3         w4         w5          w6
0.1723376  0          0          0          0           0 
0.1982651  0.1723376  0          0          0           0
0.2470480  0.1982651  0.1723376  0          0           0
0.3284342  0.2470480  0.1982651  0.1723376  0           0
0.5079993  0.3284342  0.2470480  0.1982651  0.1723376   0
1.0000000  0.5079993  0.3284342  0.2470480  0.1982651   0.1723376  

The logic is that the values in

  row1 column w1,(R1:W1 0.1723376) 
  row2 column w2 (R2:W2 0.1723376)
  row3 column w3 (R3:W3 0.1723376)
  row4 column w4 (R4:W4 0.1723376)
  row5 column w5 (R5:W5 0.1723376)
  row6 column w6 (R6:W6 0.1723376)

  row2 column w1,(R2:W1 0.1982651) 
  row3 column w2 (R3:W2 0.1982651)
  row4 column w3 (R4:W3 0.1982651)
  row5 column w4 (R5:W4 0.1982651)
  row6 column w5 (R6:W5 0.1982651)

are similar, rest follow this pattern.

The second lower triangular matrix is bit more complicated and involves the 1st column from the initial 6x2 matrix. The expected matrix is below

w1         w2         w3         w4         w5          w6
0.1723376  0          0          0          0           0 
0.1640966  0.1982651  0          0           0
0.1639326  0.1980670  0.2470480  0          0           0
0.1640966  0.1982651  0.2472952  0.2472952  0           0
0.1704523  0.2059443  0.2568733  0.2568733  0.3411550   0
0.1650842  0.1994584  0.2487835  0.2487835  0.3304109   0.4920007

The logic is as follows.

The column1 w1 elements are calculated as follows

1.178214 / (1.178214 + 1.121873 + 1.120752 + 1.121873 + 1.165325 + 1.128625)
1.121873 / (1.178214 + 1.121873 + 1.120752 + 1.121873 + 1.165325 + 1.128625)
1.120752 / (1.178214 + 1.121873 + 1.120752 + 1.121873 + 1.165325 + 1.128625)
1.121873 / (1.178214 + 1.121873 + 1.120752 + 1.121873 + 1.165325 + 1.128625)
1.165325 / (1.178214 + 1.121873 + 1.120752 + 1.121873 + 1.165325 + 1.128625)
1.128625 / (1.178214 + 1.121873 + 1.120752 + 1.121873 + 1.165325 + 1.128625)

The column2 w2 elements are estimated as follows

0.1982651 = 1.121873 / (1.121873 + 1.120752 + 1.121873 + 1.165325 + 1.128625)

0.198067 = 1.120752 / (1.121873 + 1.120752 + 1.121873 + 1.165325 + 1.128625)

0.1982651 = 1.121873 / (1.121873 + 1.120752 + 1.121873 + 1.165325 + 1.128625)

0.2059443 = 1.165325 / (1.121873 + 1.120752 + 1.121873 + 1.165325 + 1.128625)

0.1994584 = 1.128625 / (1.121873 + 1.120752 + 1.121873 + 1.165325 + 1.128625)

The column3 w3 elements are estimated as follows

0.247048 = 1.120752 / (1.120752 + 1.121873 + 1.165325 + 1.128625)

0.2472952 = 1.121873 / (1.120752 + 1.121873 + 1.165325 + 1.128625)

0.2568733 = 1.165325 / (1.120752 + 1.121873 + 1.165325 + 1.128625)

0.2487835 = 1.128625 / (1.120752 + 1.121873 + 1.165325 + 1.128625)

Need help generating these matrices in r.

1

1 Answers

0
votes

Maybe you can try the code below

M1 <- matrix(0,nrow = 6,ncol = 6)
M1[lower.tri(M1,diag = TRUE)] <- unlist(sapply(6:1,function(k) head(m[,2],k)))

M2 <- matrix(0,nrow = 6,ncol = 6)
M2[lower.tri(M2,diag = TRUE)] <- unlist(sapply(6:1, function(k) tail(m[,1],k)))
M2 <- t(t(M2)/colSums(M2))

such that

> M1
          [,1]      [,2]      [,3]      [,4]      [,5]      [,6]
[1,] 0.1723376 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
[2,] 0.1982651 0.1723376 0.0000000 0.0000000 0.0000000 0.0000000
[3,] 0.2470480 0.1982651 0.1723376 0.0000000 0.0000000 0.0000000
[4,] 0.3284342 0.2470480 0.1982651 0.1723376 0.0000000 0.0000000
[5,] 0.5079993 0.3284342 0.2470480 0.1982651 0.1723376 0.0000000
[6,] 1.0000000 0.5079993 0.3284342 0.2470480 0.1982651 0.1723376

> M2
          [,1]      [,2]      [,3]      [,4]      [,5] [,6]
[1,] 0.1723376 0.0000000 0.0000000 0.0000000 0.0000000    0
[2,] 0.1640966 0.1982651 0.0000000 0.0000000 0.0000000    0
[3,] 0.1639326 0.1980670 0.2470480 0.0000000 0.0000000    0
[4,] 0.1640966 0.1982651 0.2472952 0.3284342 0.0000000    0
[5,] 0.1704523 0.2059443 0.2568733 0.3411550 0.5079993    0
[6,] 0.1650842 0.1994584 0.2487835 0.3304109 0.4920007    1

DATA

m <- structure(c(1.178214, 1.121873, 1.120752, 1.121873, 1.165325, 
1.128625, 0.1723376, 0.1982651, 0.247048, 0.3284342, 0.5079993, 
1), .Dim = c(6L, 2L))