Computing for Research I
Download
1 / 10

Computing for Research I Spring 2011 - PowerPoint PPT Presentation


  • 98 Views
  • Uploaded on

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’

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Computing for Research I Spring 2011' - gabby


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Computing for Research ISpring 2011

Stata Programming

February 21

Primary Instructor:

Elizabeth Garrett-Mayer


Some simple programming
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


Macros
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


Example covariates
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.


Example titles
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"))


Storing results
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!


Global macros
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


More on macros
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.


Looping
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


Programming
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


ad