1

When X is singular, the following code throws a warning. Is there a way of muting it?

"warning: solve(): system seems singular; attempting approx solution"

Function:

// [[Rcpp::depends(RcppArmadillo)]]


#include <RcppArmadillo.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector fastLM(const NumericVector & y_, const NumericMatrix&  X_) {
  
  const arma::vec & y = as<arma::vec>(wrap(y_));
  const arma::mat & X = as<arma::mat>(wrap(X_));

  int n = X.n_rows, k = X.n_cols;

  arma::colvec coef = arma::solve(X, y);

  return(
    as<NumericVector>(wrap(coef))
  );
}

Thank you

JacobJacox
  • 917
  • 5
  • 14
  • 2
    I'm against suppressing such important warnings. I wouldn't do it, but a simple nuclear option: `#define ARMA_DONT_PRINT_ERRORS`. Probably better to catch the specific warning but I don't know how to do that in C++. – Roland Sep 24 '20 at 08:46
  • 1
    Also, please remove `int n = X.n_rows, k = X.n_cols;` from the code. It causes annoying compiler warnings. – Roland Sep 24 '20 at 08:48
  • Awesome, it works. I m building trees, where in the end node you calculate predictions based on LM instead of just mean, and there are very often singular subsets of matrices... It is really annoying. Also k,n are used later, i forget to remove them when I asekd the question. Anyway, thanks! – JacobJacox Sep 24 '20 at 08:54

1 Answers1

2

It's possible to use #define ARMA_DONT_PRINT_ERRORS but that will stop printing all errors and warnings from all functions.

A more targeted approach would be to use options to the solve() function, like so:

arma::colvec coef;

bool success = arma::solve(coef, X, y, arma::solve_opts::no_approx);

// if success is true coef is valid
// if success is false, coef is invalid

If you want to keep solutions that are singular to working precision, add another option:

bool success = arma::solve(coef, X, y, arma::solve_opts::no_approx + solve_opts::allow_ugly);
hbrerkere
  • 1,561
  • 8
  • 12