docstr: Represent a collection of survival times with possible
  stratification and left truncation.
  
  Parameters
  ----------
  time : array_like
      The times at which either the event (failure) occurs or
      the observation is censored.
  status : array_like
      Indicates whether the event (failure) occurs at `time`
      (`status` is 1), or if `time` is a censoring time (`status`
      is 0).
  exog : array_like
      The exogeneous (covariate) data matrix, cases are rows and
      variables are columns.
  strata : array_like
      Grouping variable defining the strata.  If None, all
      observations are in a single stratum.
  entry : array_like
      Entry (left truncation) times.  The observation is not
      part of the risk set for times before the entry time.  If
      None, the entry time is treated as being zero, which
      gives no left truncation.  The entry time must be less
      than or equal to `time`.
  offset : array_like
      An optional array of offsets
docstr_tok: ["Represent","a","collection","of","survival","times","with","possible","stratification","and","left","truncation",".","Parameters","--","--","--","--","--","time",":","array_like","The","times","at","which","either","the","event","(","failure",")","occurs","or","the","observation","is","censored",".","status",":","array_like","Indicates","whether","the","event","(","failure",")","occurs","at","`","time","`","(","`","status","`","is","1",")",",","or","if","`","time","`","is","a","censoring","time","(","`","status","`","is","0",")",".","exog",":","array_like","The","exogeneous","(","covariate",")","data","matrix",",","cases","are","rows","and","variables","are","columns",".","strata",":","array_like","Grouping","variable","defining","the","strata",".","If","None",",","all","observations","are","in","a","single","stratum",".","entry",":","array_like","Entry","(","left","truncation",")","times",".","The","observation","is","not","part","of","the","risk","set","for","times","before","the","entry","time",".","If","None",",","the","entry","time","is","treated","as","being","zero",",","which","gives","no","left","truncation",".","The","entry","time","must","be","less","than","or","equal","to","`","time","`",".","offset",":","array_like","An","optional","array","of","offsets"]
code: def __init__(
      self, time, status, exog, strata=None, entry=None, offset=None
  ):
      """
      Represent a collection of survival times with possible
      stratification and left truncation.
  
      Parameters
      ----------
      time : array_like
          The times at which either the event (failure) occurs or
          the observation is censored.
      status : array_like
          Indicates whether the event (failure) occurs at `time`
          (`status` is 1), or if `time` is a censoring time (`status`
          is 0).
      exog : array_like
          The exogeneous (covariate) data matrix, cases are rows and
          variables are columns.
      strata : array_like
          Grouping variable defining the strata.  If None, all
          observations are in a single stratum.
      entry : array_like
          Entry (left truncation) times.  The observation is not
          part of the risk set for times before the entry time.  If
          None, the entry time is treated as being zero, which
          gives no left truncation.  The entry time must be less
          than or equal to `time`.
      offset : array_like
          An optional array of offsets
      """
  
      # Default strata
      if strata is None:
          strata = np.zeros(len(time), dtype=np.int32)
  
      # Default entry times
      if entry is None:
          entry = np.zeros(len(time))
  
      # Parameter validity checks.
      self._check(time, status, strata, entry)
  
      # Get the row indices for the cases in each stratum
      stu = np.unique(strata)
      sth = {x: [] for x in stu}
      for i, k in enumerate(strata):
          sth[k].append(i)
      stratum_rows = [np.asarray(sth[k], dtype=np.int32) for k in stu]
      stratum_names = stu
  
      # Remove strata with no events
      ix = [
          i for i, ix in enumerate(stratum_rows) if status[ix].sum() > 0
      ]
      self.nstrat_orig = len(stratum_rows)
      stratum_rows = [stratum_rows[i] for i in ix]
      stratum_names = [stratum_names[i] for i in ix]
  
      # The number of strata
      nstrat = len(stratum_rows)
      self.nstrat = nstrat
  
      # Remove subjects whose entry time occurs after the last event
      # in their stratum.
      for stx, ix in enumerate(stratum_rows):
          last_failure = max(time[ix][status[ix] == 1])
  
          # Stata uses < here, R uses <=
          ii = [i for i, t in enumerate(entry[ix]) if t <= last_failure]
          stratum_rows[stx] = stratum_rows[stx][ii]
  
      # Remove subjects who are censored before the first event in
      # their stratum.
      for stx, ix in enumerate(stratum_rows):
          first_failure = min(time[ix][status[ix] == 1])
  
          ii = [i for i, t in enumerate(time[ix]) if t >= first_failure]
          stratum_rows[stx] = stratum_rows[stx][ii]
  
      # Order by time within each stratum
      for stx, ix in enumerate(stratum_rows):
          ii = np.argsort(time[ix])
          stratum_rows[stx] = stratum_rows[stx][ii]
  
      if offset is not None:
          self.offset_s = []
          for stx in range(nstrat):
              self.offset_s.append(offset[stratum_rows[stx]])
      else:
          self.offset_s = None
  
      # Number of informative subjects
      self.n_obs = sum([len(ix) for ix in stratum_rows])
  
      self.stratum_rows = stratum_rows
      self.stratum_names = stratum_names
  
      # Split everything by stratum
      self.time_s = self._split(time)
      self.exog_s = self._split(exog)
      self.status_s = self._split(status)
      self.entry_s = self._split(entry)
  
      # Precalculate some indices needed to fit Cox models.
      # Distinct failure times within a stratum are always taken to
      # be sorted in ascending order.
      #
      # ufailt_ix[stx][k] is a list of indices for subjects who fail
      # at the k^th sorted unique failure time in stratum stx
      #
      # risk_enter[stx][k] is a list of indices for subjects who
      # enter the risk set at the k^th sorted unique failure time in
      # stratum stx
      #
      # risk_exit[stx][k] is a list of indices for subjects who exit
      # the risk set at the k^th sorted unique failure time in
      # stratum stx
      self.ufailt_ix, self.risk_enter, self.risk_exit, self.ufailt = (
          [],
          [],
          [],
          [],
      )
  
      for stx in range(self.nstrat):
          # All failure times
          ift = np.flatnonzero(self.status_s[stx] == 1)
          ft = self.time_s[stx][ift]
  
          # Unique failure times
          uft = np.unique(ft)
          nuft = len(uft)
  
          # Indices of cases that fail at each unique failure time
          # uft_map = {x:i for i,x in enumerate(uft)} # requires >=2.7
          uft_map = {x: i for i, x in enumerate(uft)}  # 2.6
          uft_ix = [[] for k in range(nuft)]
          for ix, ti in zip(ift, ft):
              uft_ix[uft_map[ti]].append(ix)
  
          # Indices of cases (failed or censored) that enter the
          # risk set at each unique failure time.
          risk_enter1 = [[] for k in range(nuft)]
          for i, t in enumerate(self.time_s[stx]):
              ix = np.searchsorted(uft, t, "right") - 1
              if ix >= 0:
                  risk_enter1[ix].append(i)
  
          # Indices of cases (failed or censored) that exit the
          # risk set at each unique failure time.
          risk_exit1 = [[] for k in range(nuft)]
          for i, t in enumerate(self.entry_s[stx]):
              ix = np.searchsorted(uft, t)
              risk_exit1[ix].append(i)
  
          self.ufailt.append(uft)
          self.ufailt_ix.append(
              [np.asarray(x, dtype=np.int32) for x in uft_ix]
          )
          self.risk_enter.append(
              [np.asarray(x, dtype=np.int32) for x in risk_enter1]
          )
          self.risk_exit.append(
              [np.asarray(x, dtype=np.int32) for x in risk_exit1]
          )
  
imports: import numpy
  from statsmodels.base import model
  import statsmodels.base.model
  from statsmodels.tools.decorators import cache_readonly
  from statsmodels.compat.pandas import Appender
label_desc: Use image node_id 1 to create a new PHSurvivalTime object  with example: obj = PHSurvivalTime(time, status, exog, strata, entry, offset)