1
$\begingroup$

I want to understand what's going on with a categorical variable reference group generated using dmatrices(), when building logistic regression models with sm.Logit().

In my toy model I'm predicting the type of transmission (am) from fuel consumption (mpg) and the engine type (vs) using the mtcars data set. am and vs are categorical variables (0 or 1), and mpg is a continuous variable.

When using dmatrices() and not removing the intercept from dmatrices(), I get the following output for the model (model1):

enter image description here

When using dmatrices() and removing the intercept from dmatrices(), I get the following output for the model (model2):

enter image description here

The problem is that I don't understand why C(vs)[0] is in model2. I thought it was the reference group and therefore should have been dropped like in model1?

Thanks for any help!

# libraries
import pandas as pd 
from patsy import dmatrices
import statsmodels.api as sm

# dataset 
mtcars = sm.datasets.get_rdataset("mtcars", "datasets", cache=True).data
df = pd.DataFrame(mtcars)

# model 1 (with intercept)
Y1, x1 = dmatrices('am ~ mpg + C(vs)', df, return_type = 'dataframe') 
mod1 = sm.Logit(Y1, x1).fit()
mod1.summary()

# model 2 (without intercept)
Y2, x2 = dmatrices('am ~ 0 + mpg + C(vs)', df, return_type = 'dataframe') 
mod2 = sm.Logit(Y2, x2).fit()
mod2.summary()
$\endgroup$
0
$\begingroup$

When you put "0 + " in the formula, the explicit intercept (column of 1's) is omitted, but in its place you have indicator variables for all levels of the factor variable (vs), i.e. there is no reference level. The premise is that you always want an intercept, the only issue is how you want the intercept coded.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.