International Graduate School of Genetic and Molecular Epidemiology (GAME) Computing Notes and Introduction to Stata

Similar documents
Doctoral Program in Epidemiology for Clinicians, April 2001 Computing notes

Introduction to Stata Toy Program #1 Basic Descriptives

I Launching and Exiting Stata. Stata will ask you if you would like to check for updates. Update now or later, your choice.

Stata v 12 Illustration. First Session

Stata version 13. First Session. January I- Launching and Exiting Stata Launching Stata Exiting Stata..

Introduction to Stata First Session. I- Launching and Exiting Stata Launching Stata Exiting Stata..

Introduction to STATA

STATA 13 INTRODUCTION

BIOSTATISTICS LABORATORY PART 1: INTRODUCTION TO DATA ANALYIS WITH STATA: EXPLORING AND SUMMARIZING DATA

Introduction to Stata. Written by Yi-Chi Chen

Stata: A Brief Introduction Biostatistics

After opening Stata for the first time: set scheme s1mono, permanently

Introduction to Minitab 1

Brief Guide on Using SPSS 10.0

You will learn: The structure of the Stata interface How to open files in Stata How to modify variable and value labels How to manipulate variables

An Introduction to Stata Part I: Data Management

Stata version 14 Also works for versions 13 & 12. Lab Session 1 February Preliminary: How to Screen Capture..

Introduction to Stata

Dr. Barbara Morgan Quantitative Methods

STATA Version 9 10/05/2012 1

Stata version 12. Lab Session 1 February Preliminary: How to Screen Capture.. 2. Preliminary: How to Keep a Log of Your Stata Session..

Basics of Stata, Statistics 220 Last modified December 10, 1999.

Intro to Stata for Political Scientists

Econ Stata Tutorial I: Reading, Organizing and Describing Data. Sanjaya DeSilva

Applied Regression Modeling: A Business Approach

Summarising Data. Mark Lunt 09/10/2018. Arthritis Research UK Epidemiology Unit University of Manchester

An Introduction to Stata Exercise 1

Introduction to STATA

Stata versions 12 & 13 Week 4 Practice Problems

A Short Guide to Stata 10 for Windows

Introduction to Stata. Getting Started. This is the simple command syntax in Stata and more conditions can be added as shown in the examples.

TYPES OF VARIABLES, STRUCTURE OF DATASETS, AND BASIC STATA LAYOUT

RUDIMENTS OF STATA. After entering this command the data file WAGE1.DTA is loaded into memory.

A quick introduction to STATA:

Stata versions 12 & 13 Week 4 - Practice Problems

Department of Economics Spring 2016 University of California Economics 154 Professor Martha Olney Stata Lesson Wednesday February 17, 2016

API-202 Empirical Methods II Spring 2004 A SHORT INTRODUCTION TO STATA 8.0

A Quick Guide to Stata 8 for Windows

ECONOMICS 452* -- Stata 12 Tutorial 1. Stata 12 Tutorial 1. TOPIC: Getting Started with Stata: An Introduction or Review

Homework 1 Excel Basics

Using Large Data Sets Workbook Version A (MEI)

1 Introduction. 1.1 What is Statistics?

1. Basic Steps for Data Analysis Data Editor. 2.4.To create a new SPSS file

ECO375 Tutorial 1 Introduction to Stata

Your Name: Section: INTRODUCTION TO STATISTICAL REASONING Computer Lab #4 Scatterplots and Regression

Appendix II: STATA Preliminary

ECONOMICS 351* -- Stata 10 Tutorial 1. Stata 10 Tutorial 1

STAT:5400 Computing in Statistics

OVERVIEW OF WINDOWS IN STATA

Applied Regression Modeling: A Business Approach

Microsoft Excel 2007

Using Microsoft Excel

Also, for all analyses, two other files are produced upon program completion.

Chapter 3: Data Description Calculate Mean, Median, Mode, Range, Variation, Standard Deviation, Quartiles, standard scores; construct Boxplots.

Introduction to gretl

Statistics with a Hemacytometer

BIOSTATS 640 Spring 2018 Introduction to R Data Description. 1. Start of Session. a. Preliminaries... b. Install Packages c. Attach Packages...

Chapter One: Getting Started With IBM SPSS for Windows

BIOL 417: Biostatistics Laboratory #3 Tuesday, February 8, 2011 (snow day February 1) INTRODUCTION TO MYSTAT

