Introduction to Stata and Hypothesis testing · Introduction to Stata and Hypothesis ... Notice the...

16
1 Introduction to Stata and Hypothesis testing. The goals today are simple – let’s open Stata, understand basically how it works, understand what a dofile is, and then run some basic hypothesis tests for testing a mean and testing differences in means. 1. Finding Stata on the network and opening it. The first step is to open Stata. Open the folder for My computer and find the X drive: Doubleclick on the Xdrive, find the folder for Stata and then double click on Stata.exe .

Transcript of Introduction to Stata and Hypothesis testing · Introduction to Stata and Hypothesis ... Notice the...

Page 1: Introduction to Stata and Hypothesis testing · Introduction to Stata and Hypothesis ... Notice the list of variables in the Variables window and the ... Introduction to Stata and

 

Introduction to Stata and Hypothesis testing. 

The goals today are simple – let’s open Stata, understand basically how it works, understand what a do‐file is, and then run some basic hypothesis tests for testing a mean and testing differences in means. 

 

1. Finding Stata on the network and opening it. 

The first step is to open Stata.  Open the folder for My computer and find the X drive: 

 

 

Double‐click on the X‐drive, find the folder for Stata    and then double click 

on Stata.exe . 

 

 

 

Page 2: Introduction to Stata and Hypothesis testing · Introduction to Stata and Hypothesis ... Notice the list of variables in the Variables window and the ... Introduction to Stata and

 

2. Once Stata is open. 

When you first start Stata, you should see something like this: 

 

Results window – prints to the screen any results that Stata produces 

Command: where you can type commands, tell stata what to do 

Variables: will list all variables that are in a loaded data set. 

Review: will list all commands that you have already run. 

Page 3: Introduction to Stata and Hypothesis testing · Introduction to Stata and Hypothesis ... Notice the list of variables in the Variables window and the ... Introduction to Stata and

 

3. A simple command and the output 

We can use Stata as a fancy calculator.  In the command window, type  

display 4+5 

Now looks at the results window and the review window. 

 

You see the command and the result (9) in the results window. 

 

Also try   display “My name is Tammy.  My office is P”  3050+6 

Page 4: Introduction to Stata and Hypothesis testing · Introduction to Stata and Hypothesis ... Notice the list of variables in the Variables window and the ... Introduction to Stata and

 

4. Opening a data set in Stata. 

I have prepared the data set slid_IP602.dta in advance.   You should have downloaded this from the course website and saved it to your USB drive.  On my computer, this was saved in the folder G:\IP602_lab  

In Stata, find the Open icon and click on it 

 

Find your data set and open it 

 

You should now see something like this: 

Page 5: Introduction to Stata and Hypothesis testing · Introduction to Stata and Hypothesis ... Notice the list of variables in the Variables window and the ... Introduction to Stata and

 

 

Although you just clicked on everything, Stata entered the command for you “use….” to load the data set.  Notice the list of variables in the Variables window and the command in the review window. 

You can look at the data set in a spreadsheet by clicking on the data browser icon (look for the magnifying glass over a spreadsheet).  You should then see this: 

Page 6: Introduction to Stata and Hypothesis testing · Introduction to Stata and Hypothesis ... Notice the list of variables in the Variables window and the ... Introduction to Stata and

 

 

Each column is a variable and each row is one observation – very similar to what we had in Excel.  If you scrolled down the data editor (arrows on the side) you can see that we have 144749 observations and scrolling across you can see more variables in the order that they were listed in the Variables window.  When a cell has a period (.) in it, we are missing information for that observation. 

Close the data editor window.  

In the command line type 

  summarize 

Page 7: Introduction to Stata and Hypothesis testing · Introduction to Stata and Hypothesis ... Notice the list of variables in the Variables window and the ... Introduction to Stata and

 

 

This will provide you with basic summary statistics including a mean and standard deviation for each variable, and how many observations have valid information for each variable. 

To give you a chance to view all results, Stata might pause and show “more” at the bottom of the results.   If you hit enter or click on more it will continue. 

 

Next, we will modify the command to get more specific results. 

Page 8: Introduction to Stata and Hypothesis testing · Introduction to Stata and Hypothesis ... Notice the list of variables in the Variables window and the ... Introduction to Stata and

 

Suppose we only care about wages: 

 

Recall that Statistics Canada often uses a complex survey design (not simple random sampling).  We should really use weights to get the mean: 

 

Suppose we only wanted to know the mean wage for a sub‐sample of younger men: 

 

(in this data set, sex==1 for men and sex==2 for women). 

Suppose we needed some percentiles: 

 

 

Page 9: Introduction to Stata and Hypothesis testing · Introduction to Stata and Hypothesis ... Notice the list of variables in the Variables window and the ... Introduction to Stata and

 

Summarize is really only useful for continuous variables.  For categorical variables, we can get the proportion of observations in each category by using the command tabulate: 

 

Again, we might want to use weights and a subsample – the added syntax is very similar across Stata commands 

 

What is province 10?  See the information in the document about data at the library and getting these codes (also posted on the course website). 

Before moving to the next section, type clear in the command window – we want to start fresh. 

