Arrange a given random vector into a matrix or array form.
rvmatrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)
rvarray(data = NA, dim = length(data), dimnames = NULL)
an optional data vector.
the desired number of rows.
the desired number of columns.
logical. If FALSE
(the default) the matrix is filled by
columns, otherwise the matrix is filled by rows.
A dimnames attribute for the matrix: a list of length 2 giving the row and column names respectively.
the dim attribute for the array to be created, that is a vector of length one or more giving the maximal indices in each dimension.
These are 'rv' compatible versions of the functions matrix
and
array
.
The function rvmatrix
generates the random variable matrix via an
rvarray
call.
The rvarray
function calls first array
to set the dimensions
of the argument data
and then coerces the resulting array object to
an 'rv' object.
Kerman, J. and Gelman, A. (2007). Manipulating and Summarizing Posterior Simulations Using Random Variable Objects. Statistics and Computing 17:3, 235-244.
See also vignette("rv")
.
To plot random matrices, see mlplot
.
n.rows <- 3; n.cols <- 4; n <- (n.rows*n.cols)
mu.true <- rnorm(1:n.rows, mean=1:n.rows, sd=1)
theta <- rvmatrix(rvnorm(n=n.cols, mean=mu.true, sd=0.5), nrow=n.rows)
col.labels <- paste("Time", 1:n.cols, sep=":")
row.labels <- paste("Unit", 1:n.rows, sep=":")
dimnames(theta) <- list(row.labels, col.labels)
print(theta)
#> row col mean sd 1% 2.5% 25% 50% 75% 97.5% 99% sims
#> [1,1] Unit:1 Time:1 : 1.00 0.48 -0.101 0.063 0.66 0.98 1.31 1.9 2.1 200
#> [2,1] Unit:2 Time:1 : 0.67 0.50 -0.393 -0.340 0.31 0.64 0.99 1.7 1.9 200
#> [3,1] Unit:3 Time:1 : 4.20 0.54 2.981 3.093 3.85 4.22 4.58 5.2 5.3 200
#> [1,2] Unit:1 Time:2 : 0.95 0.52 -0.180 -0.079 0.63 0.99 1.30 1.8 1.9 200
#> [2,2] Unit:2 Time:2 : 0.68 0.51 -0.554 -0.426 0.39 0.66 1.02 1.5 1.6 200
#> [3,2] Unit:3 Time:2 : 4.19 0.50 3.082 3.204 3.85 4.20 4.51 5.2 5.3 200
#> [1,3] Unit:1 Time:3 : 0.98 0.49 0.016 0.132 0.60 1.03 1.30 1.9 2.0 200
#> [2,3] Unit:2 Time:3 : 0.73 0.49 -0.364 -0.314 0.43 0.78 1.07 1.6 1.7 200
#> [3,3] Unit:3 Time:3 : 4.10 0.49 3.098 3.278 3.78 4.10 4.47 5.0 5.1 200
#> [1,4] Unit:1 Time:4 : 1.01 0.53 -0.057 0.148 0.61 1.03 1.37 2.0 2.3 200
#> [2,4] Unit:2 Time:4 : 0.64 0.50 -0.322 -0.270 0.27 0.65 0.99 1.6 1.7 200
#> [3,4] Unit:3 Time:4 : 4.14 0.48 3.032 3.137 3.82 4.18 4.42 5.0 5.2 200
print(E(theta))
#> Time:1 Time:2 Time:3 Time:4
#> Unit:1 0.9953021 0.9466902 0.9786610 1.0136122
#> Unit:2 0.6703186 0.6807653 0.7329507 0.6438023
#> Unit:3 4.2039574 4.1868029 4.1006313 4.1358105