Computing for Research I Spring 2013 Primary Instructor: Elizabeth Garrett-Mayer Stata Programming February 21.

Slides:



Advertisements
Similar presentations
XP New Perspectives on Introducing Microsoft Office 2003 Tutorial 1 1 Using Common Features of Microsoft Office 2003 Tutorial 1.
Advertisements

ITEC 1001 Tutorial 1 Using Common Features of Microsoft Office 2003.
1. Overview Basic commands for writing do-files
Computing for Research I Spring 2013 Primary Instructor: Elizabeth Garrett-Mayer Regression Using Stata February 19.
Stata and logit recap. Topics Introduction to Stata – Files / directories – Stata syntax – Useful commands / functions Logistic regression analysis with.
Introduction to the gedit editor. gedit: the Gnome editor Gnome: Gnome is a freely available (i.e., no cost) desktop environment for the UNIX system The.
 Statistics package  Graphics package  Programming language  Can be used to share/reproduce analyses  Many new packages being created - can be downloaded.
Computing for Research I Spring 2011 Primary Instructor: Elizabeth Garrett-Mayer Stata Programming February 28.
Information & Library Services Australian Education Index, British Education Index and ERIC Sally Giffen August 2006.
By Hrishikesh Gadre Session II Department of Mechanical Engineering Louisiana State University Engineering Equation Solver Tutorials.
XP New Perspectives on Microsoft Office Word 2003 Tutorial 1 1 Microsoft Office Word 2003 Tutorial 1 – Creating a Document.
XP 1 Microsoft Office Word 2003 Tutorial 1 – Creating a Document.
XP 1 Using Common Features of Microsoft Office 2003 Rod Rodrigues.
Introduction to MATLAB MECH 300H Spring Starting of MATLAB.
XP Using Common Features of Microsoft Office Tutorial 1.
Lesson 3 Text Basics Adapted From Source:
EPSII 59:006 Spring Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops.
STATA User Group September 2007 Shuk-Li Man and Hannah Evans.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller.
L1: INTRODUCTION Getting started with Stata Angela Ambroz May 2015.
Econometric Analysis Using Stata
Microsoft Office Word 2003 Tutorial 1 Creating a Document.
SAS Workshop Lecture 1 Lecturer: Annie N. Simpson, MSc.
TERMS TO KNOW. Desktop This does not mean a computer desktop vs. a laptop. You probably keep a number of commonly used items on your desk at home such.
Automating Your Work: An Introduction to Programming in Stata Shawna N. Smith 29 July 2009.
Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research.
XP New Perspectives on Browser and Basics Tutorial 1 1 Browser and Basics Tutorial 1.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Objectives Understand what MATLAB is and why it is widely used in engineering and science Start the MATLAB program and solve simple problems in the command.
Scottish Social Survey Network: Master Class 1 Data Analysis with Stata Dr Vernon Gayle and Dr Paul Lambert 23 rd January 2008, University of Stirling.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Tricks in Stata Anke Huss Generating „automatic“ tables in a do-file.
XP 1 Microsoft Word 2002 Tutorial 1 – Creating a Document.
STATA Mini Course Fall 2015 Jane Leber Herr Littauer 113 1Stata Mini Course – Spring 2015.
L3: BIG STATA CONCEPTS Getting started with Stata Angela Ambroz May 2015.
Microsoft Word ITE115 Trisha Cummings. MsWord - Word Processing Program Allows you to create Letters, Envelopes, Mailing Labels, Memo’s , Fax’s.
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Advanced Stata Workshop FHSS Research Support Center.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Microsoft Office XP Illustrated Introductory, Enhanced Started with Windows 2000 Getting.
Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004.
March 23 & 28, Csci 2111: Data and File Structures Week 10, Lectures 1 & 2 Hashing.
March 23 & 28, Hashing. 2 What is Hashing? A Hash function is a function h(K) which transforms a key K into an address. Hashing is like indexing.
Parameter Study Principles & Practices. What is Parameter Study? Parameter study is the application of a single algorithm over a set of independent inputs:
Getting Started with Stata 2/11/2010 Tom Tomberlin Nealia Khan Learning Technologies Center Harvard Graduate School of Education.
Dec-15H.S.1 Stata 8, Programing Hein Stigum Presentation, data and programs at:
Comparison of different output options from Stata
Editing Basics Lesson 8. Skills Matrix SKILL #MATRIX SKILL 2.2.1Cut, copy, and paste text 2.2.2Find and replace text 4.1.1Insert building blocks in documents.
1 Econometrics (NA1031) Lecture 1 Introduction. 2 ”How much” type questions oBy how much a unit change in income affects consumption? oBy how much should.
This poster has been designed to act as a customisable template. You do not have to use this template but it might be a useful starting point. The poster.
Today Introduction to Stata – Files / directories – Stata syntax – Useful commands / functions Logistic regression analysis with Stata – Estimation – GOF.
5-1 MGMG 522 : Session #5 Multicollinearity (Ch. 8)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Stata Review Session Economics 1018 Abby Williamson and Hongyi Li November 17, 2006.
Topics Introduction to Stata – Files / directories – Stata syntax – Useful commands / functions Logistic regression analysis with Stata – Estimation –
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Stata – be the master Stata. “After I have run my standard commands, what can I do to make my model better (and understand better what is going on)?”
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
IPEmotion License Management PM (V1.2).
111 State Management Beginning ASP.NET in C# and VB Chapter 4 Pages
Release Numbers MATLAB is updated regularly
Econometrics 704 Emilio Cuilty
ECONOMETRICS ii – spring 2018
Multiple Imputation Using Stata
T. Jumana Abu Shmais – AOU - Riyadh
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Stata Basic Course Lab 2.
SEM: Step by Step In AMOS and Mplus.
Presentation transcript:

