1 / 10

Computing for Research I Spring 2011

Computing for Research I Spring 2011. Stata Programming February 21. Primary Instructor: Elizabeth Garrett-Mayer. Some simple programming. Once again, princeton’s site has some great easy info: http:// data.princeton.edu/stata/programming.aspx We will discuss a few things: ‘macros’

gabby
Download Presentation

Computing for Research I Spring 2011

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computing for Research ISpring 2011 Stata Programming February 21 Primary Instructor: Elizabeth Garrett-Mayer

  2. Some simple programming • Once again, princeton’s site has some great easy info: http://data.princeton.edu/stata/programming.aspx • We will discuss a few things: • ‘macros’ • looping • writing commands • We will not discuss ‘mata’: powerful matrix programming language

  3. macros • macro = a name associated with some text. • macros can be local or global in scope. • Example of use: shorthand for repeated phrase • graphics title • set of ‘adjustment’ covariates

  4. Example: covariates * use SCBC data use "I:\Classes\StatComputingI\SCBC2004.dta", clear * make tumor numeric and transform gen sizen=real(tumor) gen logsize = log(sizen) replace logsize = . if sizen==999 regress logsize age black graden *define local macro local adjusters age black graden regress logsize `adjusters' regress logsize `adjusters' i.ercat regress logsize `adjusters' i.prcat regress logsize `adjusters' i.ercati.prcat NOTE: must use accent (`) in upper left of keyboard as beginning quote and apostrophe (‘) (next to enter key) for end quote.

  5. Example: titles * another example infile str14 country setting effort change /// using http://data.princeton.edu/wws509/datasets/effort.raw, clear graph twoway (lfitci change setting) /// (scatter change setting) /// , title("Fertility Decline by Social Setting") /// ytitle("Fertility Decline") /// legend(ring(0) pos(5) order(2 "linear fit" 1 "95% CI")) local gtitles title("Fertility Decline by Social Setting") ytitle("Fertility Decline") * with macro graph twoway (lfitci change setting) /// (scatter change setting) /// , `gtitles' legend(ring(0) pos(5) order(2 "linear fit" 1 "95% CI")) * without macro graph twoway (lfitci change setting) /// (scatter change setting) /// , legend(ring(0) pos(5) order(2 "linear fit" 1 "95% CI"))

  6. Storing results * run regression and store r-squared value regress change setting local rsq = e(r2) display rsq * run new regression regress change setting effort display e(r2) see old saved r-squared display rsq * still there!

  7. Global macros • Global macros have names of up to 32 characters and, as the name indicates, have global scope. • You define a global macro using global name [=] text and evaluate it using $name. (You may need to use ${name} to clarify where the name ends.) • “I suggest you avoid global macros because of the potential for name conflicts.” • A useful application, however, is to map the function keys on your keyboard. If you work on a shared network folder with a long name try something like this

  8. More on macros • Macros can also be used to obtain and store information about the system or the variables in your dataset using extended macro functions. • For example you can retrieve variable and value labels, a feature that can come handy in programming. • There are also commands to manage your collection of macros, including macro list and macro drop. Type help macro to learn more.

  9. Looping • foreach: loops over a set of variables • forvalues: loops over a set of values (index) • Also: • while loops • if and else sets of commands

  10. Programming • ‘ado’ files • create commands in ado file and put them in the appropriate directory for Stata to find • Can also create them in do files for local use • See • http://data.princeton.edu/stata/programming.aspx • www.ssc.upenn.edu/scg/stata/stata-programming-1.ppt

More Related