Evan Cooch
2017-Sep-28 13:10 UTC
[R] building random matrices from vectors of random parameters
Thanks for both the mapply and array approaches! However, although intended to generate the same result, they don't: # mapply approach n = 3 sa <- rnorm(n,0.8,0.1) so <- rnorm(n,0.5,0.1) m <- rnorm(n,1.2,0.1) mats = mapply(function(sa1, so1, m1) matrix(c(0,sa1*m1,so1,sa1),2,2,byrow=T), sa, so, m, SIMPLIFY = FALSE) print(mats) [[1]] ????????? [,1]????? [,2] [1,] 0.0000000 0.8643679 [2,] 0.4731249 0.7750431 [[2]] ????????? [,1]????? [,2] [1,] 0.0000000 0.8838286 [2,] 0.5895258 0.7880983 [[3]] ????????? [,1]????? [,2] [1,] 0.0000000 1.1491560 [2,] 0.4947322 0.9744166 Now, the array approach: # array approach ms <- array(c(rep(0, 3),sa*m,so,sa), c(3, 2, 2)) for (i in 1:n) { print(ms[i,,]) ????????? [,1]????? [,2] [1,] 0.0000000 0.4731249 [2,] 0.8643679 0.7750431 ????????? [,1]????? [,2] [1,] 0.0000000 0.5895258 [2,] 0.8838286 0.7880983 ???????? [,1]????? [,2] [1,] 0.000000 0.4947322 [2,] 1.149156 0.9744166 These matrices are the transpose of those returned by the mapply approach. To see if one approach or the other is 'confused', I simply rerun setting sd=0 for the parameters -- thus, every matrix will be the same. The correct matrix would be: ???? [,1] [,2] [1,]? 0.0 0.96 [2,]? 0.5 0.80 In fact, this is what is returned by the mapply approach, while the array approach returns the transpose. I gather the 'missing step' is to use aperm, but haven't figured out how to get that to work...yet. On 9/28/2017 5:11 AM, Duncan Murdoch wrote:> ms <- array(c(rep(0, 5),sa*m,so,sa), c(5, 2, 2))[[alternative HTML version deleted]]
Evan Cooch
2017-Sep-28 13:14 UTC
[R] building random matrices from vectors of random parameters
> > In fact, this is what is returned by the mapply approach, while the > array approach returns the transpose. I gather the 'missing step' is > to use aperm, but haven't figured out how to get that to work...yet.ms <- array(c(rep(0, 3),sa*m,so,sa), c(3, 2, 2)) ms_new <- aperm(ms,c(1,3,2)); for (i in 1:n) { print(ms_new[i,,]) } ???? [,1] [,2] [1,]? 0.0 0.96 [2,]? 0.5 0.80 ???? [,1] [,2] [1,]? 0.0 0.96 [2,]? 0.5 0.80 ???? [,1] [,2] [1,]? 0.0 0.96 [2,]? 0.5 0.80> > > On 9/28/2017 5:11 AM, Duncan Murdoch wrote: >> ms <- array(c(rep(0, 5),sa*m,so,sa), c(5, 2, 2)) >[[alternative HTML version deleted]]
Duncan Murdoch
2017-Sep-28 15:55 UTC
[R] building random matrices from vectors of random parameters
On 28/09/2017 9:10 AM, Evan Cooch wrote:> Thanks for both the mapply and array approaches! However, although > intended to generate the same result, they don't: > > # mapply approach > > n = 3 > sa <- rnorm(n,0.8,0.1) > so <- rnorm(n,0.5,0.1) > m <- rnorm(n,1.2,0.1) > mats = mapply(function(sa1, so1, m1) > matrix(c(0,sa1*m1,so1,sa1),2,2,byrow=T), sa, so, m, SIMPLIFY = FALSE) > > print(mats) > > [[1]] > ????????? [,1]????? [,2] > [1,] 0.0000000 0.8643679 > [2,] 0.4731249 0.7750431 > > [[2]] > ????????? [,1]????? [,2] > [1,] 0.0000000 0.8838286 > [2,] 0.5895258 0.7880983 > > [[3]] > ????????? [,1]????? [,2] > [1,] 0.0000000 1.1491560 > [2,] 0.4947322 0.9744166 > > > Now, the array approach: > > # array approach > > ms <- array(c(rep(0, 3),sa*m,so,sa), c(3, 2, 2)) > > for (i in 1:n) { print(ms[i,,]) > > ????????? [,1]????? [,2] > [1,] 0.0000000 0.4731249 > [2,] 0.8643679 0.7750431 > > ????????? [,1]????? [,2] > [1,] 0.0000000 0.5895258 > [2,] 0.8838286 0.7880983 > > ???????? [,1]????? [,2] > [1,] 0.000000 0.4947322 > [2,] 1.149156 0.9744166 > > > These matrices are the transpose of those returned by the mapply > approach. To see if one approach or the other is 'confused', I simply > rerun setting sd=0 for the parameters -- thus, every matrix will be the > same. The correct matrix would be: > > ???? [,1] [,2] > [1,]? 0.0 0.96 > [2,]? 0.5 0.80 > > > In fact, this is what is returned by the mapply approach, while the > array approach returns the transpose. I gather the 'missing step' is to > use aperm, but haven't figured out how to get that to work...yet. > > > On 9/28/2017 5:11 AM, Duncan Murdoch wrote: >> ms <- array(c(rep(0, 5),sa*m,so,sa), c(5, 2, 2)) >Sorry about that -- I didn't notice the "byrow = T" in your original. Duncan Murdoch
Evan Cooch
2017-Sep-28 16:10 UTC
[R] building random matrices from vectors of random parameters
Sure -- thanks -- only took me 3-4 attempts to get aperm to work (as opposed to really thinking hard about how it works ;-) On 9/28/2017 11:55 AM, Duncan Murdoch wrote:> On 28/09/2017 9:10 AM, Evan Cooch wrote: >> Thanks for both the mapply and array approaches! However, although >> intended to generate the same result, they don't: >> >> # mapply approach >> >> n = 3 >> sa <- rnorm(n,0.8,0.1) >> so <- rnorm(n,0.5,0.1) >> m <- rnorm(n,1.2,0.1) >> mats = mapply(function(sa1, so1, m1) >> matrix(c(0,sa1*m1,so1,sa1),2,2,byrow=T), sa, so, m, SIMPLIFY = FALSE) >> >> print(mats) >> >> [[1]] >> ?????????? [,1]????? [,2] >> [1,] 0.0000000 0.8643679 >> [2,] 0.4731249 0.7750431 >> >> [[2]] >> ?????????? [,1]????? [,2] >> [1,] 0.0000000 0.8838286 >> [2,] 0.5895258 0.7880983 >> >> [[3]] >> ?????????? [,1]????? [,2] >> [1,] 0.0000000 1.1491560 >> [2,] 0.4947322 0.9744166 >> >> >> Now, the array approach: >> >> # array approach >> >> ms <- array(c(rep(0, 3),sa*m,so,sa), c(3, 2, 2)) >> >> for (i in 1:n) { print(ms[i,,]) >> >> ?????????? [,1]????? [,2] >> [1,] 0.0000000 0.4731249 >> [2,] 0.8643679 0.7750431 >> >> ?????????? [,1]????? [,2] >> [1,] 0.0000000 0.5895258 >> [2,] 0.8838286 0.7880983 >> >> ????????? [,1]????? [,2] >> [1,] 0.000000 0.4947322 >> [2,] 1.149156 0.9744166 >> >> >> These matrices are the transpose of those returned by the mapply >> approach. To see if one approach or the other is 'confused', I simply >> rerun setting sd=0 for the parameters -- thus, every matrix will be >> the same. The correct matrix would be: >> >> ????? [,1] [,2] >> [1,]? 0.0 0.96 >> [2,]? 0.5 0.80 >> >> >> In fact, this is what is returned by the mapply approach, while the >> array approach returns the transpose. I gather the 'missing step' is >> to use aperm, but haven't figured out how to get that to work...yet. >> >> >> On 9/28/2017 5:11 AM, Duncan Murdoch wrote: >>> ms <- array(c(rep(0, 5),sa*m,so,sa), c(5, 2, 2)) >> > > > Sorry about that -- I didn't notice the "byrow = T" in your original. > > Duncan Murdoch >[[alternative HTML version deleted]]
Maybe Matching Threads
- building random matrices from vectors of random parameters
- building random matrices from vectors of random parameters
- building random matrices from vectors of random parameters
- building random matrices from vectors of random parameters
- building random matrices from vectors of random parameters