This HTML version of is provided for convenience, but it is not the best format for the book. In particular, some of the symbols are not rendered correctly. You might prefer to read the PDF version, or you can buy a hardcopy here. Chapter 1 Statistical thinking for programmersThis book is about turning data into knowledge. Data is cheap (at least relatively); knowledge is harder to come by. I will present three related pieces:
The thesis of this book is that if you know how to program, you can use that skill to help you understand probability and statistics. These topics are often presented from a mathematical perspective, and that approach works well for some people. But some important ideas in this area are hard to work with mathematically and relatively easy to approach computationally. The rest of this chapter presents a case study motivated by a question I heard when my wife and I were expecting our first child: do first babies tend to arrive late? 1.1 Do first babies arrive late?If you Google this question, you will find plenty of discussion. Some people claim it’s true, others say it’s a myth, and some people say it’s the other way around: first babies come early. In many of these discussions, people provide data to support their claims. I found many examples like these:
Reports like these are called anecdotal evidence because they are based on data that is unpublished and usually personal. In casual conversation, there is nothing wrong with anecdotes, so I don’t mean to pick on the people I quoted. But we might want evidence that is more persuasive and an answer that is more reliable. By those standards, anecdotal evidence usually fails, because:
So how can we do better? 1.2 A statistical approachTo address the limitations of anecdotes, we will use the tools of statistics, which include:
By performing these steps with care to avoid pitfalls, we can reach conclusions that are more justifiable and more likely to be correct. 1.3 The National Survey of Family GrowthSince 1973 the U.S. Centers for Disease Control and Prevention (CDC) have conducted the National Survey of Family Growth (NSFG), which is intended to gather “information on family life, marriage and divorce, pregnancy, infertility, use of contraception, and men’s and women’s health. The survey results are used ... to plan health services and health education programs, and to do statistical studies of families, fertility, and health.”1 We will use data collected by this survey to investigate whether first babies tend to come late, and other questions. In order to use this data effectively, we have to understand the design of the study. The NSFG is a cross-sectional study, which means that it captures a snapshot of a group at a point in time. The most common alternative is a longitudinal study, which observes a group repeatedly over a period of time. The NSFG has been conducted seven times; each deployment is called a cycle. We will be using data from Cycle 6, which was conducted from January 2002 to March 2003. The goal of the survey is to draw conclusions about a population; the target population of the NSFG is people in the United States aged 15-44. The people who participate in a survey are called respondents; a group of respondents is called a cohort. In general, cross-sectional studies are meant to be representative, which means that every member of the target population has an equal chance of participating. Of course that ideal is hard to achieve in practice, but people who conduct surveys come as close as they can. The NSFG is not representative; instead it is deliberately oversampled. The designers of the study recruited three groups—Hispanics, African-Americans and teenagers—at rates higher than their representation in the U.S. population. The reason for oversampling is to make sure that the number of respondents in each of these groups is large enough to draw valid statistical inferences. Of course, the drawback of oversampling is that it is not as easy to draw conclusions about the general population based on statistics from the survey. We will come back to this point later. Exercise 1 Although the NSFG has been conducted seven times, it is not a longitudinal study. Read the Wikipedia pages http://wikipedia.org/wiki/Cross-sectional_study and http://wikipedia.org/wiki/Longitudinal_study to make sure you understand why not. Exercise 2 In this exercise, you will download data from the NSFG; we will use this data throughout the book.
1.4 Tables and recordsThe poet-philosopher Steve Martin once said: “Oeuf” means egg, “chapeau” means hat. It’s like those French have a different word for everything. Like the French, database programmers speak a slightly different language, and since we’re working with a database we need to learn some vocabulary. Each line in the respondents file contains information about one respondent. This information is called a record. The variables that make up a record are called fields. A collection of records is called a table. If you read survey.py you will see class definitions for Record, which is an object that represents a record, and Table, which represents a table. There are two subclasses of Record—Respondent and Pregnancy—which contain records from the respondent and pregnancy tables. For the time being, these classes are empty; in particular, there is no init method to initialize their attributes. Instead we will use Table.MakeRecord to convert a line of text into a Record object. There are also two subclasses of Table: Respondents and Pregnancies. The init method in each class specifies the default name of the data file and the type of record to create. Each Table object has an attribute named records, which is a list of Record objects. For each Table, the GetFields method returns a list of tuples that specify the fields from the record that will be stored as attributes in each Record object. (You might want to read that last sentence twice.) For example, here is Pregnancies.GetFields: def GetFields(self): return [ ('caseid', 1, 12, int), ('prglength', 275, 276, int), ('outcome', 277, 277, int), ('birthord', 278, 279, int), ('finalwgt', 423, 440, float), ] The first tuple says that the field caseid is in columns 1 through 12 and it’s an integer. Each tuple contains the following information:
For pregnancy records, we extract the following variables:
If you read the casebook carefully, you will see that most of these variables are recodes, which means that they are not part of the raw data collected by the survey, but they are calculated using the raw data. For example, prglength for live births is equal to the raw variable wksgest (weeks of gestation) if it is available; otherwise it is estimated using mosgest * 4.33 (months of gestation times the average number of weeks in a month). Recodes are often based on logic that checks the consistency and accuracy of the data. In general it is a good idea to use recodes unless there is a compelling reason to process the raw data yourself. You might also notice that Pregnancies has a method called Recode that does some additional checking and recoding. Exercise 3
In this exercise you will write a program to explore the data
in the Pregnancies table.
You can download a solution to this exercise from http://thinkstats.com/first.py. 1.5 SignificanceIn the previous exercise, you compared the gestation period for first babies and others; if things worked out, you found that first babies are born about 13 hours later, on average. A difference like that is called an apparent effect; that is, there might be something going on, but we are not yet sure. There are several questions we still want to ask:
Answering these questions will take most of the rest of this book. Exercise 4
The best way to learn about statistics is to work on a project you are
interested in. Is there a question like, “Do first babies arrive
late,” that you would like to investigate? Think about questions you find personally interesting, or items of conventional wisdom, or controversial topics, or questions that have political consequences, and see if you can formulate a question that lends itself to statistical inquiry. Look for data to help you address the question. Governments are good sources because data from public research is often freely available2. Another way to find data is Wolfram Alpha, which is a curated collection of good-quality datasets at http://wolframalpha.com. Results from Wolfram Alpha are subject to copyright restrictions; you might want to check the terms before you commit yourself. Google and other search engines can also help you find data, but it can be harder to evaluate the quality of resources on the web. If it seems like someone has answered your question, look closely to see whether the answer is justified. There might be flaws in the data or the analysis that make the conclusion unreliable. In that case you could perform a different analysis of the same data, or look for a better source of data. If you find a published paper that addresses your question, you should be able to get the raw data. Many authors make their data available on the web, but for sensitive data you might have to write to the authors, provide information about how you plan to use the data, or agree to certain terms of use. Be persistent! 1.6 Glossary
|
Like this book?
Are you using one of our books in a class?We'd like to know about it. Please consider filling out this short survey.
|