/
rvmatrix_rvarray.R
83 lines (76 loc) · 2.71 KB
/
rvmatrix_rvarray.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#' @rdname rvarray
#' @param nrow the desired number of rows.
#' @param ncol the desired number of columns.
#' @param byrow logical. If \code{FALSE} (the default) the matrix is filled by
#' columns, otherwise the matrix is filled by rows.
#' @export
rvmatrix <- function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL) {
data <- as.vector(data)
if (missing(nrow))
nrow <- ceiling(length(data)/ncol)
else if (missing(ncol))
ncol <- ceiling(length(data)/nrow)
if (byrow) {
X <- t(rvarray(data, c(ncol, nrow), dimnames=dimnames))
} else {
X <- rvarray(data, c(nrow, ncol), dimnames=dimnames)
}
return(X)
}
#' Matrices and Arrays of Random Vectors
#'
#' Arrange a given random vector into a matrix or array form.
#'
#' These are 'rv' compatible versions of the functions \code{\link{matrix}} and
#' \code{\link{array}}.
#'
#' The function \code{rvmatrix} generates the random variable matrix via an
#' \code{rvarray} call.
#'
#' The \code{rvarray} function calls first \code{array} to set the dimensions
#' of the argument \code{data} and then coerces the resulting array object to
#' an 'rv' object.
#'
#' @aliases rvmatrix rvarray is.matrix.rv as.matrix.rv
#' @param data an optional data vector.
#' @param dimnames A dimnames attribute for the matrix: a list of length 2
#' giving the row and column names respectively.
#' @param dim 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.
#' @author Jouni Kerman \email{jouni@@kerman.com}
#' @seealso To plot random matrices, see \code{\link{mlplot}}.
#' @references Kerman, J. and Gelman, A. (2007). Manipulating and Summarizing
#' Posterior Simulations Using Random Variable Objects. Statistics and
#' Computing 17:3, 235-244.
#'
#' See also \code{vignette("rv")}.
#' @keywords classes
#' @examples
#'
#' 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)
#' print(E(theta))
#'
#' @export rvarray
rvarray <- function (data = NA, dim = length(data), dimnames = NULL) {
as.rv(array(data = data, dim = dim, dimnames = dimnames))
}
#' @noRd
#' @method is.matrix rv
#' @param x an R object.
is.matrix.rv <- function (x) {
dx <- dim(x)
return((!is.null(dx)) && length(dx)==2)
}
as.matrix.rv <- function (x, ...) {
if (is.matrix(x)) {
return(x)
}
dn <- if (!is.null(names(x))) list(names(x), NULL) else NULL
rvarray(x, dim=c(length(x), 1), dimnames=dn)
}