Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

formula support for categorical endog variable in logistic regression

Open
#146 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
python
Domain
data

Research direction

Start by reproducing the two smf.logit formula examples in the issue, comparing the manual dummy-variable form with C(sex) ~ C(smoker) + C(time). Investigate the reported patsy limitation and determine what behavior categorical endogenous variables should have; the work is done when the categorical formula is supported with appropriate regression results and regression coverage.

Written by the indexing model from the issue text.

Description

patsy: '0.5.1'

https://github.com/statsmodels/statsmodels/issues/5552

SM: 0.9.0
For categorical endog variable in logistic regression, I still have to gerneate a dummay variable for it like the following.

import pandas as pd
import seaborn as sns
import numpy as np
import statsmodels.formula.api as smf
# generate dummy
df['male'] = df.sex.map({'Male': 1, 'Female': 0})
# regression
formula = 'male ~ C(smoker) + C(time)'
model = smf.logit(formula, data=df).fit()
model.summary()

If I just do

formula = 'C(sex) ~ C(smoker) + C(time)'
model = smf.logit(formula, data=df).fit()
model.summary()

I will get

ValueError: operands could not be broadcast together with shapes (244,2) (244,) 

This is a little bit weird, since the formula support all categorical variables but the endog. I wonder if this could be a poential feature to imporve.
Btw, is there any current workaround for this issue if I wanna use formula?

@bashtage:

This is a patsy limit. You could just define a function C1

def C1(cat):
     return pd.get_dummies(cat, drop_first=True)

and then use

formula = 'C1(sex) ~ C(smoker) + C(time)'

Dominant language
Python
Stars
989
Forks
106
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pydata/patsy

All issues in pydata/patsy

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.