Solve a redundant system of linear equations in R


April 2019


67 time


I have a noninvertible matrix A and a vector b for which I believe there is a solution x to Ax = b. I would like to find an example of such x. When I try solve(A,b) in R it produces an error because A is singular. Is there any way to make R give me a random solution?

1 answers


I eventually tried lm(b ~ 0 + A) which works. It will leave estimates for some columns as NA, which you can substitute for 0 to get an example solution. For example

A = matrix(c(1,1,0,0),nr=2,byrow=F)

b = c(2,2)

lm(b ~ A)

will produce coefficients 2 and NA for the two columns of A. solve(A,b), lsfit(A,b), and qr.solve(A,b) do not work.

Edit: MASS::ginv(A) %*% b works too.