[R] impossible to invert a spam-object, but possible when it's a matrix-object
bhh at xs4all.nl
Mon Feb 11 16:01:33 CET 2013
On 11-02-2013, at 15:00, "Camarda, Carlo Giovanni" <Camarda at demogr.mpg.de> wrote:
> Dear Uwe,
> thanks for the response.
> I knew my matrix was almost singular, but is there any way to implement the algorithm is solve(base) with a spam-matrix?
> As workaround, I might add a ridge penalty:
> ApP <- A+10^-1*diag.spam(nrow(A))
> but this would completely jeopardize other parts of my model.
I've inspected your matrix with package Matrix.
It doesn't seem catastrophically ill-conditioned.
It's not positive definite.
It seems that spam is issuing a misleading error message.
ent <- c(2312.12324929972,-2000,1000,-2000,1000,-2000,
rr <- as.integer(c(1,6,12,18,24,29,35,41,47,52,55,57,59))
cc <- as.integer(c(1,2,3,4,7,1,2,3,5,8,10,1,2,3,6,9,11,1,
di <- as.integer(c(12,12))
A <- sparseMatrix(x=ent,p=rr-1,j=cc)
# not pd
# chol(A) # ==> not positive definite
chol(A+diag(1e-1,nrow(A))) # this works but isn't what you want
A.m <- as.matrix(A)
chol(A.m,pivot=TRUE) # without pivot: not positive definite
# has rank 9 and warning message
B <- as.matrix(A)
all.equal(as.matrix(A %*% solve(B)), diag(12), check.attributes=FALSE)
all.equal(as.matrix(B %*% solve(A)), diag(12), check.attributes=FALSE)
More information about the R-help