0
$\begingroup$

I have a 5-level ordinal independent variable ('ACT1_2') and a Boolean output variable ('target'). ACT1_2 is a response to a survey question. When I was doing some exploratory data analysis, I graphed the percentage of respondents who responded True within each value of ACT1_2. I got the following graph:

EDA graph

There seems to be a pretty obvious positive association with increasing values of ACT1_2 and feeling like one is able to handle unexpected expenses.

However, when I fit a logistic regression model using this variable, I got a negative value for its coefficient. I read that this could be explained by the presence of other variables (although I did try to handle multicollinearity by making sure all my VIFs were less than 10), but the coefficient stayed negative even when I fit a logistic regression model with ACT1_2 as the only variable.

How can I reconcile these two findings?

Relevant code is reproduced below.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm
%matplotlib inline

df = pd.read_csv('https://www.consumerfinance.gov/documents/5614/NFWBS_PUF_2016_data.csv')

# Remove null values
df = df[(df['FWB1_1'] != -1) & (df['FWB1_1'] != -4) & (df['ACT1_2'] != -1)]
# Create target variable
df['target'] = df['FWB1_1'] > 3

# Frequency table for plotting values
freq_table = pd.crosstab(df['ACT1_2'], df['target'])

num_categories=5

x_vals = np.arange(num_categories) + 1
y_vals = np.empty(num_categories)
# Loop through categories
for i in np.arange(num_categories):
    # Calculate percentage of True responses within category
    y_vals[i] = freq_table.loc[i+1, True] / np.sum(freq_table.iloc[i,0:2])
_ = plt.bar(x_vals, y_vals)
_ = plt.xlabel('ACT1_2')
_ = plt.ylabel('% of True Responses')

enter image description here

X = df['ACT1_2'].values
y = df['target'].values

logit_model=sm.Logit(y,X)
result=logit_model.fit()
print(result.summary2())

Optimization terminated successfully.
         Current function value: 0.681643
         Iterations 4
                         Results: Logit
================================================================
Model:              Logit            No. Iterations:   4.0000   
Dependent Variable: y                Pseudo R-squared: -0.034   
Date:               2018-11-26 11:50 AIC:              8684.7666
No. Observations:   6369             BIC:              8691.5258
Df Model:           0                Log-Likelihood:   -4341.4  
Df Residuals:       6368             LL-Null:          -4198.7  
Converged:          1.0000           Scale:            1.0000   
------------------------------------------------------------------
         Coef.    Std.Err.      z       P>|z|     [0.025    0.975]
------------------------------------------------------------------
x1      -0.0819     0.0068   -12.0212   0.0000   -0.0953   -0.0686
================================================================
$\endgroup$
  • 3
    $\begingroup$ It's a little suspicious that your independent variables has five levels, but only one associated coefficient... Perhaps--and this is a wild guess--it's being treated as numeric, and the associated levels are ordered alphabetically, so that "Always" = 1, etc.? $\endgroup$ – The Laconic Nov 25 '18 at 20:23
  • 1
    $\begingroup$ We may be able to help you if you can post your data and/or your code. This does look strange. $\endgroup$ – Stephan Kolassa Nov 26 '18 at 14:20
  • 2
    $\begingroup$ That approach is much more likely to generate useful responses. Often, in fact, the exercise of simplifying a problem will reveal its solution. $\endgroup$ – whuber Nov 26 '18 at 20:01
  • 1
    $\begingroup$ Did you add an intercept column to X? Statsmodel doesn't add it by default $\endgroup$ – Cam.Davidson.Pilon Nov 26 '18 at 21:36
  • 1
    $\begingroup$ @Cam.Davidson.Pilon Wow...I'm pretty sure that was actually the problem. It's funny (but I'm also a bit frustrated with myself) because I had even noticed that an "intercept variable" wasn't included in the summary of my logistic regression model and looked up how to include it. The answers I found weren't very relevant to my specific situation (a result of my poor Googling skills) so I decided not to consider that possible solution more out of laziness than anything. Yet it's so obvious that assuming a 0 intercept would drastically change the coefficients...Thank you so much! $\endgroup$ – A. Bruce Nov 27 '18 at 0:41
4
$\begingroup$

The OP needed to add the intercept column, which isn't added automatically by Statsmodels. Docs here.

$\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.