| linear_algebra_stats | R Documentation |
'broadcast' provides some simple Linear Algebra Functions for Statistics:
cinv()
sd_lc()
ecumprob()
cinv(x)
sd_lc(X, vc, bad_rp = NaN)
ecumprob(y, sim, eps = 0)
x |
a real symmetric positive-definite square matrix. |
X |
a numeric (or logical) matrix of multipliers/constants |
vc |
the variance-covariance matrix for the (correlated) random variables. |
bad_rp |
if |
y |
values to estimate the cumulative probability for. |
sim |
a matrix (or data.frame) with at least 500 columns of simulated values. |
eps |
a non-negative numeric scaler smaller than |
cinv()
cinv()
computes the Choleski inverse
of a real symmetric positive-definite square matrix.
sd_lc()
Given the linear combination X %*% b, where:
X is a matrix of multipliers/constants;
b is a vector of (correlated) random variables;
vc is the symmetric variance-covariance matrix for b;
sd_lc(X, vc)
computes the standard deviations for the linear combination X %*% b,
without making needless copies.
sd_lc(X, vc) will use much less memory than a base 'R' approach.
sd_lc(X, vc) will usually be faster than a base 'R' approach
(depending on the Linear Algebra Library used for base 'R').
ecumprob()
The ecumprod(y, sim) function takes a matrix (or data.frame) of simulated values sim,
and for each row i (after broadcasting),
estimates the cumulative distribution function of sim[i, ],
and returns the cumulative probability for y[i].
In terms of statistics,
it is equivalent to the following operation for each index i:
ecdf(sim[i,])(y[i])
However, ecumprob() is much faster, and supports NAs/NaNs.
In terms of linear algebra,
it is equivalent to the following broadcasted operation:
rowMeans(sim <= y)
where y and sim are broadcaster arrays.
However, ecumprob() is much more memory-efficient,
supports a data.frame for sim,
and has statistical safety checks.
For cinv():
A matrix.
For sd_lc():
A vector of standard deviations.
For ecumprob():
A vector of cumulative probabilities.
If for any observation i (after broadcasting),
y[i] is NA/NaN or any of sim[i,] is NA/NaN,
the result for i will be NA.
If zero-length y or sim is given, a zero-length numeric vector is returned.
John A. Rice (2007), Mathematical Statistics and Data Analysis (6th Edition)
chol, chol2inv
# variances ====
vc <- datasets::ability.cov$cov
X <- matrix(rnorm(100), 100, ncol(vc))
solve(vc)
cinv(vc) # faster than `solve()`, but only works on positive definite matrices
all(round(solve(vc), 6) == round(cinv(vc), 6)) # they're the same
sd_lc(X, vc)
# ecumprob() ====
sim <- rnbinom(10 * 1e4, mu = 3, size = 2) |> matrix(10, 1e4)
y <- sample(0:9)
# vector:
pnbinom(y[1], mu = 3, size = 2) # real probability
ecumprob(y[1], sim[1, , drop = TRUE]) # approximation
# matrix:
cbind(
real = pnbinom(y, mu = 3, size = 2), # real probability
approx = ecumprob(y, sim) # approximation
)
# data.frame:
cbind(
real = pnbinom(y, mu = 3, size = 2), # real probability
approx = ecumprob(y, as.data.frame(sim)) # approximation
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.