How can I get the design matrix from fitlm
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Lina Christensen
el 14 de Dic. de 2018
Comentada: Veli-Matti Taavitsainen
el 7 de Mzo. de 2023
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 comentarios
Respuestas (1)
Ningshan Li
el 7 de Jun. de 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 comentario
Veli-Matti Taavitsainen
el 7 de Mzo. de 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!
Ver también
Categorías
Más información sobre Polynomials en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!