Computing for Research I Spring 2013 Primary Instructor: Elizabeth Garrett-Mayer Stata Programming February 21

Some simple programming Once again, princeton’s site has some great easy info: We will discuss a few things: – ‘macros’ – looping – writing commands We will not discuss ‘mata’: powerful matrix programming language

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 syntax: local name content

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.ercat i.prcat NOTE: must use accent (`) in upper left of keyboard as beginning quote and apostrophe (‘) (next to enter key) for end quote.

More examples local erprknown ercat<9 & prcat<9 regress logsize `adjusters' i.ercat i.prcat if `erprknown‘ An important property of the local macros, and the reason they are called "local", is that they only exist within the process where they were defined This means when you highlight and run from a ‘do’ file, all of the local definitions need to be defined in the highlighted portion. Stata will NOT remember locals defined from earlier calls to the do file!

Example: titles * another example infile str14 country setting effort change /// using 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 Stata commands (and new commands that you and others write) can be classified as follows: – r-class: General commands such as summarize. Results are returned in r() and generally must be used/saved before executing more commands. – e-class: Estimation commands such as regress, logistic etc., that fit statistical models. Results are returned in e() and remain there until the next model is estimated.

(continued) – s-class: Programming commands that assist in parsing. These commands are relatively rare. Results are returned in s(). – n-class: Commands that do not save results at all, such as generate and replace. – c-class: Values of system parameters and settings and certain constants (such as the value of π) which are contained in c().

Accessing returned values return list, ereturn list, sreturn list and creturn list return all the values contained in the r(), e(), s() and c() vectors, respectively. For example, after using summarize, r() will contain r(N), r(mean), r(sd), r(sum) etc. Elements of each of the vectors can be used when creating new variables. They can also be saved as macros.

Using regression results Although coefficients and standard errors from the most recent model are saved in e(), it is quicker to refer to them by using _b[ varname ] and _se[ varname ], respectively. regress change setting effort gen fitvals = setting*_b[setting] + effort*_b[effort] + _cons*_b[_cons] predict fit

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!

Saving matrix results matrix list e(b) matrix list e(V) matrix betamodel1=get(_b) matrix list betamodel1 * help matrix get

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: global F5 \\server\shared\research\project\subproject\ Then when you hit F5, Stata will substitute the full name. And your do files can use commands like do ${F5}dofile. (We need the braces to indicate that the macro is called F5, not F5dofile.)

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. Check out help macro to learn more.

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 ‘ 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 – – –

Ado files An ado-file (“automatic do-file”) is a do-file that defines a Stata command. It has the file extension.ado. Not all Stata commands are defined by ado-files: some are built-in commands. The difference between a do-file and an ado-file is that when the name of the latter is typed as a Stata command, Stata will search for and run that file. For example, the program mysum could be saved in mysum.ado and used in future sessions

Ado files Ado-files often have help (.hlp) files associated with them. There are three main sources of ado-files: – Official updates from StataCorp. – User-written additions (e.g. from the Stata Journal). – Ado-files that you have written yourself. Stata stores these in different locations, which can be reviewed by typing sysdir.

Ado files Official updates are saved in the folder associated with UPDATES. User-written additions are saved in the folder associated with PLUS. Ado-files written by yourself should be saved in the folder associated with PERSONAL. If you have an Internet connection, official updates and user-written ado-files can be installed easily. To install official updates, type: update from