Comments (3)
Hi Alex! Thanks for reaching out again.
This is also not a bug, and is documented abstractly here: https://matthewwardrop.github.io/formulaic/guides/grammar/#behaviours-and-conventions
The patsy documentation related to this is here: https://patsy.readthedocs.io/en/latest/formulas.html#redundancy-and-categorical-factors.
In short, though, we want to ensure that the matrices generated are (at least structurally) full rank. Since categorical encodings span the intercept, including the full Cartesian product of values would span the intercept column, leading to non-full-rank model matrices. Instead, we give you full set of features that span the requested Cartesian product.
If you omit the intecept, you will see that the Cartesian product is more like you expect.
You can also disable this behaviour by passing ensure_full_rank=False
to the model_matrix
or get_model_matrix
functions, for example:
At the cost of guaranteeing structurally that the model matrix will not be full rank. In (e.g.) linear regression, this will cause X.T @ X
to not be invertible and introduce a gauge degree of freedom in your model.
R is inconsistent with this kind of behaviour. It tries to reduce the rank of categorical variables on their own, but gives up for higher order interactions. We just do it properly.
from formulaic.
Ah, ok, this makes perfect sense! Thank you for your swift response =) My incorrect assumption was that formulaic
mimics R's model.matrix()
, but obviously it is based on patsy
. I will have to think through if I want to set ensure_full_rank = False
as a default for pyfixest
(for perfect compatibility with fixest
/ R), or if I should keep with the formulaic
defaults. Both fixest
and pyfixest
include a function to detect and drop multicollinear variables, so I (can) in principle ensure that the design matrix is of full rank even after calling formulaic.get_model_matrix(..., ensure_full_rank = False)
. I should also spend some time to read through the patsy
docs!
As a side point, I've worked quite a bit with formulaic
in the past week, and I found myself several times thinking that working with formulaic
is really quite a pleasant experience! Starting from a already high base, using some of the "more advanced" features of the package has clearly increased my appreciation of your work even further! =)
from formulaic.
Thanks for the compliment!
It's worth noting, though, that even when you set ensure_full_rank=False
it won't match R exactly in all cases... Since R does sometimes try to reduce rank of categorical encodings to maintain full rank... It just isn't as consistent in doing so. There is no option in Formulaic that perfectly matches R's inconsistent behaviour.
from formulaic.
Related Issues (20)
- How to include structural zeros? HOT 1
- Retain Column Names for sparse model matrices HOT 4
- Formulaic not raising an exception when required fields are missing in the dataset HOT 2
- Allow formatting the categorical encoded variables HOT 4
- Throw error when formula has parameters that are not available HOT 2
- Support polars HOT 4
- Dropping Indices via "+0" or "-1" and reference levels for categoricals HOT 1
- Extending `formulaic` to work with other input types HOT 2
- Handling individual columns that can expand into multiple columns HOT 7
- Support the hashing trick as an encoding strategy for categorical features HOT 6
- `model_spec.transform_state` bugged when formula is not correctly written HOT 1
- Is there a way to get the baseline value for categorical variables? HOT 7
- Add . operator HOT 1
- Suggestions for creating `get_feature_names_out` for Scikit Learn ColumnTransformer compatibility? HOT 3
- Is it possible to define custom operators? HOT 2
- Is it possible to force the `Formula` class to not expand categorical variables? HOT 3
- Add required variables to the `Formula` class HOT 6
- Potential Bug / different defaults for Intercept / Reference Levels when using `Formula.get_model_matrix()` with categoricals HOT 2
- Incompatibility with pandas development version HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from formulaic.