[R] building random matrices from vectors of random parameters
Peter Langfelder
peter.langfelder at gmail.com
Thu Sep 28 06:19:54 CEST 2017
I would try something like
n = 5
a <- 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),
a, so, m, SIMPLIFY = FALSE)
> mats
[[1]]
[,1] [,2]
[1,] 0.0000000 0.9129962
[2,] 0.4963598 0.7067311
[[2]]
[,1] [,2]
[1,] 0.0000000 1.0150316
[2,] 0.5489887 0.8469046
[[3]]
[,1] [,2]
[1,] 0.0000000 0.9516137
[2,] 0.3724521 0.8306535
[[4]]
[,1] [,2]
[1,] 0.0000000 1.0525355
[2,] 0.8075108 0.8314638
[[5]]
[,1] [,2]
[1,] 0.0000000 0.9400074
[2,] 0.4803386 0.7901753
On Wed, Sep 27, 2017 at 5:47 PM, Evan Cooch <evan.cooch at gmail.com> wrote:
> Suppose I have interest in a matrix with the following symbolic structure
> (specified by 3 parameters: sa, so, m):
>
> matrix(c(0,sa*m,so,sa),2,2,byrow=T)
>
> What I can't figure out is how to construct a series of matrices, where the
> elements/parameters are rnorm values. I'd like to construct separate
> matrices, with each matrix in the series using the 'next random parameter
> value'. While the following works (for generating, say, 5 such random
> matrices)
>
> replicate(5,matrix(c(0,rnorm(1,0.8,0.1)*rnorm(1,1.2,0.1),rnorm(1,0.5,0.1),rnorm(1,0.8,0.1)),2,2,byrow=T))
>
> its inelegant, and a real pain if the matrix gets large (say, 20 x 20).
>
> I'm wondering if there is an easier way. I tried
>
>> sa <- rnorm(5,0.8,0.1)
>> so <- rnorm(5,0.5,0.1)
>> m <- rnorm(5,1.2,0.1)
>
> matrix(c(0,sa*m,so,sa),2,2,byrow=T)
>
> but that only returns a single matrix, not 5 matrices as I'd like. I also
> tried several variants of the 'replicate' approach (above), but didn't
> stumble across anything that seemed to work.
>
> So, is there a better way than something like:
>
> replicate(5,matrix(c(0,rnorm(1,0.8,0.1)*rnorm(1,1.2,0.1),rnorm(1,0.5,0.1),rnorm(1,0.8,0.1)),2,2,byrow=T))
>
> Many thanks in advance...
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list