pydata / pydata/patsy

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.

Dominant language
Python
Stars
990
Forks
106
Avg merge
7d 34m
Merged PRs (30d)
1

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)'

Contributor guide

No contributing guide indexed for this repository

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.

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.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.