STATA Mini Course Fall 2015 Jane Leber Herr [email protected] Littauer 113 1Stata Mini Course...

27
STATA Mini Course Fall 2015 Jane Leber Herr [email protected] Littauer 113 1 Stata Mini Course – Spring 2015

Transcript of STATA Mini Course Fall 2015 Jane Leber Herr [email protected] Littauer 113 1Stata Mini Course...

Page 1: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

1

STATA Mini Course

Fall 2015

Jane Leber Herr

[email protected]

Littauer 113

Stata Mini Course – Spring 2015

Page 2: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

2

Goal: Make you comfortable with Stata

1) Can’t teach you all of Stata

2) But can teach you basics AND

3) Teach you how to teach yourself Stata

The Stata Mini-Course

Stata Mini Course – Spring 2015

Page 3: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

3

Why is Stata so AWESOME? Let me count the ways…

1) In Excel, you can’t produce a step-by-step record.In Stata, .do files do exactly that!

Why is that important?

• Can remind yourself what you did (and why)

• Can tweak one thing, and rerun – Ta Da!

• Can easily find bugs in your code

Why use Stata??

Stata Mini Course – Spring 2015

Page 4: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

4

Why is Stata so AWESOME? Let me count the ways…

1) In Excel, you can’t produce a step-by-step recordIn Stata, .do files do exactly that!

2) Excel is MUCH slower at manipulating data

3) Stata produces much nicer graphs

4) Excel can’t handle econometrics the way Stata can!

How much data manipulation ok in Excel?

NONE!

Why Stata??

Stata Mini Course – Spring 2015

Page 5: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

5

1) Stata mini-course lectures & problem sets

Class Dates:

• Tuesday 2/24

• Tuesday 3/10

• Tuesday 3/31

Problem Set Dates:

• Friday 2/20

• Friday 3/6

• Friday 3/27

• Friday 4/10

Stata Course Structure

Stata Mini Course – Spring 2015

Page 6: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

6

2) Stata office hours (see isite)

Stata Course Structure

Stata Mini Course – Spring 2015

Page 7: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

7

3) Help by email

We will try to respond to emails ASAP, but goal:

• Within 24 hours during the week

• By Monday if over the weekend

Google your question!

Email etiquette:

• If you email both of us, do it in a way that we can tell (then we can cc: each other/not duplicate effort)

• If you figure out the answer on your own, let us know!

Stata Course Structure

Stata Mini Course – Spring 2015

Page 8: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

8

Resources available on the isite:

• Class schedule and pset due dates

• Announcements (e.g., OH switches)

• Our contact info

• OH schedule (appointment sign up)

• Lecture materials

• Problem Sets

• Other AWESOME resources

Stata iSite

Stata Mini Course – Spring 2015

Page 9: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

9

Download from HUIT (see details in PSet #1)

• Keyserver Client (IMPORTANT!)

• Stata (tips on how to figure out which version you need)

• Harvard VPN

Help?

• Email HUIT ([email protected])

• Science Center Computer lab

Downloading Stata

Stata Mini Course – Spring 2015

Page 10: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

10

Stata Windows:

• Command line

• Results panel

• Variables

• List of commands you’ve run so far

What is Stata?

Stata Mini Course – Spring 2015

Page 11: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

11

Most awesome part of Stata!– File that holds all the commands you want Stata to run

– Can be a file that:

• Builds your dataset & variables

• Runs all your regressions

• Builds your results tables

• Builds your figures

• Or any combination!

– You will build a .do file for each Stata pset

.do files!

Stata Mini Course – Spring 2015

Page 12: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

12

Where is the .do file? – .do file button

– Window: “do file editor”

.do file example

Stata Mini Course – Spring 2015

Page 13: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

13

Saving the results in a .log file – log using example.log

…log close

– For Stata psets, will send your instructor:

• your .do file

• the .log file it creates that holds all the results

.log file

Stata Mini Course – Spring 2015

Page 14: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

14

1) Comment at top (e.g., purpose of the .do file)

