RfastOfficial / Rfast

A collection of Rfast functions for data analysis. Note 1: The vast majority of the functions accept matrices only, not data.frames. Note 2: Do not have matrices or vectors with have missing data (i.e NAs). We do no check about them and C++ internally transforms them into zeros (0), so you may get wrong results. Note 3: In general, make sure you give the correct input, in order to get the correct output. We do no checks and this is one of the many reasons we are fast.
142 stars 19 forks source link

error from calling crossprod() from cova() #116

Open patrickjdanaher opened 1 week ago

patrickjdanaher commented 1 week ago

Describe the bug When I call cova(), I get the error, "Error in crossprod(x) : "crossprod" is not a BUILTIN function"

To Reproduce The following suffices to produce the bug:

Rfast::cova(matrix(rnorm(1000), 100))

Desktop (please complete the following information): R version 4.3.3 (2024-02-29 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19045) Rfast_2.1.0

Additional context Rewriting the function as below, calling base::crossprod instead of crossprod alone, fixed it:

function(x, center = FALSE, large = FALSE) 
{
  n <- dim(x)[1]
  if (!center) {
    m <- sqrt(n) * Rfast::colmeans(x)
    if (large) {
      s <- (Rfast::Crossprod(x, x) - tcrossprod(m))/(n - 
                                                       1)
    }
    else s <- (base::crossprod(x) - tcrossprod(m))/(n - 1)
  }
  else {
    m <- Rfast::colmeans(x)
    x <- eachrow(x, m, oper = "-")
    if (large) {
      s <- Rfast::Crossprod(x, x)/(n - 1)
    }
    else s <- base::crossprod(x)/(n - 1)
  }
  s
}
ManosPapadakis95 commented 1 week ago

This is not a bug. It must have to do with R because the crossprod it is from the base package. We don't have to call it using the package name because it is the standard.