|
矩阵X'X的逆
在统计计算中,我们常常需要计算这样矩阵的逆,如OLS估计中求系数矩阵。R中的包
“strucchange”提供了有效的计算方法。
> args(solveCrossprod)
function (X, method = c("qr", "chol", "solve"))
其中:method指定求逆方法,选用“qr”效率最高,选用“chol”精度最高,选用
“slove”与slove(crossprod(x,x))效果相同,例如:
> A=matrix(rnorm(16),4,4)
> solveCrossprod(A,method="qr")
[,1] [,2] [,3] [,4]
[1,] 0.6132102 -0.1543924 -0.2900796 0.2054730
[2,] -0.1543924 0.4779277 0.1859490 -0.2097302
[3,] -0.2900796 0.1859490 0.6931232 -0.3162961
[4,] 0.2054730 -0.2097302 -0.3162961 0.3447627
> solveCrossprod(A,method="chol")
[,1] [,2] [,3] [,4]
[1,] 0.6132102 -0.1543924 -0.2900796 0.2054730
[2,] -0.1543924 0.4779277 0.1859490 -0.2097302
[3,] -0.2900796 0.1859490 0.6931232 -0.3162961
[4,] 0.2054730 -0.2097302 -0.3162961 0.3447627
> solveCrossprod(A,method="solve")
[,1] [,2] [,3] [,4]
[1,] 0.6132102 -0.1543924 -0.2900796 0.2054730
[2,] -0.1543924 0.4779277 0.1859490 -0.2097302
[3,] -0.2900796 0.1859490 0.6931232 -0.3162961
[4,] 0.2054730 -0.2097302 -0.3162961 0.3447627
> solve(crossprod(A,A))
[,1] [,2] [,3] [,4]
[1,] 0.6132102 -0.1543924 -0.2900796 0.2054730
[2,] -0.1543924 0.4779277 0.1859490 -0.2097302
[3,] -0.2900796 0.1859490 0.6931232 -0.3162961
[4,] 0.2054730 -0.2097302 -0.3162961 0.3447627 |
|