You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
formula support for categorical endog variable in logistic regression #5552
This is a little bit weird, since the formula support all categorical variables but the endog. I wonder if this could be a potential feature to improve.
Btw, is there any current workaround for this issue if I wanna use formula?
If the y array is not numerical, then patsy always transforms to two col categorical
however, because patsy just evals arbitrary code, the following works in the example
ex = np.array("a a b b a b".split())
endo, exo = patsy.dmatrices("I((ex=='a').astype(int)) ~ 1", {"ex": ex})
np.asarray(endo)
array([[1.],
[1.],
[0.],
[0.],
[1.],
[0.]])
I() prevents patsy from trying to parst what is in the (). For example, y ~ 1 + x1 + x2 + x3 vs y ~ 1 + x1 + I(x2 + x3). You should be able to replace the I above with a function
@bashtage It seems like I had some trouble to understand what would be the input of the function we define. Now it makes sense.
For the first approach using a user-defined function, the issue that we need to hard-code the selected category and neither the formula nor our output Dep. Variable will include the select group.
Activity
bashtage commentedon May 7, 2019
This is a patsy limit. You could just define a function C1
and then use
formula = 'C1(sex) ~ C(smoker) + C(time)'bashtage commentedon May 7, 2019
This might be a request for Patsy to provide an option in
Cto drop one category always.stevenlis commentedon May 16, 2019
Thanks. just curious if the results will be dependent on panda's coding.
stevenlis commentedon Jan 29, 2020
I wonder if there is anyway I can define a function that accept two args then use it in the formula?
like
This way I can select the group I'm interested.
formula = 'Code(sex, target='Male') ~ C(smoker) + C(time)'Or use this:
https://patsy.readthedocs.io/en/latest/API-reference.html#handling-categorical-data ?
bashtage commentedon Jan 29, 2020
The formula will have access to any functions in the calling namespace
stevenlis commentedon Jan 29, 2020
for one arg is fine but what if I have two args?
josef-pkt commentedon Jan 29, 2020
If the y array is not numerical, then patsy always transforms to two col categorical
however, because patsy just evals arbitrary code, the following works in the example
stevenlis commentedon Jan 29, 2020
Sorry @josef-pkt I'm not quite following. How should I write this in a function form so that I can use it in formula of SM?
josef-pkt commentedon Jan 29, 2020
you can use "I((ex=='a').astype(int))" directly in the formula, then you can edit the reference level in the formula, i.e. replace 'a'
e.g. the following should work
formula = 'I((sex=='Male').astype(int)) ~ C(smoker) + C(time)'
josef-pkt commentedon Jan 29, 2020
to confirm that it also works with pandas (using column name in
I())same result with
stevenlis commentedon Jan 29, 2020
@josef-pkt Thanks buddy. That seems really work, but I have no idea why i worked. The doc basically says it's an identify function and nothing more
https://patsy.readthedocs.io/en/latest/builtins-reference.html#patsy.builtins.I
Could you please explain it a little bit?
bashtage commentedon Jan 30, 2020
I()prevents patsy from trying to parst what is in the(). For example,y ~ 1 + x1 + x2 + x3vsy ~ 1 + x1 + I(x2 + x3). You should be able to replace theIabove with a functionstevenlis commentedon Jan 30, 2020
@bashtage It seems like I had some trouble to understand what would be the input of the function we define. Now it makes sense.
For the first approach using a user-defined function, the issue that we need to hard-code the selected category and neither the formula nor our output
Dep. Variablewill include the select group.If we use
Ifunction, we can select the interested group on fly and it will also be displayed in our results output.stevenlis commentedon Jan 30, 2020
@bashtage Could you consider adding this to the doc?
https://www.statsmodels.org/stable/generated/statsmodels.discrete.discrete_model.Logit.from_formula.html#statsmodels.discrete.discrete_model.Logit.from_formula
bashtage commentedon Jan 30, 2020
PRs with new or modifications of existing notebooks are always welcome!