code: def _autolag(
      mod,
      endog,
      exog,
      startlag,
      maxlag,
      method,
      modargs=(),
      fitargs=(),
      regresults=False,
  ):
      """
      Returns the results for the lag length that maximizes the info criterion.
  
      Parameters
      ----------
      mod : Model class
          Model estimator class
      endog : array_like
          nobs array containing endogenous variable
      exog : array_like
          nobs by (startlag + maxlag) array containing lags and possibly other
          variables
      startlag : int
          The first zero-indexed column to hold a lag.  See Notes.
      maxlag : int
          The highest lag order for lag length selection.
      method : {"aic", "bic", "t-stat"}
          aic - Akaike Information Criterion
          bic - Bayes Information Criterion
          t-stat - Based on last lag
      modargs : tuple, optional
          args to pass to model.  See notes.
      fitargs : tuple, optional
          args to pass to fit.  See notes.
      regresults : bool, optional
          Flag indicating to return optional return results
  
      Returns
      -------
      icbest : float
          Best information criteria.
      bestlag : int
          The lag length that maximizes the information criterion.
      results : dict, optional
          Dictionary containing all estimation results
  
      Notes
      -----
      Does estimation like mod(endog, exog[:,:i], *modargs).fit(*fitargs)
      where i goes from lagstart to lagstart+maxlag+1.  Therefore, lags are
      assumed to be in contiguous columns from low to high lag length with
      the highest lag in the last column.
      """
      # TODO: can tcol be replaced by maxlag + 2?
      # TODO: This could be changed to laggedRHS and exog keyword arguments if
      #    this will be more general.
  
      results = {}
      method = method.lower()
      for lag in range(startlag, startlag + maxlag + 1):
          mod_instance = mod(endog, exog[:, :lag], *modargs)
          results[lag] = mod_instance.fit()
  
      if method == "aic":
          icbest, bestlag = min((v.aic, k) for k, v in results.items())
      elif method == "bic":
          icbest, bestlag = min((v.bic, k) for k, v in results.items())
      elif method == "t-stat":
          # stop = stats.norm.ppf(.95)
          stop = 1.6448536269514722
          # Default values to ensure that always set
          bestlag = startlag + maxlag
          icbest = 0.0
          for lag in range(startlag + maxlag, startlag - 1, -1):
              icbest = np.abs(results[lag].tvalues[-1])
              bestlag = lag
              if np.abs(icbest) >= stop:
                  # Break for first lag with a significant t-stat
                  break
      else:
          raise ValueError(
              f"Information Criterion {method} not understood."
          )
  
      if not regresults:
          return icbest, bestlag
      else:
          return icbest, bestlag, results
  
imports: from __future__ import annotations
  from statsmodels.compat.numpy import lstsq
  from statsmodels.compat.pandas import deprecate_kwarg
  from statsmodels.compat.python import Literal, lzip
  from statsmodels.compat.scipy import _next_regular
  from typing import Union
  import warnings
  import numpy
  from numpy.linalg import LinAlgError
  import pandas
  from scipy import stats
  from scipy.interpolate import interp1d
  from scipy.signal import correlate
  from statsmodels.regression.linear_model import OLS, yule_walker
  from statsmodels.tools.sm_exceptions import CollinearityWarning, InfeasibleTestError, InterpolationWarning, MissingDataError, ValueWarning
  from statsmodels.tools.tools import Bunch, add_constant
  from statsmodels.tools.validation import array_like, bool_like, dict_like, float_like, int_like, string_like
  from statsmodels.tsa._bds import bds
  from statsmodels.tsa._innovations import innovations_algo, innovations_filter
  from statsmodels.tsa.adfvalues import mackinnoncrit, mackinnonp
  from statsmodels.tsa.tsatools import add_trend, lagmat, lagmat2ds
label_desc: Use image node_id 1 for calling a global function with example usage: _autolag(mod, endog, exog, startlag, maxlag, method, modargs, fitargs, regresults) and returns: icbest,  bestlag, icbest,  bestlag,  results