11.07.2015 Views

[U] User's Guide

[U] User's Guide

[U] User's Guide

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

246 [ U ] 18 Programming Statabroken out and stored in local macros for us. For a newvarname, the new name typed by the useris placed in the local macro varlist, and the type of the variable (float, double, . . . ) is placedin typlist (even if the user did not specify a storage type, in which case the type is the currentdefault storage type).This is now an excellent program. There are, however, two more improvements we could make.First, we have demonstrated that, by the use of ‘syntax newvarname’, we can allow the user todefine not only the name of the created variable but also the storage type. However, when it comesto the creation of intermediate variables, such as ‘hii’ and ‘di2’, it is good programming practiceto keep as much precision as possible. We want our final answer to be precise as possible, regardlessof how we ultimately decide to store it. Any calculation that uses a previously generated variablewould benefit if the previously generated variable were stored in double precision. Below we modifyour program appropriately:program fvaratioversion 11syntax newvarnametempvar hii ei di2quietly {predict double ‘hii’ if e(sample), hatend}begin fvaratio.ado, version 7// changed, as are// other linespredict double ‘ei’ if e(sample), residgen double ‘di2’ = (‘ei’*‘ei’)/e(rss)gen ‘typlist’ ‘varlist’ = ///(e(N)-(e(df_m)+1)) / (e(N)-(e(df_m)+2)) * ///(1 - ‘di2’/(1-‘hii’)) / (1-‘hii’)end fvaratio.ado, version 7As for the second improvement we could make, fvaratio is intended to be used sometimeafter regress. How do we know the user is not misusing our program and executing it after, say,logistic? e(cmd) will tell us the name of the last estimation command; see [U] 18.9 Accessingresults calculated by estimation commands and [U] 18.10.2 Saving results in e() above. We shouldchange our program to readbegin fvaratio.ado, version 8program fvaratioversion 11if "‘e(cmd)’"!="regress" {// new this versionerror 301}syntax newvarnametempvar hii ei di2quietly {predict double ‘hii’ if e(sample), hatpredict double ‘ei’ if e(sample), residgen double ‘di2’ = (‘ei’*‘ei’)/e(rss)gen ‘typlist’ ‘varlist’ = ///(e(N)-(e(df_m)+1)) / (e(N)-(e(df_m)+2)) * ///(1 - ‘di2’/(1-‘hii’)) / (1-‘hii’)}endend fvaratio.ado, version 8The error command issues one of Stata’s prerecorded error messages and stops our program. Error301 is “last estimates not found”; see [P] error. (Try typing error 301 at the command line.)In any case, this is a perfect program.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!