# lab1

## Table of Contents

## 1 Week 1 Lab

### 1.1 Starsign

Does your star sign have an effect on whether you believe astrology is scientific??

Huh. 5.1% of Capricorns (Michelle O) say astrology is very scientific, compared with 8.7% of Pisces (Chuck Norris) pic.twitter.com/HobaFtqoWH
— Philip N. Cohen (@familyunequal) January 27, 2014

### 1.2 Measures of association

We have discussed the following measures of association, defined for 2*2 tables:

- Difference in proportions
- Relative rate or ratio of proportions
- Odds ratio

Calculate each of these measures for the following table:

Outcome | |||
---|---|---|---|

No | Yes | Total | |

Type 1 | 380 | 120 | 500 |

Type 2 | 420 | 80 | 500 |

Total | 800 | 200 | 1000 |

#### 1.2.1 With a spreadsheet…

This spreadsheet will allow you to explore these measures a bit more. You can change the numbers in the "Type 1/Outcome Yes" and "Total/Outcome Yes" cells and the three measures are calculated for you. Use it to check what you have calculated by hand.

For "Total/Outcome Yes" values of 100, 200, 400, 800 and 900, do the following:

- Find what values of "Type 1/Yes" give you a difference in

proportions of 0.2 for each of these totals

- Find what values of "Type 1/Yes" give you a relative rate of 2.0

(or as close as possible) for each total

- Find what values of "Type 1/Yes" give you odds ratio of 2.0

(or as close as possible) for each total

Keep a note of your numbers and compare their patterns. What do you notice?

### 1.3 Putting tables into Stata

We can enter tables into Stata quite easily using the following strategy. Given a table that looks like this:

Agree | Disagree | Total | |

Male | 122 | 223 | 345 |

Female | 268 | 1632 | 1900 |

we can put it into Stata like this:

input gender att count 1 1 122 1 2 223 2 1 268 2 2 1632 end label define gndr 1 "Male" 2 "Female" label values gender gndr label define agree 1 "Agree" 2 "Disagree" label values att agree tab gender att [freq=count]

Run this syntax, and run a χ^{2} test. Do `help tab`

if you need a hint for the χ^{2} test.

### 1.4 Adding capabilities to Stata

Lots of people write additional procedures for Stata, and many of
these are easily available. See `help net`

and ```
help
ssc
```

for an overview. We are going to use one such add-on,
`TAB_CHI`

, today. Do `net search tab_chi`

first,
just to see how to search. One place it is found is the Stata web site,
and another is the Repec economics article archive. Either of the two
following commands should install it:

net install tab_chi

or

ssc install tab_chi

The former looks on the Stata website, the latter on the Statistical Software Components archive.

### 1.5 Analysing a real table

This is a table relating social class of origin and highest educational qualification:

| qual class | Univ 2nd level Incomplet | Total -------------------+---------------------------------+---------- Prof/Man | 1025 1566 767 | 3358 Routine non-manual | 124 687 713 | 1524 Skilled manual | 31 483 464 | 978 Semi/unskilled | 18 361 716 | 1095 -------------------+---------------------------------+---------- Total | 1198 3097 2660 | 6955 Source: British Household Panel Survey 2001

By hand, calculate the odds ratio comparing prof/man versus semi/unskilled in their chances of having a university education (university versus anything else). Interpret it.

Do the same for routine-non-manual versus semi/unskilled and skilled versus semi/unskilled. Is there a pattern in the three ORs?

Enter the table in Stata, and use `tab`

and
`tabchi`

to do the following

- Analyse the pattern of percentages (
`tab a b, row`

) - Analyse the pattern of expected values and raw residuals (observed minus expected,
`tabchi a b, raw`

) - Analyse the adjusted residuals (
`tabchi a b, adj`

) - Run the χ
^{2}test and interpret

### 1.6 Using Stata "reproducibly"

Working from do-files makes Stata easier to manage for more complex tasks. A good practice is to have a single do-file for each main task, so that

- You have a document that explains readably, step by step, what you have done
- You have a file that can re-do the task (e.g., with modifications,

or with different data.

The goal would be to have a do-file that takes you from a stable data set, to a table, statistical analysis or graph (of set thereof) that is going to end up in a research report. This can be extremely useful when you come back to work you have done earlier, allowing you to verify and reproduce it.

#### 1.6.1 Where are you?

Stata usually starts up with your "Documents and Settings" folder
current. Enter the command `pwd`

to verify. It is a good idea
to have a separate sub-folder (e.g. "Documents and Settings\SO5032") to
put your work in. To move to this do (assuming it exists):

cd SO5032

Any files Stata looks for or writes, will be in this folder unless you tell it otherwise.

#### 1.6.2 Building up a do-file interactively

Generally you will use trial and error to find out how to do a particular task, but the resulting do-file should just contain the necessary steps. A good is to open a do-file in the do-file editor (pencil/paper icon on toolbar) and run commands from there, and from the command line, tidying them up as you go along. Once you have generated the output you want, check that you can reproduce it by running the entire file.

To run a do-file you have multiple options: using the buttons in the
do-file editor is one, but you can also call it from the command line.
If your file is `table1.do`

, you do `do table1`

at
the command line. If you want to run a do-file without seeing output
(e.g. if it is mostly data manipulation) you can do `run table1`

.

Note that lines starting with "*" or "//" are comments, and are ignored by Stata. This is helpful for adding notes to explain what you are doing.

#### 1.6.3 Logging

Stata can log the textual output (effectively the contents of the
"Results" window) to a file. To begin: `log using`

*filename* (where
*filename* is a proper file specification), to stop, `log close`

.

Graphs don't appear in the "Results" window, but in their own
windows. Once you create a graph, you can save it to a file, so that Stata can re-read it
(do e.g. `graph save graph1`

, and later re-access it with
`graph use graph1`

), or you can export it to a large number
of formats that you can use in other programs (do `graph export graph1.png`

, for instance). See `help graph export`

to
find out what is possible.