title: Getting "PerfectSeparationError:" when using the statsmodels.GLM( ) class for Logistic Regression
body: <p>I am trying to fit a Logistic Regression model using the <code>GLM</code> class from <code>statsmodels</code> library. I have a toy dataset with 1250 records in total and 8 Independent variable.</p>
  <pre><code>Predictor: ['Year', 'Lag1', 'Lag2', 'Lag3', 'Lag4', 'Lag5', 'Volume', 'Today']
  train dataset shape: (1000, 9)    # 9 predictors including Intercept
  Test dataset shape : (250, 9)
  
  &lt;class 'pandas.core.frame.DataFrame'&gt;
  RangeIndex: 1250 entries, 0 to 1249
  Data columns (total 9 columns):
   #   Column     Non-Null Count  Dtype  
  ---  ------     --------------  -----  
   0   Year       1250 non-null   int64  
   1   Lag1       1250 non-null   float64
   2   Lag2       1250 non-null   float64
   3   Lag3       1250 non-null   float64
   4   Lag4       1250 non-null   float64
   5   Lag5       1250 non-null   float64
   6   Volume     1250 non-null   float64
   7   Today      1250 non-null   float64
   8   Direction  1250 non-null   object 
  dtypes: float64(7), int64(1), object(1)
  memory usage: 88.0+ KB
  
  </code></pre>
  <p>Here is my code:</p>
  <pre><code>import statsmodels.api as sm
  from sklearn.preprocessing import OrdinalEncode
  
  X4 = df.drop(labels = ['Direction'], axis = 1)
  y4 = df['Direction'].copy()
  
  X4_train, X4_test, y4_train, y4_test = train_test_split(X4, y4, stratify = y4, train_size = 0.8)
  
  X4_train = sm.add_constant(X4_train, prepend = True)  # adding an intercept manually to training data
  X4_test = sm.add_constant(X4_test, prepend = True)    # adding an intercept manually to test data
  
  OE4 = OrdinalEncoder()
  
  y4_train = OE4.fit_transform(np.asarray(y4_train).reshape(-1,1)).ravel()
  y4_test = OE4.transform(np.asarray(y4_test).reshape(-1,1)).ravel()
  
  glm4 = sm.GLM(endog = y4_train, exog = X4_train, family = sm.families.Binomial())
  glm4_result = glm4.fit()
  glm4_result.summary()
  
  </code></pre>
  <p>After splitting into training and test, when I fit the model I'm getting the below error along with a <strong>RuntimeWarning: invalid value encountered in true_divide</strong></p>
  <p>Here is the entire error:</p>
  <pre><code>C:\ProgramData\Anaconda3\lib\site-packages\statsmodels\genmod\families\family.py:894: RuntimeWarning: invalid value encountered in true_divide
    n_endog_mu = self._clean((1. - endog) / (1. - mu))
  C:\ProgramData\Anaconda3\lib\site-packages\statsmodels\genmod\families\links.py:188: RuntimeWarning: overflow encountered in exp
    t = np.exp(-z)
  C:\ProgramData\Anaconda3\lib\site-packages\statsmodels\genmod\families\family.py:893: RuntimeWarning: invalid value encountered in true_divide
    endog_mu = self._clean(endog / mu)
  ---------------------------------------------------------------------------
  PerfectSeparationError                    Traceback (most recent call last)
  &lt;ipython-input-166-66b45a65beec&gt; in &lt;module&gt;
        2 
        3 glm4 = sm.GLM(endog = y4_train, exog = X4_train[['Lag1', 'Lag2', 'Lag3', 'Lag4', 'Lag5', 'Volume', 'Today']], family = sm.families.Binomial())
  ----&gt; 4 glm4_result = glm4.fit()
        5 glm4_result.summary()
  
  C:\ProgramData\Anaconda3\lib\site-packages\statsmodels\genmod\generalized_linear_model.py in fit(self, start_params, maxiter, method, tol, scale, cov_type, cov_kwds, use_t, full_output, disp, max_start_irls, **kwargs)
     1025             return self._fit_irls(start_params=start_params, maxiter=maxiter,
     1026                                   tol=tol, scale=scale, cov_type=cov_type,
  -&gt; 1027                                   cov_kwds=cov_kwds, use_t=use_t, **kwargs)
     1028         else:
     1029             self._optim_hessian = kwargs.get('optim_hessian')
  
  C:\ProgramData\Anaconda3\lib\site-packages\statsmodels\genmod\generalized_linear_model.py in _fit_irls(self, start_params, maxiter, tol, scale, cov_type, cov_kwds, use_t, **kwargs)
     1172             if endog.squeeze().ndim == 1 and np.allclose(mu - endog, 0):
     1173                 msg = &quot;Perfect separation detected, results not available&quot;
  -&gt; 1174                 raise PerfectSeparationError(msg)
     1175             converged = _check_convergence(criterion, iteration + 1, atol,
     1176                                            rtol)
  
  PerfectSeparationError: Perfect separation detected, results not available
  
  </code></pre>
  <p>However, I can see that if I only keep <strong>6 predictors</strong> or less then fit() method works and I can then see the summary( ), but If I exceed more than six then I will keep getting this &quot;PerfectSeperationError&quot;!</p>
  <p>So, does this class statsmodels.GLM() only accepts just 6 predictors, In my view it must not be the case otherwise what's the use of this class!! I'm probably sure of making some mistake in my code, which I am not able to find out, hence could someone please help me do the required correction?<a href="https://i.stack.imgur.com/KOwlI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KOwlI.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/BcxTd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BcxTd.png" alt="enter image description here" /></a></p>