Convert a 2-dimensional tabular object where each row stores a flattened square matrix to a 3-dimensional array of square matrices and vice versa. This allows multiple transition matrices to be stored as either tabular objects (e.g., matrices, data frames, etc) or as arrays.

as_array3(x)

as_tbl2(
  x,
  output = c("data.table", "data.frame", "matrix", "tpmatrix"),
  prefix = "",
  sep = "_"
)

Arguments

x

For as_array3() a 2-dimensional tabular object where each row stores a flattened square matrix ordered rowwise. Reasonable classes are matrix, data.frame, data.table, and tpmatrix. For as_tbl2() a 3-dimensional array where each slice is a square matrix.

output

The class of the object returned by the function. Either a data.table, data.frame, matrix, or tpmatrix.

prefix, sep

Arguments passed to tpmatrix_names() for naming the transition probability columns. The states argument is based on the column names (i.e., names of the second dimension) of array; if NULL, then states are named s1, ..., sh where h is the number of states.

Value

For as_array3() a 3-dimensional array of square matrices; for as_tbl2() a 2-dimensional tabular object as specified by output.

See also

Examples

p_12 <- c(.7, .6) pmat <- tpmatrix( C, p_12, 0, 1 ) pmat
#> s1_s1 s1_s2 s2_s1 s2_s2 #> 1: 0.3 0.7 0 1 #> 2: 0.4 0.6 0 1
as_array3(pmat)
#> , , 1 #> #> s1 s2 #> s1 0.3 0.7 #> s2 0.0 1.0 #> #> , , 2 #> #> s1 s2 #> s1 0.4 0.6 #> s2 0.0 1.0 #>
as_array3(as.matrix(pmat))
#> , , 1 #> #> [,1] [,2] #> [1,] 0.3 0.7 #> [2,] 0.0 1.0 #> #> , , 2 #> #> [,1] [,2] #> [1,] 0.4 0.6 #> [2,] 0.0 1.0 #>
as_tbl2(as_array3(pmat))
#> s1_s1 s1_s2 s2_s1 s2_s2 #> 1: 0.3 0.7 0 1 #> 2: 0.4 0.6 0 1
as_tbl2(as_array3(pmat), prefix = "p_", sep = ".")
#> p_s1.s1 p_s1.s2 p_s2.s1 p_s2.s2 #> 1: 0.3 0.7 0 1 #> 2: 0.4 0.6 0 1