*
* @StepProbit  depvar  start  end
* # list of explanatory variables in regression format.
*
* @StepProbit does a backwards stepwise reduction of a probit model,
* dropping one variables each pass as long as the smallest t-statistic
* is less than the threshold in absolute value.
*
* Options:
*   THRESHOLD=threshold t-statistic[1.50]
*
*   FORCE=number of regressors to include in all models [0]
*     By default, all variables listed on the supplementary card are treated
*     identically. Any may be included or omitted from the final regression.
*     To force regressors into every model, list those variables first on
*     the supplementary card and use the option FORCE to indicate how many
*     are to be forced in. If some of these variables are handled using lag
*     fields, count the number of actual regressors.

*   [PRINT]/NOPRINT
*     NOPRINT suppresses the output
*   TITLE=Title for output ["Stepwise Probit"]
*
* Revision Schedule:
*   04/2005 Written by Tom Doan, Estima
*   09/2023 Revised to add FORCE, PRINT and TITLE options
*   09/2023 Revised to correct final regression
*
procedure stepprobit depvar start end
type series depvar
type integer start end
*
option real threshold 1.50
option integer force  0
option switch  print  1
option string  title
*
local vec[int] pullv
local int      mint
local integer  i startl endl
local string   ltitle
*
* Run the probit over all variables
*
ddv(dist=probit,print) depvar start end
compute startl=%regstart(),endl=%regend()
*
loop
   *
   * Find the position of the smallest t-statistic in absolute value.
   * This ignores the first <<force>> positions in searching for the smallest.
   *
   compute mint=force+%minindex(%xsubvec(%abs(%tstats),force+1,%nreg))
   *
   * If this is larger than the threshold, we're done
   *
   if abs(%tstats(mint))>threshold
      break
   *
   * Generate a VECT[INT] of the positions that we want to keep
   * from this regression. This will skip over the one with the
   * minimum t-stat.
   *
   dim pullv(%nreg-1)
   compute ipull=0
   ewise pullv(i)=(ipull=ipull+1+(i==mint))
   *
   * Run a new probit with the reduced set of variables
   *
   ddv(noprint) depvar startl endl
   # %rlfromtable(%tableextract(%eqntable(0),pullv))
end loop
*
* Run the same probit with the PRINT option turned back on
*
if %defined(title)
   compute ltitle=title
else
   compute ltitle="Stepwise Probit"

ddv(print=print,title=ltitle) depvar startl endl
# %rlfromtable(%eqntable(0))
end