An Introduction to Stata Part II: Data Analysis

Lab 1: Introduction, Plotting, Data manipulation

Advanced Regression Analysis Autumn Stata 6.0 For Dummies

QUEEN MARY, UNIVERSITY OF LONDON. Introduction to Statistics

Appendix II: STATA Preliminary

SPSS. (Statistical Packages for the Social Sciences)

ICSSR Data Service. Stata: User Guide. Indian Council of Social Science Research. Indian Social Science Data Repository

Introduction to StatsDirect, 15/03/2017 1

STATA Tutorial. Introduction to Econometrics. by James H. Stock and Mark W. Watson. to Accompany

Getting Our Feet Wet with Stata SESSION TWO Fall, 2018

How to use Excel Spreadsheets for Graphing

Lab #1: Introduction to Basic SAS Operations

A quick introduction to STATA:

Introduction (SPSS) Opening SPSS Start All Programs SPSS Inc SPSS 21. SPSS Menus

Depending on the computer you find yourself in front of, here s what you ll need to do to open SPSS.

1 Introduction to Using Excel Spreadsheets

2 The Stata user interface

Intro To Excel Spreadsheet for use in Introductory Sciences

If you use Stata for Windows, starting Stata is straightforward. You just have to double-click on the wstata (or stata) icon.

Excel tutorial Introduction

MINITAB 17 BASICS REFERENCE GUIDE

Department of Economics Spring 2018 University of California Economics 154 Professor Martha Olney Stata Lesson Thursday February 15, 2018

StatLab Workshops 2008

AcaStat User Manual. Version 8.3 for Mac and Windows. Copyright 2014, AcaStat Software. All rights Reserved.

EXCEL BASICS: MICROSOFT OFFICE 2007

Chapter 2 Assignment (due Thursday, April 19)

WHO STEPS Surveillance Support Materials. STEPS Epi Info Training Guide

GETTING STARTED WITH MINITAB INTRODUCTION TO MINITAB STATISTICAL SOFTWARE

2. Getting started with MLwiN

Introduction to Microsoft Excel

Data Management Project Using Software to Carry Out Data Analysis Tasks

Minitab 17 commands Prepared by Jeffrey S. Simonoff

Preparing for Data Analysis

LAB 1 INSTRUCTIONS DESCRIBING AND DISPLAYING DATA

Excel R Tips. is used for multiplication. + is used for addition. is used for subtraction. / is used for division

Survey of Math: Excel Spreadsheet Guide (for Excel 2016) Page 1 of 9

SAS Training Spring 2006

Statistical Analysis Using SPSS for Windows Getting Started (Ver. 2018/10/30) The numbers of figures in the SPSS_screenshot.pptx are shown in red.

User Services Spring 2008 OBJECTIVES Introduction Getting Help Instructors

SPSS QM II. SPSS Manual Quantitative methods II (7.5hp) SHORT INSTRUCTIONS BE CAREFUL

Quick Start Guide Jacob Stolk PhD Simone Stolk MPH November 2018

Transcription:

International Graduate School of Genetic and Molecular Epidemiology (GAME) Computing Notes and Introduction to Stata Paul Dickman September 2003 1 A brief introduction to Stata Starting the Stata program We assume you have a computer in front of you and that you know how to open an application in Windows. To start Stata: 1. Click on Start 2. Click on Programs 3. Click on Stata 4. Select Intercooled Stata 8 It is also possible to start Stata by double-clicking on the Stata icon on your desktop, or clicking on a Stata data set. If everything has gone smoothly so far, then you should be able to see the Stata default windows. 1. Review 2. Variables 3. Results 4. Command 5. Stata Toolbar based, in part, on notes written by David Clayton and Michael Hills 1

Tutorials The command tutorial starts a somewhat interactive series of tutorials to learn some of the Stata facilities in specific areas. Further details are available on page 12 of this handout. To run the tutorial intro type. tutorial intro Useful Stata links Resources for learning Stata can be found at http://www.stata.com/links/resources1.html Getting help Click on Help, or type help followed by a command name, for on-line help. whelp. Try also Closing the program Choose exit from the file menu, click the Windows close box (the x in the top right corner), or type exit at the command line. You will have to type clear first if you have any data in memory (or simply type exit, clear). Note that Stata is case sensitive. To interrupt a Stata command, click on break or press ctrl break. Types of Stata files Data files in Stata format are given the extension.dta. These are created using save filename and read in with use filename. There are four other types of input file:.raw for raw data,.dct for data plus variable names,.do for batch files containing Stata commands,.ado for Stata programs, and.log for log files. Syntax command varnames if... in... using..., options The if part restricts the command to records satisfying certain logical conditions (eg sex==1), the in part restricts the command to certain line numbers, and the using part specifies any files which may be needed. Abbreviations Stata accepts unambiguous abbreviations for commands and variable names. 2

