# [R] solve() doesn`t work

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed Jun 19 17:26:44 CEST 2002

```Frithjof Weinert <mail at weinert.biz> writes:

> Hi,
>
> I tried to inverse a matrix but it doesn`t work. I hope somebody can help
> me. This is what I did.
>
> > x <- cbind(1,kurse[,-c(1)])
> > y <- kurse\$index
> > t(x) %*% x
> Error in t(x) %*% x : requires numeric matrix/vector arguments
> > x <- as.matrix(x)
> > xtxi <- solve(t(x) %*% x)
> Error in solve.default(t(x) %*% x) : singular matrix `a' in solve

It means that your matrix is near-singular.

> eigen(t(x)%*%x)
\$values
[1] 6.718414e+06 4.166548e+03 3.170856e+02 3.815431e+01 3.324083e+01
[6] 1.370728e+01 1.365406e+00 1.212465e-03

\$vectors
...

Notice the factor of 5e9 between the largest and the smallest
eigenvalue. In fact, you can work around it like this (but the
precision will suffer):

> solve(t(x)%*%x,tol=1e-10)
[,1]         [,2]          [,3]          [,4]          [,5]
824.4508915  3.314864820 -0.4763403196 -4.0699637637  0.1052127229
bmw   3.3148648  0.082230914 -0.0025540139 -0.0106190685 -0.0080733381
mru  -0.4763403 -0.002554014  0.0006542950 -0.0002472137  0.0008589666
rwe  -4.0699638 -0.010619068 -0.0002472137  0.1052825026  0.0162370133
vow   0.1052127 -0.008073338  0.0008589666  0.0162370133  0.0157643276
kar   0.6565594  0.017020959  0.0002213754  0.0144204919  0.0063406071
sie   2.1469705  0.012543437 -0.0013758730 -0.0114628060  0.0019913048
bas -15.0682942 -0.166379673  0.0085819595 -0.0932488883 -0.0619568748
[,6]         [,7]         [,8]
0.6565593592  2.146970504 -15.06829414
bmw  0.0170209590  0.012543437  -0.16637967
mru  0.0002213754 -0.001375873   0.00858196
rwe  0.0144204919 -0.011462806  -0.09324889
vow  0.0063406071  0.001991305  -0.06195687
kar  0.0264386956 -0.008382337  -0.09138646
sie -0.0083823370  0.019680899  -0.06057200
bas -0.0913864614 -0.060571996   0.93040568

Notice that the problem is partly self-inflicted. You seem to be
trying to fit a linear model and lm(), which is designed for the
purpose, has no trouble with these data:

> summary(lm(index~.,data=dd))\$cov.unscaled
(Intercept)          bmw           mru           rwe           vow
(Intercept) 824.4508901  3.314864816 -0.4763403187 -4.0699637528  0.1052127251
bmw           3.3148648  0.082230914 -0.0025540139 -0.0106190685 -0.0080733381
mru          -0.4763403 -0.002554014  0.0006542950 -0.0002472137  0.0008589666
rwe          -4.0699638 -0.010619068 -0.0002472137  0.1052825026  0.0162370133
vow           0.1052127 -0.008073338  0.0008589666  0.0162370133  0.0157643276
kar           0.6565594  0.017020959  0.0002213754  0.0144204919  0.0063406071
sie           2.1469705  0.012543437 -0.0013758730 -0.0114628060  0.0019913049
bas         -15.0682941 -0.166379673  0.0085819595 -0.0932488885 -0.0619568748
kar          sie          bas
(Intercept)  0.6565593623  2.146970499 -15.06829413
bmw          0.0170209590  0.012543437  -0.16637967
mru          0.0002213754 -0.001375873   0.00858196
rwe          0.0144204919 -0.011462806  -0.09324889
vow          0.0063406071  0.001991305  -0.06195687
kar          0.0264386956 -0.008382337  -0.09138646
sie         -0.0083823370  0.019680899  -0.06057200
bas         -0.0913864614 -0.060571996   0.93040568

--
O__  ---- Peter Dalgaard             Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics     2200 Cph. N
(*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```