Page 10: Introduction to Stata and Hypothesis testing · Introduction to Stata and Hypothesis ... Notice the list of variables in the Variables window and the ... Introduction to Stata and

10 

 

5. Using do‐files 

You could accomplish a lot of work by typing everything in the command line and then copying and pasting your work.  Unfortunately it would be a lot of work to reproduce your results from scratch and chances are when you are working on a project you will find some typo early on and have to repeat everything.  Or, you seem to have a mistake in your results, but can’t recall every step you took along the way.  You end up having to start over.  The simple solution to this problem is to create a do‐file. 

A do‐file is a text file in which you write all your commands, from start to finish, and then tell Stata to run all the commands in the do‐file at once.   Learning the syntax is a bit tricky, but will make your life easier in the long run. 

With Stata open, find the icon for starting a do‐file and click on it: 

 

You should see a new window pop up: 

 

Page 11: Introduction to Stata and Hypothesis testing · Introduction to Stata and Hypothesis ... Notice the list of variables in the Variables window and the ... Introduction to Stata and

11 

 

You can type all your commands in this window.  Try the following: 

 

To run the commands, find the “execute” icon (paper with lines and arrow).  Click on this and then look at the Stata results window: 

 

Page 12: Introduction to Stata and Hypothesis testing · Introduction to Stata and Hypothesis ... Notice the list of variables in the Variables window and the ... Introduction to Stata and

12 

 

To keep the commands for later use, you need to save the do‐file.  In the do‐file window, you would save the file as usual. Save it in the same place as your data set 

 

You should allow it to keep the default extension (.do). I have named my file IP602lab1.do. 

You can then open the do‐file later on any computer, by coming back to the do‐file editor.  You can also open it in programs like Notepad or Wordpad. 

6. Keeping a log file 

Rather than constantly copying and pasting output from the results window, it is easiest to keep a log (output) file.  Tell stata to store the output in the file myoutput.txt by adding the lines to your do‐file. 

 

Page 13: Introduction to Stata and Hypothesis testing · Introduction to Stata and Hypothesis ... Notice the list of variables in the Variables window and the ... Introduction to Stata and

13 

 

Anything written between the lines log using and log close will be stored in myoutput.txt. 

Execute your do‐file again.  Now go to your folder (G:\IP602_lab) and find the file myoutput.txt.  Open it. 

 

This file would be updated and replaced each time you run the do‐file. 

 

7. Errors  in syntax. 

If you make a typo, you will get errors on the results screen.  For example,  

 

 

Stata provides a brief explanation to help you find your mistake.  Just fix it in the do‐file and re‐run things.  Notice Stata is Case sensitive. 

If you made a mistake before getting to the log close command, you might get: 

 

Page 14: Introduction to Stata and Hypothesis testing · Introduction to Stata and Hypothesis ... Notice the list of variables in the Variables window and the ... Introduction to Stata and

14 

 

Just type log close in the command line and try executing the do‐file again. 

8. Hypothesis tests in Stata 

In Stata’s command line, type help ttest.  A help window should pop up.  (you can find most Stata commands this way, or use the drop‐down menus). 

Here, we can find the syntax for two types of tests we want to do: testing a mean and testing differences in means.  In this help file you can find out more about Stata’s defaults. 

a. Testing a claim about a mean 

In the do‐file, add the line ttest wage==20 . This will test the null hypothesis that wages are $20 per hour.  Execute the do‐file and look at the results. 

 

 

Page 15: Introduction to Stata and Hypothesis testing · Introduction to Stata and Hypothesis ... Notice the list of variables in the Variables window and the ... Introduction to Stata and

15 

 

 

Stata will automatically run 3 tests for you.  Note: != is not equal in Stata’s language. 

We do not reject the null in the first test,  and reject the null in the second and third set of tests.  Consider the relevant critical value in each test.   

Unfortunately, we cannot build use of weights into our means here – Stata just won’t let us (for very good reasons that you would pay attention to if you did a full stats course). 

 

Suppose we are only interested in a subset of the population and we want to change the significance level to 10% instead of 5%: 

 

This time we reject the null in the first and second test, not the third.  Look to your table for the t‐distribution (notes, Figure 3.3) for the appropriate critical values of ‐1.282, 1.645 and 1.282. 

 

b. Testing for differences in means 

Suppose we are asked to test whether men and women have the same wages.  From the help ttest page, we could find 

Page 16: Introduction to Stata and Hypothesis testing · Introduction to Stata and Hypothesis ... Notice the list of variables in the Variables window and the ... Introduction to Stata and

16 

 

 

The varname is wage, the groupvar is sex.   We also need the option unequal to tell stata we are not going to assume that men’s and women’s wages have the same variance. 

 

Notice how Stata will set up the null hypothesis and the resulting t‐statistic.  Notice you could compute this by hand from the information in the table. 

We would reject the hypothesis that the difference between men’s and women’s wages is zero when the alternative is that the difference (men’s – women’s) is not zero or greater than zero.  We do not reject the null when the alternative is that the difference is less than zero (indicating men earn less than women). 

Again, we can be more specific in our tests, comparing only younger men to younger women: