For starters, I think you want to use -forvalues- instead of -foreach-.
Because you do -tab- after -sum-, r(mean) is lost. Try:
qui sum hit
local mu=r(mean)
tab `e(depvar)' pred, freq nocol
di in green "the mean hit rate is `mu'"
if you want to store the mean, or just reverse the order of the
-sum- and the -tab-.
Hope this helps.
Jeph
Dirk Nachbar wrote:
All
here is a program (the last for this week I promise) which is supposed
to run after various choice models. It should calculate the hit rate of
correct predictions. Two things create problems: the mean hit rate is
not shown and the -foreach- loop doesn't seem to work. Maybe someone
would like to take a closer look.
clear
set obs 100
gen x=invnorm(uniform())
gen e=invnorm(uniform())
gen y=round(2*exp(1+x+e)/(1+exp(1+x+e)),1)
ologit y x
/*
Program to run after most Discrete Choice Models
it reports the hit rate of correct predictions
*/
capture program drop postdcm
program define postdcm
version 8.2
syntax [if] [in] [, threshold(real 0.5)]
if `threshold'<0 | `threshold'>1 {
di as error "threshold has to be between 0 and 1"
exit 198
}
if "`e(cmd)'"=="" {
di as error "no DCM estimations found"
exit 198
}
if "`e(cmd)'"=="logit" | "`e(cmd)'"=="probit" | "`e(cmd)'"=="xtlogit" |
"`e(cmd)'"=="xtprobit" {
tempvar pred pred2
qui predict pred
gen pred2=(pred>=`threshold')
gen hit=(`e(depvar)'==pred2)
sum hit
tab `e(depvar)' pred2, freq nocol
di in green "the mean hit rate is `r(mean)'"
}
if "`e(cmd)'"=="ologit" | "`e(cmd)'"=="oprobit" | "`e(cmd)'"=="mlogit" |
"`e(cmd)'"=="mprobit" {
qui sum `e(depvar)'
gen pred=`r(min)'
foreach i=`r(min)'/`r(max)' {
tempvar pred`i'
qui predict pred`i', outcome(`i')
if `i'==`r(min)' {
gen pred=`i'
local max=pred`i'
}
else if pred`i'>`max' {
replace pred=`i'
local max=pred`i'
}
}
gen hit=(`e(depvar)'==pred)
qui sum hit
tab `e(depvar)' pred, freq nocol
di in green "the mean hit rate is `r(mean)'"
}
end
postdcm
Dirk Nachbar
Assistant Economist
Pensim2
Department for Work and Pensions
Level 4, The Adelphi
1-11 John Adam St
WC2N 6HT London
020 796 28531
**********************************************************************
This document is strictly confidential and is intended only for use by
the addressee. If you are not the intended recipient, any disclosure,
copying, distribution or other action taken in reliance of the
information contained in this e-mail is strictly prohibited. Any views
expressed by the sender of this message are not necessarily those of the
Department for Work and Pensions. If you have received this transmission
in error, please use the reply function to tell us and then permanently
delete what you have received. Please note: Incoming and outgoing e-mail
messages are routinely monitored for compliance with our policy on the
use of electronic communications.
**********************************************************************
The original of this email was scanned for viruses by Government Secure
Intranet (GSi) virus scanning service supplied exclusively by Cable &
Wireless in partnership with MessageLabs.
On leaving the GSI this email was certified virus free.
The MessageLabs Anti Virus Service is the first managed service to
achieve the CSIA Claims Tested Mark (CCTM Certificate Number
2006/04/0007), the UK Government quality mark initiative for information
security products and services. For more information about this please
visit www.cctmark.gov.uk
*
* 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/