Learn R Programming

statnet.common (version 4.10.0)

ssolve: Wrappers around matrix algebra functions that pre-scale their arguments

Description

Covariance matrices of variables with very different orders of magnitude can have very large ratios between their greatest and their least eigenvalues, causing them to appear to the algorithms to be near-singular when they are actually very much SPD. These functions first scale the matrix's rows and/or columns by its diagonal elements and then undo the scaling on the result.

Usage

ssolve(a, b, ..., snnd = TRUE)

sginv(X, ..., snnd = TRUE)

ginv_eigen(X, tol = sqrt(.Machine$double.eps), ...)

xTAx_seigen(x, A, tol = sqrt(.Machine$double.eps), ...)

srcond(x, ..., snnd = TRUE)

snearPD(x, ...)

xTAx_ssolve(x, A, ...)

xTAx_qrssolve(x, A, tol = 1e-07, ...)

sandwich_ssolve(A, B, ...)

Arguments

snnd

assume that the matrix is symmetric non-negative definite (SNND). This typically entails scaling that converts covariance to correlation and use of eigendecomposition rather than singular-value decomposition. If it's "obvious" that the matrix is not SSND (e.g., negative diagonal elements), an error is raised.

x, a, b, X, A, B, tol, ...

corresponding arguments of the wrapped functions.

Details

ginv_eigen() reimplements MASS::ginv() but using eigendecomposition rather than SVD; this means that it is only suitable for symmetric matrices, but that detection of negative eigenvalues is more robust.

ssolve(), sginv(), sginv_eigen(), and snearPD() wrap solve(), MASS::ginv(), ginv_eigen(), and Matrix::nearPD(), respectively. srcond() returns the reciprocal condition number of rcond() net of the above scaling. xTAx_ssolve(), xTAx_qrssolve(), xTAx_seigen(), and sandwich_ssolve() wrap the corresponding statnet.common functions.

Examples

Run this code
x <- rnorm(2, sd=c(1,1e12))
x <- c(x, sum(x))
A <- matrix(c(1, 0, 1,
              0, 1e24, 1e24,
              1, 1e24, 1e24), 3, 3)
stopifnot(all.equal(
  xTAx_qrssolve(x,A),
  structure(drop(x%*%sginv(A)%*%x), rank = 2L, nullity = 1L)
))

x <- rnorm(2, sd=c(1,1e12))
x <- c(x, rnorm(1, sd=1e12))
A <- matrix(c(1, 0, 1,
              0, 1e24, 1e24,
              1, 1e24, 1e24), 3, 3)

stopifnot(try(xTAx_qrssolve(x,A), silent=TRUE) ==
  "Error in xTAx_qrssolve(x, A) : x is not in the span of A\n")

Run the code above in your browser using DataLab