Running ‘dist_matrix.R’
  Running ‘dsm_projection.R’ [3s/12s]
Running the tests in ‘tests/dsm_projection.R’ failed.
Complete output:
  > ## Validate SVD and other latent subspace projections
  > ## based on small "hieroglyphs" example matrix
  > 
  > library(wordspace)
  Loading required package: Matrix
  > library(Matrix)
  > 
  > matrix.equal <- function(x, y, name="matrix comparison", tol=1e-12, ignore.sign=FALSE, verbose=TRUE) {
  +   if (nrow(x) == nrow(y) && ncol(x) == ncol(y)) {
  +     if (ignore.sign) {
  +       ## sign of columns is arbitrary in SVD projection
  +       max.diff.col.1 <- apply(abs(x - y), 2, max) # max diff in each column (same sign)
  +       max.diff.col.2 <- apply(abs(x + y), 2, max) # max diff in each column (opposite sign)
  +       max.diff <- max(pmin(max.diff.col.1, max.diff.col.2)) # pick smaller diff for each col, then take maximum
  +     } else {
  +       max.diff <- max(abs(x - y))
  +     }
  +     if (max.diff < tol) {
  +       invisible(TRUE)
  +     } else {
  +       if (verbose) cat(sprintf("%s: largest difference between matrices = %g exceeds tolerance limit\n", name, max.diff))
  +       invisible(FALSE)
  +     }
  +   } else {
  +     if (verbose) cat(sprintf("%s: different matrix formats %d x %d != %d x %d\n", name, nrow(x), ncol(x), nrow(y), ncol(y)))
  +     invisible(FALSE)
  +   }
  + }
  > 
  > dist.compare <- function (M1, M2, label="TODO", verbose=TRUE, method="euclidean", tol=0.3) { # tol = proportion of average distance (for matrix with larger distances)
  +   stopifnot(nrow(M1) == nrow(M2))
  +   nr <- nrow(M1)
  +   M1 <- dist.matrix(M1, method=method)
  +   M2 <- dist.matrix(M2, method=method)
  +   n <- nr * (nr - 1)
  +   sum1 <- sum(M1) / n
  +   sum2 <- sum(M2) / n
  +   d <- sqrt(sum((M1 - M2)^2) / n) # root mean squared difference
  +   rel.error <- d / max(sum1, sum2)
  +   if (verbose) cat(sprintf("%s: average difference = %.1f, relative error = %4.1f%%\n", label, d, 100 * rel.error))
  +   if (rel.error > tol) stop("relative error exceeds tolerance limit")
  +   invisible(rel.error)
  + }
  > 
  > ## test data: sparse co-occurrence matrix in sparse and dense representation
  > O <- DSM_HieroglyphsMatrix
  > E <- outer(rowSums(O), colSums(O)) / sum(O)
  > M1 <- scale((O - E) / sqrt(E), center=TRUE, scale=FALSE)  # z-score (signed), centered (so SVD projection == PCA)
  > M2 <- as(M1, "dgCMatrix") # force sparse matrix representation to test sparse algorithms
  > 
  > 
  > ## full-rank SVD decomposition
  > svd.R <- svd(M1, nu=6, nv=6)
  > proj.R <- svd.R$u %*% diag(svd.R$d) # projection into SVD space
  > approx.R <- proj.R %*% t(svd.R$v)   # matrix approximation
  > 
  > proj.ws <- dsm.projection(M1, method="svd", n=6, with.basis=TRUE)
  > approx.ws <- proj.ws %*% t(attr(proj.ws, "basis"))
  > 
  > dist.compare(proj.R, M1, label="Full-rank SVD projection (R)", tol=.001)
  Full-rank SVD projection (R): average difference = 0.0, relative error =  0.0%
  > dist.compare(approx.R, M1, label="Full-rank SVD approximation (R)", tol=.001)
  Full-rank SVD approximation (R): average difference = 0.0, relative error =  0.0%
  > stopifnot(matrix.equal(approx.R, M1, tol=1e-6))
  > 
  > dist.compare(proj.ws, M1, label="Full-rank SVD projection (R)", tol=.001)
  Full-rank SVD projection (R): average difference = 0.0, relative error =  0.0%
  > dist.compare(approx.ws, M1, label="Full-rank SVD approximation (R)", tol=.001)
  Full-rank SVD approximation (R): average difference = 0.0, relative error =  0.0%
  > stopifnot(matrix.equal(approx.ws, M2, tol=1e-6))
  > 
  > 
  > ## SVD projection
  > svd.R <- svd(M1, nu=3, nv=3)
  > proj.R <- svd.R$u %*% diag(svd.R$d[1:3]) # projection into SVD space
  > proj.ws <- dsm.projection(M1, method="svd", n=3)
  > proj.ws.sparse <- dsm.projection(M2, method="svd", n=3)
  
   *** caught segfault ***
  address 6, cause 'memory not mapped'
  
  Traceback:
   1: .Call(svdLAS2_, dim(M), M@i, M@p, M@x, as.integer(rank), as.double(tol *     c(-1, 1)), as.double(kappa))
   2: sparsesvd(M, rank = n)
   3: dsm.projection(M2, method = "svd", n = 3)
  An irrecoverable exception occurred. R is aborting now ...