Statalist The Stata Listserver


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

Re: st: predict at observed values


From   [email protected]
To   [email protected]
Subject   Re: st: predict at observed values
Date   Sun, 06 May 2007 20:12:41 -0500

Sorry the code did not come through. Here it is and it is adapted from
www.indiana.edu/~jslsoc/stata/spostci/examples/prvalue_observed.log.

. //  Predicted outcomes for binary probit using delta method
. quiet xi: probit caries2 seals c_age sex  i.p_educ3 hincome2  p_hins stayw2
p_bcountry2  lang2 if race==0 & nomiss==1, robust nolog
. * list of variables to be held at their observed values
. local obsx "c_age sex  i.p_educ3 hincome2  p_hins stayw2   p_bcountry2  lang2"
. * variable to contain predictions for each case
. gen pr_seals = .
(245 missing values generated)
.     label var pr_seals "Predictions at seals=1"
. * loop through each case
. local nobs = _N
. foreach i of numlist 1/`nobs'  {
  2.     * local to hold x() string with observed values
.     local xis ""
  3.     * loop through each variable in the observed list
.     foreach v of varlist `obsx'    {
  4.         * get value for current observation
.         local x = `v'[`i']
  5.         * add it to the string
.         local xis "`xis' `v'=`x'"
  6.     }
  7.     * compute prvalue, but only look at output for first case
.     if `i'==1 {
  8.         di _new "For the first observation"
  9.         di      "========================="
 10.         prvalue, x(seals=1 `xis')
 11.     }
 12.     else {
 13.         qui prvalue, x(seals=0 `xis')
 14.     }
 15.     * retreive predicted probability
.     local p = pepred[2,2]
 16.     * insert prediction into variable
.     qui replace pr_seals = `p' if _n==`i'
 17.     * look at prediction for first case
.     if `i'==1 {
 18.         local pr = pr_seals[1]
 19.         di _new "Prediction from prvalue: `pr'"
 20.     }
 21. }
i:  operator invalid
r(198);

*
*   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