markmfredrickson / optmatch

Functions for optimal matching in R
https://markmfredrickson.github.io/optmatch
Other
47 stars 14 forks source link

MASS error in R-devel #151

Closed josherrickson closed 6 years ago

josherrickson commented 6 years ago

I noticed that there's a new warning/error popping up in Travis for R-devel:

* checking for unstated dependencies in ‘tests’ ... WARNING
'::' or ':::' import not declared from: ‘MASS’
'library' or 'require' call not declared from: ‘MASS’

This does not show up when submitting to the win-builder. However, we should still investigate when we get a chance.

>>> git grep MASS                                                                                              
tests/testthat/test.compute_mahal.R:    x <- MASS::mvrnorm(k, mu = c(-1, 0, 1),
tests/testthat/test.rank.mahal.R:if (requireNamespace("MASS")) {
tests/testthat/test.rank.mahal.R:        icov <- MASS::ginv(cv)
tests/testthat/test.rank.mahal.R:  if (requireNamespace("MASS")) {
tests/testthat/test.rank.mahal.R:      if (requireNamespace("MASS")) {

Since this is only in tests, hopefully we can work around the use of MASS (e.g. quickly looking at test.compute_mahal.R, perhaps we can just generate the data serially with arbitrary non-zero correlations).

josherrickson commented 6 years ago

Assigning to @benthestatistician since it looks like those test files were last touched by him. Perhaps replace the MASS::ginv with solve? Less general but might be fine on our test cases.

benthestatistician commented 6 years ago

Does anyone see a problem w/ simply copying these function definitions into our test files? The MASS package license is listed at "GPL-2 | GPL-3". I'd might add some text indicating the source.

> MASS::mvrnorm
function (n = 1, mu, Sigma, tol = 1e-06, empirical = FALSE, EISPACK = FALSE) 
{
    p <- length(mu)
    if (!all(dim(Sigma) == c(p, p))) 
        stop("incompatible arguments")
    if (EISPACK) 
        stop("'EISPACK' is no longer supported by R", domain = NA)
    eS <- eigen(Sigma, symmetric = TRUE)
    ev <- eS$values
    if (!all(ev >= -tol * abs(ev[1L]))) 
        stop("'Sigma' is not positive definite")
    X <- matrix(rnorm(p * n), n)
    if (empirical) {
        X <- scale(X, TRUE, FALSE)
        X <- X %*% svd(X, nu = 0)$v
        X <- scale(X, FALSE, TRUE)
    }
    X <- drop(mu) + eS$vectors %*% diag(sqrt(pmax(ev, 0)), p) %*% 
        t(X)
    nm <- names(mu)
    if (is.null(nm) && !is.null(dn <- dimnames(Sigma))) 
        nm <- dn[[1L]]
    dimnames(X) <- list(nm, NULL)
    if (n == 1) 
        drop(X)
    else t(X)
}
> MASS::ginv
function (X, tol = sqrt(.Machine$double.eps)) 
{
    if (length(dim(X)) > 2L || !(is.numeric(X) || is.complex(X))) 
        stop("'X' must be a numeric or complex matrix")
    if (!is.matrix(X)) 
        X <- as.matrix(X)
    Xsvd <- svd(X)
    if (is.complex(X)) 
        Xsvd$u <- Conj(Xsvd$u)
    Positive <- Xsvd$d > max(tol * Xsvd$d[1L], 0)
    if (all(Positive)) 
        Xsvd$v %*% (1/Xsvd$d * t(Xsvd$u))
    else if (!any(Positive)) 
        array(0, dim(X)[2L:1L])
    else Xsvd$v[, Positive, drop = FALSE] %*% ((1/Xsvd$d[Positive]) * 
        t(Xsvd$u[, Positive, drop = FALSE]))
}
josherrickson commented 6 years ago

It's not the most elegant solution, but as long as none of the objects inside those functions also rely on MASS, it should be fine.

josherrickson commented 6 years ago

Also, just noticed this error is appearing in the CRAN check: https://cran.r-project.org/web/checks/check_results_optmatch.html. It's interesting that this is appearing here, but not on the winbuilder server (https://win-builder.r-project.org). I had thought they were equivalent.

benthestatistician commented 6 years ago

I just pushed up [master 0834339], which ought to take care of the problem. Couldn't check b/c my local testing setup is wonky. But I imagine that if I check back early next week Travis will have tested it for me. Pls let me know if I'm misunderstanding how that works.

josherrickson commented 6 years ago

I can confirm this fixed the issue with Travis. Closing.