How can I get the design matrix from fitlm
7 ビュー (過去 30 日間)
古いコメントを表示
I am using fitlm to generate simple multiple regressions. The model contains both continuous and categorical predictors. In a simulation setup I will choose random parts of a big dataset and use it as the data for the model. From time to time, I will get the following error:
Warning: Regression design matrix is rank deficient to within machine precision.
The reason for the warning is, that there will from time to time not be sufficient data to cover categories or the data will only belong to one category (no variation).
Is there a way to get the design matrix from fitlm, so that I can check if it is rank deficient, and in that case just select a new random number?
Thank you!
0 件のコメント
回答 (1 件)
Ningshan Li
2019 年 6 月 7 日
When you call the fitlm function with an input form of (X, y), then you have specified the design matrix X. It gets a bit cumbersome if you use a table tbl as the function input (I usually use tables ...).
So after you have called the fitlm function as
mdl = fitlm(tbl)
you can use mdl.CoefficientNames to access the independent variables in the table, so the design matrix can be constructed by
X = tbl{:, mdl.CoefficientNames(2:end)}
where I exclude the 1st coefficient, which will always be intercept.
1 件のコメント
Veli-Matti Taavitsainen
2023 年 3 月 7 日
This answer doesn't help with categorical variables. For example, in my tbl, I have a variable named 'Katalyytti' with levels 'KatA', 'KatB' and 'KatC', so in the model there will be, for example, 'Katalyytti_KatA' which is not recognized in tbl. The same hold for interactions too. My table has the variables
Columns 1 through 6
{'Katalyytti'} {'T'} {'Vaha'} {'Neste'} {'Kaasu'} {'Char'}
Columns 7 through 9
{'Properties'} {'Row'} {'Variables'}
and mdl.CoefficientNames is
Columns 1 through 4
{'(Intercept)'} {'Katalyytti_KatA'} {'Katalyytti_KatB'} {'Katalyytti_KatC'}
Columns 5 through 8
{'T'} {'Katalyytti_KatA:T'} {'Katalyytti_KatB:T'} {'Katalyytti_KatC:T'}
Column 9
{'T^2'}
which clearly shows the discreoancy. The problem hasn't been solved yet!
参考
カテゴリ
Help Center および File Exchange で Polynomials についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!