Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: -swaic- for Stata V7+


From   "Steichen, Thomas J." <SteichT@rjrt.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: -swaic- for Stata V7+
Date   Mon, 21 Mar 2005 15:08:57 -0500

Daniel Chandler reported a less-than-graceful termination of swaic7.ado 
when the minimum AIC model was the null model. 

For those who may have copied it when I previously posted it on the list,
here is a fixed version. Note that although the error did not affect the 
printed output, the error caused the "last estimates" to be changed, thus
the original model had to be rerun to try additional swaic options.

Tom Steichen

------- swaic7.ado ----cut here-----------------------------------

*! version 1.4 TJS    21mar2005 minAIC at null model + formatting
*  version 1.3 TJS    24sep2004 xi: works under both pre and post version 7
*  version 1.2 TJS    23sep2004 added model list, min aic output
*  version 1.1 Z.WANG 22NOV1999 (STB-54: sg134)
*  version 1.0 Z.WANG 07Nov1999 
program define swaic7, rclass     /* changed to rclass */
  version 6.0
  syntax [, FP(str) FChi(str) Back Model ]
  local yvar "`e(depvar)'"

* v7 fix
  global caller = _caller()      /* added */
* di in red "caller: " $caller
    
  if "`e(cmd)'" == "" {
    di in r "last estimates not found"
    exit 301
  } 
  if index(`"logitlogisticpoissonprobit"', `"`e(cmd)'"') > 0 {
    local cmd "`e(cmd)'"
    local yvar1 yvar(`yvar')
  }    
  if "`e(cmd)'" == "cox" {
    if "`e(cmd2)'" != "" {
      local cmd `e(cmd2)'
      local yvar1
      local yvar
    } 
    else {
      di in gr "Please use stcox instead of cox in estimation command."
      exit 199
    }
  }
  if `"`e(cmd2)'"' == `"streg"' {
    local dist `"dist(`e(cmd)')"' 
    local cmd `e(cmd2)'
    local yvar1
    local yvar
  } 
  if "`cmd'" == "" {
    di in w "`e(cmd)' " in gr "not supported by " in w "swaic"
    di in gr _con `"Current"' in w `" swaic"' in gr `" only supports "' 
    di in w  _con "logit logistic poisson stcox streg" 
    di in gr `" and"' in w " probit" in gr "."
    exit 199
  }
  if "`e(offset)'" != "" {
    tempvar off 
    gen `off'=`e(offset)'
    lab var `off' "`e(offset)'"
    local offset offset(`off')
  }
  if "`e(vcetype)'" ~= "" { 
    local robust r
  }
  if "`e(clustvar)'" ~= "" {
    local cluster cluster(`e(clustvar)')
  }
  local ops `offset' `robust' `cluster' `dist'

  tempvar smpl
  qui gen `smpl' = e(sample)
  mat bs = e(b) 
  local xnames : colnames(bs) 
  _xnam `xnames'
  
  local xnames  `r(alln)'
  local vnum  = `r(vnum)'
  local factn = `r(factn)'
  local maic  = .
  local mlist = " "  

  di in gr "Stepwise Model Selection by AIC"  
  di in gr "`cmd' regression. `dist'"
  di in gr "number of obs = " in ye e(N)

/*   formatting changes added for longer var names   
         1         2         3         4         5         6         7        
123456789012345678901234567890123456789012345678901234567890123456789012345678
------------------------------------------------------------------------------
         Xxxxxxxxxxxxxxxxxxx    Df     Chi2   P>Chi2    -2*ll  Df Res.     AIC
------------------------------------------------------------------------------
Full Model                                          Xxxxxx.xx Xxxxxx Xxxxxx.xx   
 Step X: XxxxxxxxxxxxxxxxxxxXxxxxxXxxxxx.xxXxxx.xxxxXxxxxx.xx Xxxxxx Xxxxxx.xx
------------------------------------------------------------------------------
*/

  di in gr _dup(78) "-" 
  di in gr _col(10) %-19s "`yvar'" _col(33) "Df" _col(40) "Chi2" /*    
    */ _col(47) "P>Chi2" _col(57) "-2*ll" _col(64) "Df Res." _col(76) "AIC"
  di in gr _dup(78) "-"
  token `xnames' 
* Display Table
  if "`fp'" == "" {
    local fp "%9.4f" 
  }
  if "`fchi'" == "" {
    local fchi "%9.2f" 
  }

  local decreas = 0
  local endhere = 0
* backward stepping  
  if "`back'" ~= "" {
    qui `cmd' `yvar' `xnames' if `smpl', `ops'
    local aic0 = -2 * e(ll) + 2 * (e(df_m) + `factn')
    local dev0 = -2 * e(ll)
    local rdf0 = e(N) - e(df_m) - `factn'
    local mdf0 = e(df_m)
    di in gr "Full Model" in ye _col(53) `fchi' `dev0' _col(63) /*
      */ %6.0f `rdf0' " " `fchi' `aic0'
    local i = 1
    local xs `xnames'
    local maic = `aic0'               /* added */
    while `i' <= `vnum' {
      local i_1 = `i' - 1
      _stepb, `yvar1' xvar(`xs') cmd(`cmd') smpl(`smpl') /*
        */ factn(`factn')  `ops' 
      local pv`i'  `r(pv)'
      local mdf`i' `r(mdf)'
      local aic`i' `r(aic)'
      local dev`i' `r(dev)'
      local chi`i' = `dev`i'' - `dev`i_1''
      local df`i'  = `mdf`i_1'' - `mdf`i''
      local p`i'   = chiprob(`df`i'', `chi`i'')
      local rdf`i' = `rdf`i_1'' + `df`i''
      if `decreas' == 1 {
        if `aic`i'' > `aic`i_1'' { 
          local endhere = 1
        }  
      } 
      if `aic`i'' < `aic`i_1'' {
        if `endhere' == 0 {
          local maic = `aic`i''               /* added */
          local mlist `r(mlist)'
        }
        local decreas = 1
      }
     di in gr " Step `i': " _col(9) %-19s "-`pv`i''" /*
     */ in ye %6.0f  `df`i''     `fchi' `chi`i'' /*
     */         `fp'  `p`i''     `fchi' `dev`i'' /*
     */   " " %6.0f `rdf`i'' " " `fchi' `aic`i''

      _newx, xvar(`xs') pick(`r(pv)')
      local xs `r(newlst)'
      local i = `i' + 1
    }
  }
* forward stepping  
  else {
    qui `cmd' `yvar' `xnames' if `smpl', `ops'
    local aic0 = -2 * e(ll_0) + 2 * `factn'
    local dev0 = -2 * e(ll_0)
    local rdf0 = e(N) - `factn'
    local mdf0 = 0
    local df0  = 0
    di in gr "Null Model" in ye _col(53) `fchi' `dev0' /*
      */ _col(63) %6.0f `rdf0' " " `fchi' `aic0'
    local xs `xnames'
    local i = 1
    local maic = `aic0'               /* added */
    while `i' <= `vnum' { 
      local i_1 = `i' - 1
      _stepf, `yvar1' xvar(`"`xs'"') cmd(`cmd') smpl(`smpl') /*
        */ `plist' factn(`factn') `ops' 
      local pv`i'    `r(pv)'
      local mdf`i' = `r(mdf)'
      local aic`i' = `r(aic)'
      local dev`i' = `r(dev)'
      local chi`i' = `dev`i_1'' - `dev`i''
      local df`i'  = `mdf`i'' - `mdf`i_1''
      local p`i'   = chiprob(`df`i'', `chi`i'')
      local rdf`i' = `rdf0' - `mdf`i''
      local picked   `picked' `pv`i''
      if `decreas' == 1 {
        if `aic`i'' > `aic`i_1'' { 
          local endhere = 1
        }  
      } 
      if `aic`i'' < `aic`i_1'' {
        local maic = `aic`i''                 /* added */
        if `endhere' == 0 {
          local mlist `picked'
        }
        local decreas = 1
      }
     di in gr " Step `i': " _col(9) %-19s "`pv`i''" /*
     */ in ye %6.0f  `df`i''     `fchi' `chi`i'' /*
     */         `fp'  `p`i''     `fchi' `dev`i'' /*
     */   " " %6.0f `rdf`i'' " " `fchi' `aic`i''

      local plist plist(`picked')
      _newx, xvar(`xs') pick(`pv`i'')
      local xs `r(newlst)'
      local i = `i' + 1
    } 
  }

  di in gr _dup(78) "-"

  if "`model'" != "" { 
    `cmd' `yvar' `mlist' if `smpl', `options' nolog
  }
  di _n as txt "minimun AIC =" as res %9.3f `maic' /*      added
  */    as txt ";  model:" as res " `mlist'"            /* added */
  qui `cmd' `yvar' `xnames' if `smpl', `options'

  return local model   = "`mlist'"            /* added */
  return local min_aic = `maic'               /* added */

end

program define _stepb, rclass 
  syntax [, Yvar(str) (str Xvar(str) CMD(str) smpl(str) factn(str) *]
  token `xvar'
  local aicmin = 10000
  local mdf    = .
  local dev    = .
  local i = 1
  while "``i''" ~= "" {
    local i_1 = `i' - 1  
    _newx, xvar(`xvar') pick(``i'') 
    local newx `r(newlst)'
    qui `cmd' `yvar' `newx' if `smpl', `options'
    local dev`i' = -2 * e(ll)
    local df`i' = e(df_m)   
    local aic`i' = `dev`i'' + 2 * (`df`i'' + `factn')
    if `aic`i'' < `aicmin' {
      local pv       "``i''"
      local mdf    = `df`i''
      local dev    = `dev`i''
      local mlist    `newx'
      local aicmin = `aic`i''
    }
    local i = `i' + 1
  }
  return local pv    `pv'
  return local aic = `aicmin'
  return local mdf = `mdf'
  return local dev = `dev'
  return local mlist `mlist'
end

program define _stepf, rclass 
  syntax [, Yvar(str) plist(str) Xvar(str) factn(str) CMD(str) smpl(str) *]
  token `xvar'
  local aicmin = 10000
  local mdf    = .
  local dev    = .
  local i = 1
  while "``i''" ~= "" {
    local i_1 = `i' - 1  
    qui `cmd' `yvar' `plist' ``i'' if `smpl', `options'
    local dev`i' = -2 * e(ll)
    local aic`i' = `dev`i'' + 2 * (e(df_m) + `factn')
    if `aic`i'' < `aicmin' {
      local pv       "``i''"
      local mdf    = e(df_m)
      local dev    = `dev`i''
      local aicmin = `aic`i''
    }
    local i = `i' + 1
  }
  return local pv    `pv'
  return local aic = `aicmin'
  return local mdf = `mdf'
  return local dev = `dev'
end

program define _ab, rclass
  token `0', parse(" _")
  if $caller < 7 { unab cat:  `1'* }          /* added */
    else         { unab cat: _`1'* }   
  return local cname "`1'*"
  token `cat'
  local i = 1
  while "``i''" != "" {
    local i = `i' + 1
  }
  return scalar catdf = `i' - 1
end

program define _xnam, rclass
  local factn = 0
  while "`1'" != "" {
    if $caller < 7 {                          /* added  */
      local if1 = substr("`1'", 1, 1)
      local if2 = "_"
      local i1  = 1
      local if3 = "I"
      local u   = ""
    }
    else {
      local if1 = "`1'"
      local if2 = "_cons"
      local i1  = 2
      local if3 = "_I"
      local u   = "_"
    }
    if "`if1'" != "`if2'" {                   /* modified */
      local xlist `xlist' `1'
    }
    else {
      local factn = `factn' + 1
    }
    mac shift
  }
  token `xlist'
  local i = 1 
  while "`1'" != "" {
    if substr("`1'", 1, `i1') == "`if3'" {    /* modified */
      if $caller < 7 { local x = "`1'" }      /* added  */
        else         { local x = substr("`1'", 2, .) }
      _ab `x'                                 /* modified */
      local catdf = r(catdf)
      local xnam`i' `u'`r(cname)'             /* modified */
      mac shift `catdf'
    }
    else {
      local xnam`i' `1'
      mac shift
    }
    local alln `alln' `xnam`i''
    local i = `i' + 1
  }
  return local alln `alln'
  return local vnum = `i' - 1
  return local factn `factn'
end  

program define _newx, rclass
  syntax [, Xvar(str) Pick(str)]  
  token `xvar'
  while "`1'" != "" {
    if  "`1'" != "`pick'" {
      local newlst "`newlst' `1'"
    }
    mac shift
  }
  return local newlst `newlst'
end

----------------------------------------
 

-----------------------------------------
CONFIDENTIALITY NOTE:  This e-mail message, including any  attachment(s),
contains information that may be confidential,  protected by the attorney-
client or other legal privileges, and/or  proprietary non-public
information.  If you are not an intended  recipient of this message or an
authorized assistant to an intended  recipient, please notify the sender by
replying to this message and  then delete it from your system.  Use,
dissemination, distribution,  or reproduction of this message and/or any of
its attachments (if  any) by unintended recipients is not authorized and
may be unlawful.


*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index