2 A hands-on introduction to Stata To introduce you to Stata we use the IVF data which consists of 641 records on mothers who had singleton births following in-vitro fertilisation. The variables in the dataset are shown in Table 1. Variable Units or Coding Type Name Subject number categorical id Maternal age years metric matage Hypertension 1=hypertensive, 0=normal binary hyp Gestational age weeks metric gestwks Sex of infant 1=male, 2=female binary sex Birthweight grams metric bweight Table 1: Variables in the IVF dataset Type in the commands which start with the Stata prompt (. ). Do not type the. prompt this is used to indicate a Stata command. Stata distinguishes between upper and lower case letters, and accepts abbreviations for both commands and variable names. Think carefully about what is happening after each command. The file ivf.dta contains the variables names and values for the 641 records and can be accessed over the world wide web from within Stata. To read the data, type. use http://www.pauldickman.com/teaching/game/ivf. describe Now type the following. Describe Stata will return an error message (unrecognised command: Describe). Stata is case sensitive; describe is a valid Stata command, whereas Describe is not. A good way to start the analysis is to ask for a summary of the data by typing. summarize This will produce the mean, standard deviation, and range, for each variable in turn. In most datasets there will be some missing values. These are coded using the symbol. in place of the value which is missing. Stata can recognize other codes for missing values, but this is the one which is recommended. The summarize command is useful for seeing whether there are missing values (the column labelled Obs gives the number of non-missing observations). 3

For a more detailed summary of the variable gestwks try. codebook gestwks or. summarize gestwks, detail Many Stata commands can be accessed using menus. For example, from the Summaries menu, select Median/Percentiles. You will notice that the result is identical to that obtained from the command typed previously (summarize gestwks, detail) and that Stata even shows the command which was used. The list command is used to list the values in the data file. Try out the following and see their consequences:. list in 1/5. list matage in 1/10. list matage. list matage bweight in 1/20 Stata stops after each screenfull of output. Click on more (or hit the spacebar) to get another screenfull, or press enter to continue line by line. The command list on its own would list all of the data. You can cancel this command (and any other Stata command) by clicking on Break (the icon in the toolbar which looks like a red circle with a white cross through it). Stata also contains a spreadsheet-style editor which can be brought to the front by typing. edit Close this window by clicking in the close box (in the top right corner of the window). The browse command will bring up a similar window, except changes cannot be made to the data. The data window can also be opened using icons on the toolbar (the two icons look like spreadsheets, with a magnifying glass over the data browser icon) or from the Data menu. When starting to look at any new data the first step is to check that the values of the variables make sense and correspond to the codes defined in the coding schedule. For categorical variables this can be done by looking at one-way frequency tables and checking that only the specified codes occur. For metric variables we need to look at ranges. This first look at the data will also indicate whether all values are present or whether there are some missing values on some variables. Let us begin by looking at the categorical variables. The distribution of the categorical variables hyp and sex can be viewed by typing. tabulate hyp. tab sex 4

To treat missing values as a separate category, the missing option can be used. tabulate hyp, missing Note that tab is an abbreviation for tabulate. The cross-tabulation of hyp and sex is obtained by typing. tab hyp sex Cross tabulations are useful when checking for consistency. The basic output from a cross tabulation reports frequencies only; to include row and/or column percentages add the options row, col, cell, or any combination, as in. tab hyp sex, col missing The command table is used for preparing tables of summary statistics by one, two, or even more categorical variables. For example, to obtain the means and standard deviations of bweight separately by sex, type. table sex, contents(freq mean bweight sd bweight) To make a table of the median and interquartile range for birthweight, by sex, try. table sex, contents(freq med bweight iqr bweight) Note that tab is an abbreviation for tabulate, NOT for table, which must be typed in full. You can type whelp tabulate and whelp table to understand how, if, you can abbreviate the command. 3 Restricting commands Stata commands can be restricted to records 1, 2,..., 10 (for example), by adding in 1/10 to the command. The letters f and l can be used as abbreviations for first and last, so 20/l refers to the records from 20 onwards. Commands can also be restricted to operate only on records which satisfy given conditions. The conditions are added to the command using if followed by a logical expression which takes the values true or false. For example, to restrict the command list to records with birthweight less than or equal to 2000g, type. list id bweight if bweight <= 2000 The record is listed only if the logical expression bweight <= 2000 is true. A useful command when exploring data is count which counts the number of records which satisfy some logical expression. For example. count if bweight <= 2000. count if bweight <= 2000 & sex==1 5

