Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decrease machine precision .Machine$double.eps in R?

Tags:

r

epsilon

When using an R package called did, I get this error:

Error in solve.default(preV) :    system is computationally singular:
reciprocal condition number = 4.09946e-19

The solve function has an argument called tol whereby I can set the tolerance and thereby avoid the problem. But the package does not give this option to specify this tol from its top-level functions, so there is no way to set tol for solve.

I am thinking about an alternative. Since solve() defaults using .Machine$double.eps for tol, it would be good if I could decrease this default value. How?

like image 359
William Parker Avatar asked Nov 19 '25 20:11

William Parker


1 Answers

That is something you can't decrease as it is sort of a C-standard. However, you can manually set tol = 1e-20 as a function input for example, to override the default tolerance. But, this is purely a numerical workaround. You need to think about whether it is possible to address your rank-deficiency issue at your problem level. For example, rank-deficiency can arise due to the poor scaling between your data variables, and here is a simple example: Linear model singular because of large integer datetime in R?.


I not sure as to what to do here then? As I said I'm using a function called mp.spatt where the solve function is used. So I can't really (or at least I don't know how?) add the tol = blabla argument in the solve function?

You could write a patched version of that function and rebuild the package for your own use. This is exactly why open-source software and packages are so GREAT.

Go to https://cran.r-project.org/package=did, download the source file .tar.gz for Linux, .tgz for Mac or .zip for Windows. Extract it, open the "did.R" file in the /R director / folder. Function solve is only used once in this file, so you can easily locate it:

W <- n*t(preatt)%*%solve(preV)%*%preatt

You can add tol = 0 for simplicity. Or, you many replace solve by MASS::ginv. Actually, the package authors have been using MASS::ginv everywhere else in the script. It could be the case that he just forgot to replace this solve as well (I am checking the latest version 1.1.0 on 2018/07/11).

After fixing it, generate a .tar.gz, .tgz or .zip file again. Open your R, use install.packages to install this specific file, and happily use it.

like image 195
Zheyuan Li Avatar answered Nov 22 '25 11:11

Zheyuan Li



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!