How can I get the design matrix from fitlm

7 vues (au cours des 30 derniers jours)
Lina Christensen
Lina Christensen le 14 Déc 2018
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!

Réponses (1)

Ningshan Li
Ningshan Li le 7 Juin 2019
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 commentaire
Veli-Matti Taavitsainen
Veli-Matti Taavitsainen le 7 Mar 2023
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!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Polynomials dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by