Skip to content

Commit

Permalink
lweighted.var() if given a matrix with one row now returns a matrix o…
Browse files Browse the repository at this point in the history
…f the correct dimension; a new argument (onerow) for lweighted.var() and lweighted.cov() now specifies whether the resulting matrix is NA, 0, or some other value.
  • Loading branch information
krivit committed Jan 27, 2024
1 parent 99cea95 commit 899b09a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ BugReports: https://github.com/statnet/statnet.common/issues
License: GPL-3 + file LICENSE
URL: https://statnet.org
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Encoding: UTF-8
Suggests: covr,
rlang (>= 1.1.1),
Expand Down
20 changes: 14 additions & 6 deletions R/logspace.utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
#' \code{FALSE}, the package's own implementation based on it is used, using
#' \code{double} precision, which is (on most systems) several times faster, at
#' the cost of precision.
#'
#' @param onerow If given a matrix or matrices with only one row
#' (i.e., sample size 1), [var()] and [cov()] will return `NA`. But,
#' since weighted matrices are often a product of compression, the
#' same could be interpreted as a variance of variables that do not
#' vary, i.e., 0. This argument controls what value should be
#' returned.
#'
#' @return The functions return the equivalents of the R expressions given below,
#' but faster and with less loss of precision.
#' @author Pavel N. Krivitsky
Expand Down Expand Up @@ -92,21 +100,21 @@ lweighted.mean <- function(x, logw){

#' @describeIn logspace.utils weighted variance of `x`: `crossprod(x-lweighted.mean(x,logw)*exp(logw/2))/sum(exp(logw))`
#' @export
lweighted.var <- function(x, logw){
lweighted.var <- function(x, logw, onerow = NA){
E <- lweighted.mean(x, logw)
if(is.null(dim(x))){
if(length(x)<2) return(NA)
if(length(x)<2) return(onerow)
x <- x - E
lweighted.mean(x*x, logw)
}else{
if(nrow(x)<2) return(matrix(NA, 1, ncol(x)))
if(nrow(x)<2) return(matrix(onerow, ncol(x), ncol(x)))
.Call("logspace_wmean2_wrapper", sweep_cols.matrix(x, E), logw, PACKAGE="statnet.common")
}
}

#' @describeIn logspace.utils weighted covariance between `x` and `y`: `crossprod(x-lweighted.mean(x,logw)*exp(logw/2), y-lweighted.mean(y,logw)*exp(logw/2))/sum(exp(logw))`
#' @export
lweighted.cov <- function(x, y, logw){
lweighted.cov <- function(x, y, logw, onerow = NA){
xdim <- dim(x)
E <- lweighted.mean(x, logw)
x <- if(is.null(xdim)) x - E else sweep_cols.matrix(x, E)
Expand All @@ -116,13 +124,13 @@ lweighted.cov <- function(x, y, logw){
y <- if(is.null(ydim)) y - E else sweep_cols.matrix(y, E)

if(is.null(xdim) || is.null(ydim)){
if(length(x)<2) return(NA)
if(length(x)<2) return(onerow)
o <- lweighted.mean(x*y, logw)
if(!is.null(xdim)) cbind(o)
else if(!is.null(xdim)) rbind(o)
else o
}else{
if(nrow(x)<2) matrix(NA, ncol(x), ncol(y))
if(nrow(x)<2) matrix(onerow, ncol(x), ncol(y))
else .Call("logspace_wxmean_wrapper", x, y, logw, PACKAGE="statnet.common")
}
}
Expand Down
11 changes: 9 additions & 2 deletions man/logspace.utils.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 899b09a

Please sign in to comment.