How it works...
We have two methods defined in the preceding code, df2mm and lm. The first one transforms DataFrame of explanatory variables into a matrix, using one-hot encoding if needed. The second performs model estimation.
The df2mm method, which prepares a model matrix, is more complex. It iteratively scans all columns of df. If a column contains real values, then it is simply added to the model matrix; if not, a one-hot encoding is applied and an mvalue submatrix is created. We use a dvalue dictionary to map levels to their numbers. Note that we drop the first column of this matrix to avoid linear dependence of columns in the resulting matrix.
In parallel, the function generates the column names of the matrix created.
The lm function ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access