Multiple Lower Triangular or Symmetric Matrices
ltMatrices.RdA class representing multiple lower triangular or symmetric matrices and some methods.
Usage
ltMatrices(object, diag = FALSE, byrow = FALSE, names = TRUE)
syMatrices(object, diag = FALSE, byrow = FALSE, names = TRUE)
# S3 method for class 'ltMatrices'
as.array(x, symmetric = FALSE, ...)
# S3 method for class 'syMatrices'
as.array(x, ...)
# S3 method for class 'ltMatrices'
diagonals(x, ...)
# S3 method for class 'syMatrices'
diagonals(x, ...)
# S3 method for class 'matrix'
diagonals(x, ...)
# S3 method for class 'integer'
diagonals(x, ...)
diagonals(x) <- value
# S3 method for class 'ltMatrices'
diagonals(x) <- value
# S3 method for class 'syMatrices'
diagonals(x) <- value
# S3 method for class 'ltMatrices'
solve(a, b, transpose = FALSE, ...)
# S3 method for class 'syMatrices'
chol(x, ...)
# S3 method for class 'chol'
aperm(a, perm, ...)
# S3 method for class 'invchol'
aperm(a, perm, ...)
# S3 method for class 'ltMatrices'
aperm(a, perm, ...)
# S3 method for class 'syMatrices'
aperm(a, perm, ...)
deperma(chol = solve(invchol), permuted_chol = solve(permuted_invchol),
invchol, permuted_invchol, perm, score_schol)
# S3 method for class 'ltMatrices'
Mult(x, y, transpose = FALSE, ...)
# S3 method for class 'syMatrices'
Mult(x, y, ...)
Tcrossprod(x, diag_only = FALSE)
Crossprod(x, diag_only = FALSE)
# S3 method for class 'ltMatrices'
tcrossprod(x, y = NULL, ...)
# S3 method for class 'syMatrices'
tcrossprod(x, y = NULL, ...)
# S3 method for class 'ltMatrices'
crossprod(x, y = NULL, ...)
# S3 method for class 'syMatrices'
crossprod(x, y = NULL, ...)
logdet(x)
Lower_tri(x, diag = FALSE, byrow = attr(x, "byrow"))
is.ltMatrices(x)
is.syMatrices(x)
as.ltMatrices(x)
# S3 method for class 'ltMatrices'
as.ltMatrices(x)
# S3 method for class 'syMatrices'
as.ltMatrices(x)
as.syMatrices(x)
is.chol(x)
is.invchol(x)
as.chol(x)
as.invchol(x)
chol2cov(x)
invchol2chol(x)
chol2invchol(x)
invchol2cov(x)
invchol2pre(x)
chol2pre(x)
Dchol(x, D = 1 / sqrt(Tcrossprod(x, diag_only = TRUE)))
invcholD(x, D = sqrt(Tcrossprod(solve(x), diag_only = TRUE)))
chol2cor(x)
invchol2cor(x)
chol2pc(x)
invchol2pc(x)
vectrick(C, S, A, transpose = c(TRUE, TRUE))
standardize(chol, invchol)
destandardize(chol = solve(invchol), invchol, score_schol)
as.ltMatrices(x)Arguments
- object
a
matrixrepresenting the lower triagular elements of \(N\) lower triangular matrix, each of dimension \(J \times J\). Dimensions ofobjectdepend ondiag: With diagonal elements,objectis a \(J(J+1)/2 \times N\) matrix, otherwise, the number of rows is \(J(J - 1) / 2\).- diag
logical,
objectcontains diagonal elements ifTRUE, otherwise unit diagonal elements are assumed.- byrow
logical,
objectrepresents matrices in row-major order ifTRUEor, otherwise, in column-major order.- names
logical or character vector of length \(J\).
- symmetric
logical, object is interpreted as a symmetric matrix if
TRUE.- diag_only
logical, compute diagonal elements of crossproduct only if
TRUE.- x,chol,invchol,permuted_chol,permuted_invchol
object of class
ltMatricesorsyMatrices(forchol).- value
a matrix of diagonal elements to be assigned (of dimension \(J \times N\)).
- a
object of class
ltMatrices.- perm
a permutation of the covariance matrix corresponding to
a.- D
a matrix (of dimension \(J \times N\)) of diagonal elements to be multiplied with.
- y
matrix with \(J\) rows.
- b
matrix with \(J\) rows.
- C
an object of class
ltMatrices.- S
an object of class
ltMatricesor a matrix with \(J^2\) rows representing multiple \(J x J\) matrices (columns of vec operators).- A
an object of class
ltMatrices.- transpose
a logical of length two indicating if
AorBshall be transposed invectrick. Forsolve, this argument being true computessolve(t(a), b)(in absence of at()method forltMatricesobjects).- score_schol
score matrix for a standardized
cholobject.- ...
additional arguments, currently ignored.
Details
ltMatrices interprets a matrix as lower triangular elements of
multiple lower triangular matrices. The corresponding class can be used to
store such matrices efficiently. Matrix multiplications, solutions to linear
systems, explicite inverses, and crossproducts can be computed based on such
objects. Details can be found in the lmvnorm_src package vignette.
syMatrices only store the lower triangular parts of multiple
symmetric matrices.
Value
The constructor ltMatrices returns objects of class ltMatrices
with corresponding methods. The constructor syMatrices returns objects of class
syMatrices with a reduced set of methods.
Examples
J <- 4L
N <- 2L
dm <- paste0("d", 1:J)
xm <- paste0("x", 1:N)
(C <- ltMatrices(matrix(runif(N * J * (J + 1) / 2),
ncol = N, dimnames = list(NULL, xm)),
diag = TRUE, names = dm))
#> , , x1
#>
#> d1 d2 d3 d4
#> d1 0.814133390 . . .
#> d2 0.613658734 0.564057670 . .
#> d3 0.733889880 0.356201405 0.129652295 .
#> d4 0.115458925 0.738284444 0.465802073 0.887361601
#>
#> , , x2
#>
#> d1 d2 d3 d4
#> d1 0.143109764 . . .
#> d2 0.510915912 0.205885270 . .
#> d3 0.880502964 0.516684529 0.101740554 .
#> d4 0.078370095 0.005416701 0.932764725 0.161109834
#>
## dimensions and names
dim(C)
#> [1] 2 4 4
dimnames(C)
#> [[1]]
#> [1] "x1" "x2"
#>
#> [[2]]
#> [1] "d1" "d2" "d3" "d4"
#>
#> [[3]]
#> [1] "d1" "d2" "d3" "d4"
#>
names(C)
#> [1] "d1.d1" "d2.d1" "d3.d1" "d4.d1" "d2.d2" "d3.d2" "d4.d2" "d3.d3" "d4.d3"
#> [10] "d4.d4"
## subset
C[,2:3]
#> , , x1
#>
#> d2 d3
#> d2 0.5640577 .
#> d3 0.3562014 0.1296523
#>
#> , , x2
#>
#> d2 d3
#> d2 0.2058853 .
#> d3 0.5166845 0.1017406
#>
## multiplication
y <- matrix(runif(N * J), nrow = J)
Mult(C, y)
#> x1 x2
#> d1 0.4463154 0.1179544
#> d2 0.5494554 0.4645945
#> d3 0.6638353 0.8481666
#> d4 1.4986323 0.3365020
C
#> , , x1
#>
#> d1 d2 d3 d4
#> d1 0.814133390 . . .
#> d2 0.613658734 0.564057670 . .
#> d3 0.733889880 0.356201405 0.129652295 .
#> d4 0.115458925 0.738284444 0.465802073 0.887361601
#>
#> , , x2
#>
#> d1 d2 d3 d4
#> d1 0.143109764 . . .
#> d2 0.510915912 0.205885270 . .
#> d3 0.880502964 0.516684529 0.101740554 .
#> d4 0.078370095 0.005416701 0.932764725 0.161109834
#>
## solve
solve(C)
#> , , x1
#>
#> d1 d2 d3 d4
#> d1 1.228300 . . .
#> d2 -1.336312 1.772868 . .
#> d3 -3.281398 -4.870706 7.712937 .
#> d4 2.674492 1.081750 -4.048746 1.126936
#>
#> , , x2
#>
#> d1 d2 d3 d4
#> d1 6.987643 . . .
#> d2 -17.340231 4.857074 . .
#> d3 27.587706 -24.666418 9.828922 .
#> d4 -162.538400 142.645886 -56.905725 6.206946
#>
solve(C, y)
#> x1 x2
#> d1 0.67336530 5.759378
#> d2 -0.06297391 -13.266340
#> d3 3.91509407 18.813887
#> d4 -1.20098615 -105.548730
## tcrossprod
Tcrossprod(C)
#> , , x1
#>
#> d1 d2 d3 d4
#> d1 0.66281318 0.4996001 0.5974843 0.09399897
#> d2 0.49960007 0.6947381 0.6512761 0.48728738
#> d3 0.59748426 0.6512761 0.6822835 0.40810440
#> d4 0.09399897 0.4872874 0.4081044 1.56277687
#>
#> , , x2
#>
#> d1 d2 d3 d4
#> d1 0.02048040 0.07311706 0.1260086 0.01121553
#> d2 0.07311706 0.30342381 0.5562407 0.04115575
#> d3 0.12600857 0.55624071 1.0525995 0.16670383
#> d4 0.01121553 0.04115575 0.1667038 0.90217762
#>
tcrossprod(C)
#> , , x1
#>
#> d1 d2 d3 d4
#> d1 0.66281318 0.4996001 0.5974843 0.09399897
#> d2 0.49960007 0.6947381 0.6512761 0.48728738
#> d3 0.59748426 0.6512761 0.6822835 0.40810440
#> d4 0.09399897 0.4872874 0.4081044 1.56277687
#>
#> , , x2
#>
#> d1 d2 d3 d4
#> d1 0.02048040 0.07311706 0.1260086 0.01121553
#> d2 0.07311706 0.30342381 0.5562407 0.04115575
#> d3 0.12600857 0.55624071 1.0525995 0.16670383
#> d4 0.01121553 0.04115575 0.1667038 0.90217762
#>
## convert to matrix
as.array(solve(C[1,]))[,,1]
#> d1 d2 d3 d4
#> d1 1.228300 0.000000 0.000000 0.000000
#> d2 -1.336312 1.772868 0.000000 0.000000
#> d3 -3.281398 -4.870706 7.712937 0.000000
#> d4 2.674492 1.081750 -4.048746 1.126936