Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Re: adjust problem (solution)


From   Joseph Coveney <[email protected]>
To   Statalist <[email protected]>
Subject   Re: st: Re: adjust problem (solution)
Date   Wed, 30 Aug 2006 11:46:17 +0900

Dimitriy V. Masterov wrote:

In case anyone is interested, I came up with a solution that works
around the limitations of parsing and string length:

foreach outcome in o1 o2 o3 {;
probit `outcome' x1 x2 x3 age age2 ;
local a: colfullnames e(b);
local b "age age2 _cons";
local c: list a - b;
adjust `c","_cons","",.)' if e(sample), pr by(age age2) gen(`outcome'_hat)
}

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

Shouldn't `c","_cons","",.)'  be `c' instead?

Using the code (top part of do-file below), I get missing values when one of
the x's predicts failure perfectly.

  +---------------------------------------------------------------------+
  | o1   x1   o2   x2   o3   x3   age   age2   o1_hat   o2_hat   o3_hat |
  |---------------------------------------------------------------------|
  |  1    0    0    0    0    1    53   2809     0.53        .     0.51 |
  |  0    0    0    1    1    0    18    324     0.57     0.73     0.42 |
  |  0    0    0    1    1    1    49   2401     0.53     0.63     0.52 |
  |  0    0    1    1    1    1    58   3364     0.53     0.64     0.50 |
  |  0    1    0    0    1    0    54   2916     0.53        .     0.51 |
  |---------------------------------------------------------------------|
  |  0    1    0    1    1    0    42   1764     0.53     0.63     0.52 |
  |  0    1    1    1    0    1    34   1156     0.53     0.65     0.51 |
  |  0    0    0    0    1    1    35   1225     0.53        .     0.51 |
  |  1    0    1    1    1    0    61   3721     0.54     0.65     0.49 |
  |  1    1    0    0    1    0    39   1521     0.53        .     0.52 |
  +---------------------------------------------------------------------+

Do you want this?  It happens because the observations are dropped at the
execution of -probit-:
note: x2 != 1 predicts failure perfectly
      x2 dropped and 63 obs not used

You can omit the -if e(sample)- if you want to avoid this.  If you
need to keep the -if e(sample)- in the -adjust- command for other some
reason, then you can fix-up the predictor list up-front, prior to -probit-.
See the bottom part of the do-file below.

Joseph Coveney

clear
set more off
set seed `=date("2006-08-30", "ymd")'
set obs 200
forvalues i = 1/3 {
    generate byte o`i' = uniform() > 0.5
    generate byte x`i' = uniform() > 0.5
}
generate float age = 18 + floor(60* uniform())
generate float age2 = age^2
replace x2 = 1 if o2 == 1
foreach outcome in o1 o2 o3 {
    probit `outcome' x1 x2 x3 age age2
    local a: colfullnames e(b)
    local b "age age2 _cons"
    local c: list a - b
    quietly adjust `c' if e(sample), pr by(age age2) gen(`outcome'_hat)
}
format o?_* %04.2f
list in 1/10, noobs // output shown above
*
* Preventing missing predictions
*
drop *_hat
foreach outcome in o1 o2 o3 {
    probit `outcome' x1 x2 x3 age age2
    matrix Rules = e(rules)
// I read the fine manual.
// Not there.  (For -probit-, at least.)
// Hinted at in -predict , rules | asif-.
// What are all the elements in the e(rules) vector?
    capture assert Rules[1,4] == 0
    if _rc {
        local varlist : colfullnames e(b)
        local cons _cons
        probit `outcome' `: list varlist - cons'
    }
    local a: colfullnames e(b)
    local b age age2 _cons
    quietly adjust `: list a - b' if e(sample), ///
      pr by(age age2) generate(`outcome'_hat)
}
format o?_* %04.2f
list in 1/10, noobs
exit


*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index