Matrix is singular, RCOND=NAN for a OLS regression

6 ビュー (過去 30 日間)
JoV
JoV 2022 年 4 月 24 日
コメント済み: Bruno Luong 2022 年 4 月 25 日
Hello all,
I am attempting to code an OLS regression, using a OLS-function of my own design. The code looks as follows:
Y = lntrentsa;
nobs = size(Y,1);
X = [ones(nobs,1), airbnbsa, airbrnb*oorate10];
knum = size(X,2);
b = inv(X'*X) * X'*Y;
However, I am unable to get an output for b(eta) as I get the error message:
Warning: Matrix is singular, close to singular or badly scaled. Results may be inaccurate. RCOND = NaN
Would anyone know why this might be? Which Matrix does the error message mean? I have attempted rewriting b but I still only get Nan values:
b = (X'*X)\ X'*Y
My dataset is quite large (nobs = 745776), could this perhaps have an influence?
I'd be grateful for any advice! Thank you

回答 (1 件)

Bruno Luong
Bruno Luong 2022 年 4 月 24 日
編集済み: Bruno Luong 2022 年 4 月 24 日
First make sure your data X and Y contain only finite elements (no NaN, Inf, or such).
Instead of
b = inv(X'*X) * X'*Y;
try (R2018a or later required)
[XN,C,S] = normalize(X);
b = ((XN+C./S)\Y) ./ S.';
  3 件のコメント
Bruno Luong
Bruno Luong 2022 年 4 月 24 日
編集済み: Bruno Luong 2022 年 4 月 24 日
So may be your matrix is really singular (rank(X) < 3), in this case you migh wanr to do
b = pinv(X'*X) * X'*Y;
or
[XN,C,S] = normalize(X);
XN = (XN+C./S);
b = (pinv(XN'*XN) * XN'*Y) ./ S.';
But MATLAB warning is still valid: the result may be innacurate, simply your data are not allowed to observe whatever the parameters b you want to compute.
Bruno Luong
Bruno Luong 2022 年 4 月 25 日
You might also want to experiment with different method of normalizing, e.g.,
[XN,C,S] = normalize(X,'medianiqr')

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLinear Least Squares についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by