• Comments:*/* */

2) Save the .do file:

• .do files do NOT autosave – SAVE FREQUENTLY!

Let’s build a .do file…

Stata Mini Course – Spring 2015

Page 15: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

15

3) Change the working directory (folder):

• Easiest if working in the directory where the data live

• Current directory shown at bottom left

• Short-cut Tip #1:

– Change directory in “File/Change Working Directory”

– Can now see path name in Results window

– Copy that line of code into the .do file you are building

Let’s build a .do file…

Stata Mini Course – Spring 2015

Page 16: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

16

4) Start a .log file

• log using Stata_Lect1.log

Let’s build a .do file…

Stata Mini Course – Spring 2015

Page 17: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

17

Aside: How tell Stata run command from .do file??

1) Choose File/“do”: Will run the WHOLE .do file

2) Choose “execute” button from .do file:

• If nothing highlighted: will run whole .do file

• If lines highlighted: will run JUST those lines

3) Highlight (partial) line:

• If PC: control D

• If mac: control shift D

Let’s build a .do file…

Stata Mini Course – Spring 2015

Page 18: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

18

5) Open a dataset

• use StataClass_Raw

6) Let’s look at the data!

• browse

Let’s build a .do file…

Stata Mini Course – Spring 2015

Page 19: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

19

5) Open a dataset

• use StataClass_Raw

6) Let’s look at the data!

• browse

• describe

• summarize

Let’s build a .do file…

Stata Mini Course – Spring 2015

Page 20: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

20

Figuring out how a command works:

• Link to even MORE help!

• Command syntax: summarize [varlist], …options

• List of options

• Descriptions

• EXAMPLES!

• If don’t know command name:

– “search blah”

– Google it!

Stata help files

Stata Mini Course – Spring 2015

Page 21: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

21

5) Open a dataset

• use StataClass_Raw

6) Let’s look at the data!

• browse

• describe

• summarizesummarize uhrswork, detail

Let’s build a .do file…

Stata Mini Course – Spring 2015

Page 22: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

22

5) Open a dataset

• use StataClass_Raw

6) Let’s look at the data!

• browse

• describe

• summarizesummarize uhrswork, detail

• tabulate nchild if age>=20 & age<=30tabulate nchild, nolabel

Let’s build a .do file…

Stata Mini Course – Spring 2015

Page 23: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

23

7) Build variable “female”:

• tab sex

• generate female = 1 if sex==“female”

• tab sex, nolabel

• generate female = 1 if sex==2

• browse the data (browse sex female)

• notice “missing values” for the men(no values for them if “tab female”)

• replace female = 0 if sex==1(redo “tab female”)

Lets build a variable!

Stata Mini Course – Spring 2015

Page 24: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

24

8) Cut data down to size:

• Drop the men

• Keep if age>=30 & age<=40

9) Save updated dataset:

• Do NOT call by same name as before!

• save StataClass_Lect1

Rerun from the start…

Saving Data

Stata Mini Course – Spring 2015

Page 25: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

25

10) Explore hours worked by # of kids:• sum uhrswork if nchild==0

sum uhrswork if nchild>0

• bysort nchild: sum uhrswork

11)Close the .log file

Rerun from the start…

Exploring Hrs Worked by #Kids

Stata Mini Course – Spring 2015

Page 26: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

26

ASIDE: Capitalization matters!

• Commands: Summarize ≠summarize

• Variable names: Age ≠age

Useful things to know…

Stata Mini Course – Spring 2015

Page 27: STATA Mini Course Fall 2015 Jane Leber Herr jlherr@fas.harvard.edu Littauer 113 1Stata Mini Course – Spring 2015.

27

Useful things you learn:

1) Other ways to build variables(e.g., using tabulate)

2) Labeling variables

3) Other useful commands

4) Lots of other tips that will be invaluable for doing the psets

Other things shown in the “clean” .do file:

Stata Mini Course – Spring 2015