Note the use of & to link two conditions both of which must be satisfied and that a double equal sign (==) is used for equality testing. A common error is to use = in a logical expression instead of ==. The following comparison operators and logical functions are available: Arithmetic Logical Comparison ------------------- ------------------ ------------------- + addition ~ not > greater than - subtraction or < less than * multiplication & and >= > or equal / division <= < or equal ^ power == equal ~= not equal 4 Generating and recoding variables New variables are generated using the command generate, and variables can be recoded using recode. For example, to create a new variable sex2 which is the same as sex but coded 1 for male and 0 for female, try. gen sex2=sex. recode sex2 2=0. tab sex2 5 Sorting The records in a dataset can be sorted according to the values of one or more variables. The births dataset is currently sorted by id but for some purposes it might be better to have it sorted by bweight. Try. list id bweight in 1/10. sort bweight. list id bweight in 1/10 The records are now in order of bweight and the id numbers and all other variables have also been sorted in this order. Stata commands which use the option by() usually require the data to be first sorted by the variable in the by() option. The sort is not done automatically because you should always be aware of how your data are sorted. 6

6 Editing commands The PageUp and PageDown keys (represented as arrows on the top right of the keypad) can be used to cycle through previous commands, which can then be edited. For example, if you decide that you would also like to list the values of the variable matage you could use the PageUp key to recall the previous command and then edit it in the command line to be:. list id bweight matage in 1/10 This capability is especially useful if you make a small mistake while typing a command. The command can be recalled, edited, and resubmitted. It also makes it easy to resubmit the same command with additional options. 7 Using Stata as a calculator The display command can be used to carry out simple calculations. For example, the command. display 2+2 will display the answer 4, while. display log(10) will display the answer 2.3026. Note that log means natural log in Stata. To obtain base 10 logarithms use the log10 function. For example,. display log10(1000) will return the value 3. Standard probability functions can also be displayed, as in. display normprob(1.96) which will return the probability that a random variable with a standard normal distribution (i.e. mean 0 and variance 1) is less that 1.96. 7

8 Graphical displays The Stata graphics procedures were completely rewritten for version 8 and are now quite powerful. Following are just a few simple examples. To obtain a histogram of bweight, type the following. It may take a few seconds for the graph to be displayed.. hist bweight, freq You can vary the number of rectangles in the histogram (called bins) by adding bin(20), etc. To superimpose the histogram with a normal curve which has the same mean and standard deviation as the data, add the option normal. Try, for example,. hist bweight, freq bin(20) normal You can also produce this plot via the Graphics / Easy graphs / Histogram menu. This provides a useful way of exploring the various options for the hist command. Note that you can save time by using the PageUp to recall the previous command, to which you then can add the additional options. We can also produce separate graphs for each level of a categorical variable by using a by() command. Note that we must first sort the data when using a by() command.. sort hyp. hist gestwks, by(hyp) Scatter plots can be used to evaluate the association between, for example, the metric variables bweight and matage by typing. scatter bweight matage To plot bweight against gestwks, try. scatter bweight gestwks 9 Missing values The missing value symbol in Stata is. and is treated as plus infinity in logical comparisons. Stata commands automatically exclude missing values when they are coded in this way. 8

