0

I want to generate a nonsingular data matrix, and avoid the following error in R:

"Error in solve.default(sigma) : 
  system is computationally singular: reciprocal condition number = 1.3971e-16"

I am using the is.singular.matrix function in R. How to code the "discard x" part in the ifelse function?

n=100
p=50
x<-0.5*matrix(rnorm(n*p),nrow=n,ncol=p)
ifelse(is.singular.matrix(x)=T, discard x, x=x)
J. Murray
  • 1,460
  • 11
  • 19
AB9
  • 1
  • what packages are you using? – Mike Oct 01 '19 at 20:33
  • 2
    Something doesn't make sense here. You are unlikely to get a singular matrix using `rnorm`. Moreover the matrix in your example is not square. – Karolis Koncevičius Oct 01 '19 at 20:35
  • I am using the "matrixcalc" package. – AB9 Oct 01 '19 at 20:59
  • `repeat{x<-0.5*matrix(rnorm(n*p),nrow=n,ncol=p); if(is.singular.matrix(x)){ break }}`. – Rui Barradas Oct 01 '19 at 21:39
  • 1
    A commonly used trick is to add a very small constant (e.g. `1e-9`) to the diagonal. However, it's hard to say if that's appropriate or the best approach for you without more context. Additionally, as @KarolisKoncevičius mentions, you're not even dealing with a square matrix in your example. Can you provide more context and a better [mcve]? – duckmayr Oct 01 '19 at 22:05

0 Answers0