-1

Can someone please help? Cannot solve this matrix using R. The code is reproducible.

(a <- matrix(c(1, 0, 1, 0, 10, 1, 0, 0, 1, 20, 0, 1, 1, 0, 5, 0, 1, 0, 1, 10), nrow = 5))
(b <- matrix(c(20, 15, 10, 25, 475), nrow = 5))
solve(a) %*% b
stat77
  • 123
  • 1
  • 8
  • 1
    Look at the error message you get: `Error in solve.default(a) : 'a' (5 x 4) must be square` – Richard Telford Sep 14 '18 at 08:13
  • But the book has solved it using the row reduction technique. So how is it possible that it can be solved using the row reduction technique but not using R? – stat77 Sep 14 '18 at 08:14
  • 5
    Possible duplicate of [Solving non-square linear system with R](https://stackoverflow.com/questions/19763698/solving-non-square-linear-system-with-r) – Nicolas2 Sep 14 '18 at 08:16
  • `1.` what is **THE BOOK**? `2.` Don't wrap your matrix defs in paretheses `3.` from `?solve` `"a - a square numeric or complex matrix containing the coefficients of the linear system. Logical matrices are coerced to numeric."` – Andre Elrico Sep 14 '18 at 08:17

1 Answers1

1

You have a non-square matrix. Since it has more rows than columns, you can use OLS:

> lm.fit(a, b)$coefficients
x1 x2 x3 x4 
 5 15  5 10 

Alternatively you can use a generalized inverse, e.g. from MASS:

> MASS::ginv(a) %*% b
     [,1]
[1,]    5
[2,]   15
[3,]    5
[4,]   10
Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75