10 Icons on the Stata toolbar Some file operations are easier to perform by clicking on the appropriate icon on the toolbar rather than typing commands at the command line. The most commonly used icons are the first four from the left: Open a Stata data file Save a Stata data file Print (graph or log file (if a log file is open)). Log file operations (Open, Close, Suspend, Resume). 11 Saving data files The Stata data currently in memory can be saved in a file by clicking on the Save icon (the floppy disk) on the toolbar. You will need to type in a name for your file which, by default, will be saved in the default directory with the extension.dta. 12 Logging and printing results Graphs can be printed directly by selecting Print graph from the File menu, or you can copy it and past it into any of your word processor (for instance MS Word). Other output must first be written to a log file before it can be printed. A log file can be opened by clicking on the log icon on the toolbar (the fourth icon from the left. You will need to type in a name for your file which, by default, will be saved in your personal directory with the extension.log. 13 Using the menus Most Stata commands can be accessed from the menus. Experiment with some of the commands in the Data, Graphics and Statistics menus. For example, select Graphics / Easy Graphs / Scatterplot and then select bweight as the Y axis variable and gestwks as the X axis variable and click OK. The resulting graph is the same as if you typed the command. scatter bweight gestwks 9

14 Some practice with basic commands Remember to make use of the help command during these exercises. You are encouraged to explore and use the menus. 1. List the variables bweight and hyp for records 20 25 inclusive. 2. Obtain the frequency distribution of matage together with its histogram. 3. Obtain the two way table of frequencies of sex and hyp, first with row, then column, then cell percentages. Is there evidence of an association between the two variables? Do you think it s statistically significant? [Note that you are not expected to perform a formal statistical significance test, just give your impression.] 4. Calculate the mean birthweight for hypertensive and non-hypertensive mothers. Is there evidence of an association? Do you think it s statistically significant? [Note that you are not expected to perform a formal statistical significance test, just give your impression.] 5. The mean birthweight of babies to hypertensive mothers is considerably lower than the mean birthweight of babies to non-hypertensive mothers. It turns out that this difference is highly statistically significant (based on a t-test, which you will learn later during the course). Do you believe that the association is causal (i.e. that hypertension causes babies to be smaller)? 6. It is possible that the association between hypertension and birthweight is confounded by gestational age (gstwks). If so, gestational age should be associated with both the exposure (hypertension) and the outcome (birthweight). Study appropriate tables or graphs to determine if such associations exist. 7. Imagine we wish to classify babies weighing less that 2500 g as being low birth weight. Create a dichotomous variable, lbw which takes the value 1 for babies of low birth weight and 0 otherwise. 8. Produce a table showing the proportion of low birth weight babies of each sex. 9. Produce a histogram of birthweights (use at least 20 bins). Does the distribution appear to be symmetric? 10. Now produce histograms of birthweights for each level of hyp. Do the distributions appear to be symmetric? 11. Produce a scatterplot of maternal age against patient ID. Is there evidence of an association between these variables? 12. Formal statistical tests suggest that there is a statistically significant inverse (or negative) association between maternal age against patient ID. How might such an association arise and what are the possible consequences for the analysis of these data? 10

Some useful commands A, B are categorical variables. X, Y are metric variables. Data Management use Read in a data set already in Stata format infile using Read in data in a txt file with names describe (or f3) Describe contents of data in memory list List values of variables drop A Drops the variable called A drop if... Drops all records satisfying... generate A = Creates a new variable called A replace A = Replaces contents of A recode A Recodes the variable called A save filename Save data set in Stata format sort A Sort records according to the variable A count if... Count number of observations satisfying... Statistics and Graphics summarize Y tabulate A tabulate A B table A, c(mean X) graph Y, hist graph Y X, scatter hist A regress Y X predict P Display summary statistics for Y One-way table of frequencies for A (categorical) Two-way table of frequencies for A and B Table of mean X by levels of A Displays histogram of Y Displays scatter plot of Y vs X Histogram of the categorical variable A Linear regression of Y on X Obtain prediction after regress and put in P Utilities clear Clear data from memory display 2+2 Display the result of 2+2 do filename Execute commands from filename.do exit Exit Stata exit, clear Clear and exit Stata help Obtain on-line help for both data and commands log using filename Write output to filename.log 11

The Stata tutorials The official Stata package provides the following tutorials Tutorial intro graphics tables regress anova logit survival factor ourdata yourdata Description An introduction to Stata How to make graphs How to make tables Estimating regression models, including 2SLS Estimating one-, two- and N-way ANOVA and ANOCOVA models Estimating maximum-likelihood logit and probit models Estimating maximum-likelihood survival models Estimating factor and principal component models Description of the data we provide How to input your own data into Stata Useful ones to try are intro, graphics, and yourdata. To run the tutorial intro type. tutorial intro 12