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

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

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.

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

Chapter 1. Looking at Data-Distribution

Stata v 12 Illustration. First Session

Exercise 1: Introduction to Stata

Principles of Biostatistics and Data Analysis PHP 2510 Lab2

Introduction to Stata

Recoding and Labeling Variables

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

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

To complete the computer assignments, you ll use the EViews software installed on the lab PCs in WMC 2502 and WMC 2506.

LAB 1 INSTRUCTIONS DESCRIBING AND DISPLAYING DATA

Acquisition Description Exploration Examination Understanding what data is collected. Characterizing properties of data.

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

Introduction to Minitab 1

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

2.1 Objectives. Math Chapter 2. Chapter 2. Variable. Categorical Variable EXPLORING DATA WITH GRAPHS AND NUMERICAL SUMMARIES

STATA 13 INTRODUCTION

Chapter 6: DESCRIPTIVE STATISTICS

AND NUMERICAL SUMMARIES. Chapter 2

Chapter 2 Modeling Distributions of Data

Introduction to Stata - Session 2

Dr. Barbara Morgan Quantitative Methods

STP 226 ELEMENTARY STATISTICS NOTES PART 2 - DESCRIPTIVE STATISTICS CHAPTER 3 DESCRIPTIVE MEASURES

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

Introduction to Stata: An In-class Tutorial

Stata: A Brief Introduction Biostatistics

Math 120 Introduction to Statistics Mr. Toner s Lecture Notes 3.1 Measures of Central Tendency

Things you ll know (or know better to watch out for!) when you leave in December: 1. What you can and cannot infer from graphs.

BIO 360: Vertebrate Physiology Lab 9: Graphing in Excel. Lab 9: Graphing: how, why, when, and what does it mean? Due 3/26

Univariate Statistics Summary

For many people, learning any new computer software can be an anxietyproducing

PubHlth 640 Intermediate Biostatistics Unit 2 - Regression and Correlation. Simple Linear Regression Software: Stata v 10.1

LABORATORY 1 Data Analysis & Graphing in Excel

Homework 1 Excel Basics

Chapter 2. Descriptive Statistics: Organizing, Displaying and Summarizing Data

CHAPTER 6. The Normal Probability Distribution

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

Empirical Asset Pricing

Exploratory data analysis with one and two variables

The main issue is that the mean and standard deviations are not accurate and should not be used in the analysis. Then what statistics should we use?

No. of blue jelly beans No. of bags

3 Graphical Displays of Data

AP Statistics. Study Guide

CHAPTER 3: Data Description

Week 2: Frequency distributions

Lecture Series on Statistics -HSTC. Frequency Graphs " Dr. Bijaya Bhusan Nanda, Ph. D. (Stat.)

DEPARTMENT OF HEALTH AND HUMAN SCIENCES HS900 RESEARCH METHODS

Frequency Distributions

Exploring and Understanding Data Using R.

Chapter 3 Analyzing Normal Quantitative Data

NCSS Statistical Software

STA 570 Spring Lecture 5 Tuesday, Feb 1

Averages and Variation

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

4. Descriptive Statistics: Measures of Variability and Central Tendency

Chapter 5: The standard deviation as a ruler and the normal model p131

Name Date Types of Graphs and Creating Graphs Notes

STAT:5400 Computing in Statistics

STA Module 4 The Normal Distribution

STA /25/12. Module 4 The Normal Distribution. Learning Objectives. Let s Look at Some Examples of Normal Curves

Chapter 6 The Standard Deviation as Ruler and the Normal Model

Introduction to STATA 6.0 ECONOMICS 626

Density Curve (p52) Density curve is a curve that - is always on or above the horizontal axis.

Statistics Lecture 6. Looking at data one variable

Chapter 3 - Displaying and Summarizing Quantitative Data

Chapter 2: The Normal Distribution

Stat 528 (Autumn 2008) Density Curves and the Normal Distribution. Measures of center and spread. Features of the normal distribution

CHAPTER 2: SAMPLING AND DATA

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

The first few questions on this worksheet will deal with measures of central tendency. These data types tell us where the center of the data set lies.

Barchard Introduction to SPSS Marks

Further Maths Notes. Common Mistakes. Read the bold words in the exam! Always check data entry. Write equations in terms of variables

1 Introduction to Using Excel Spreadsheets

Results Based Financing for Health Impact Evaluation Workshop Tunis, Tunisia October Stata 2. Willa Friedman

An Introduction to Stata Part I: Data Management

Chapter 3 Understanding and Comparing Distributions

CHAPTER 1. Introduction. Statistics: Statistics is the science of collecting, organizing, analyzing, presenting and interpreting data.

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

Name: Date: Period: Chapter 2. Section 1: Describing Location in a Distribution

An Introduction to STATA ECON 330 Econometrics Prof. Lemke

Assignment 3 due Thursday Oct. 11

GEOGRAPHY 426 LAB 4: Choropleth Maps

Topic (3) SUMMARIZING DATA - TABLES AND GRAPHICS

AP Statistics Prerequisite Packet

Introduction to STATA

Introduction to SPSS

Measures of Central Tendency. A measure of central tendency is a value used to represent the typical or average value in a data set.

Course contents. Overview: Goodbye, calculator. Lesson 1: Get started. Lesson 2: Use cell references. Lesson 3: Simplify formulas by using functions

Exploratory Data Analysis

Measures of Dispersion

Chapter 2 Assignment (due Thursday, April 19)

Organizing and Summarizing Data

Math 227 EXCEL / MEGASTAT Guide

Week 1: Introduction to Stata

SPSS. (Statistical Packages for the Social Sciences)

Unit I Supplement OpenIntro Statistics 3rd ed., Ch. 1

Lecture 1: Statistical Reasoning 2. Lecture 1. Simple Regression, An Overview, and Simple Linear Regression

Transcription:

BIOSTATISTICS LABORATORY PART 1: INTRODUCTION TO DATA ANALYIS WITH STATA: EXPLORING AND SUMMARIZING DATA Learning objectives: Getting data ready for analysis: 1) Learn several methods of exploring the contents of a dataset. 2) Learn the commands for labeling variables and values. First steps of data analysis: 3) Learn the commands for describing continuous and categorical variables. 4) Learn the commands to create new variables. FIRST THINGS FIRST: The general format of these labs combines an illustrative example followed by a Task that allows you to use the command again on your own. For each example, type in the commands and view the output in STATA s results window. Compare your output to the laboratory text to ensure your commands were correct. Throughout the labs there will be Notes of Caution that explain common errors or problems with the commands. STATA commands are shown in the courier font as they actually appear when typed in the STATA program or when viewed in the reference manuals. Each command is also enclosed in a box for easy reference. When referring to a generic variable name in a command, we will use varname. However, when typing the command, you should insert the actual name of your variable. CAVEAT: Keep in mind, modern statistical computer packages are a double-edged sword: on one hand, it s very easy to perform complex analyses; on the other hand, it s also very easy to do them wrong leading to incorrect answers. Make sure you understand the assumptions underlying each statistical test and, if you don t, get help from an epidemiologist or statistician. 1

EXPLORING DATA: Before performing any analysis it is important to get to know your data. STATA can help you with this task in several ways. The general goal of exploring your data is to make sure it was imported without errors and to become familiar with the general layout. Some specific goals include looking for missing values (how they are coded) and unrealistic values (e.g., age of 235) that may influence your results. Look in the Documentation Accompanying the Data When you receive a dataset, whether a national sample of hospital discharges or a homegrown case-series, read any accompanying documentation. Specifically, look for the definition of each variable, what the values of each variable mean, and how missing values are encoded. To create your course dataset, I have already done this for the Maryland CABG dataset and added appropriate labels to the STATA version of the dataset on your CD. More on Missing Values Missing values are often coded as large numbers (e.g., 999) or large negative numbers (e.g., -999) so it is important recognize this before doing any analyses. STATA codes missing values as a single period (. ) and will exclude observations that are missing when performing analyses. We have already converted missing values from the Maryland dataset to the STATA convention (. ). Using the Data Editor The best place to start looking at your data is to open your Data Editor and directly examine the format and content of your dataset. Task: Open your Maryland CABG dataset in STATA format (the one provided on the CD) open the Data Editor. Scroll down and across and examine how each variable is coded (dichotomous, continuous, etc ). Looking in the Data Editor is a good way to ensure all the variables, observations, and variable names were imported without problems. With large datasets it is impossible to examine every observation. But you can establish that the labels were brought in and the data have a uniform appearance within each column. Useful commands in STATA for Exploring Data: One useful command to make sure you have all the observations imported is to type summarize, which will give you a summary of all your variables. summarize 2

Variable Obs Mean Std. Dev. Min Max ---------------------- IDnumber 4668 2334.5 1347.68 1 4668 age 4668 65.78535 10.73579 16 94 atype 4650 2.172258.8960876 1 6 died 4661.0281056.1652922 0 1 female 4668.3018423.4591064 0 1 ---------------------- los 4668 8.430805 7.654034 0 114 pay1 4644 1.893196 1.028039 1 6 pr1 4668 3613.051 1.207581 3610 3619 race 4654 1.569618 1.38387 1 6 totchg 4668 26447.18 21297.32 4422 355980 Examining this output will allow you to check the number of observations of each variable. Also you make sure that the minimum and maximum values are within the plausible range for that variable. For example, a value of age of 325 and a value for death of 999 (coded 0 or 1 ) are not plausible. In the case of 999, it is most likely a missing value. One thing you will notice about this dataset is that the values for most numbers are numeric, i.e., the variable race is coded 1-6. When you look in the Data Editor, however, these appear as words (white, black, etc ). Using the codebook command, you can learn what numbers go with what descriptions (see output below). The codebook command also provides other useful information about each variable, including the number of total observations, number of missing observations, and the range of values. When typed with an individual variable name after codebook, STATA will tell you about that single variable only. Otherwise, it will list all the variables in your dataset. codebook race Number of missing values ------------------------------------------------------------------------------- race Race ------------------------------------------------------------------------------- type: numeric (float) label: lrace range: [1,6] units: 1 unique values: 6 missing.: 14/4668 tabulation: Freq. Numeric Label 3634 1 White 551 2 Black 40 3 Hispanic 55 4 Asian/Pacific Islander Range of values 15 5 Native American 359 6 Other 14. Key to numeric coding of variables (known as value labels in STATA) 3

0 100 200 300 400 ACS Outcomes Research Course SUMMARIZING DATA: As described in lecture, the first step in any analysis is to generate summary statistics of the variables in your dataset. For continuous variables some measure of central tendency (mean or median) and a measure of spread (standard deviation, range, or inter-quartile range) should be determined. For dichotomous or categorical variables the proportion in each category should be calculated. Summarizing continuous variables Normally distributed variables when variables are normally distributed the mean is the preferred measure of central tendency. (I m using the term Normal loosely here to mean any symmetric or bell-shaped distribution.) The best way to make sure a variable has a normal distribution is to plot a frequency histogram looking for the bell-shaped curve. In STATA, you can plot the histogram with a superimposed normal curve to make the comparison easy. General command structure: histogram varname, frequency normal The first part of this command (histogram varname) will make a graph of the variable. The second part, after the comma (frequency and normal), modifies the output from the command. Adding the word frequency makes the y-axis a number (rather than frequency density ) and adding the word normal adds the superimposed normal curve. We will first make a histogram of patient age (usually normally distributed). (this may take a few seconds to run) histogram age, frequency normal Superimposed normal curve 20 40 60 80 100 Age in years at admission Y-axis: Frequency (number) of patients of each age X-axis: Patient ages The age variable distribution is close to the normal curve, and we can summarize it using the mean and standard deviation. 4

Type the command summarize age Variable Obs Mean Std. Dev. Min Max ---------------------- age 4668 65.78535 10.73579 16 94 Mean age of 65.8 years Standard deviation of 10.7 years Variables without a normal distribution when variables do not have a normal distribution the best measure of central tendency is the median (50 th percentile). Task: Make a histogram of length of stay is it normally distributed? No, most often length of stay (and costs) have right-skewed distributions (long right-sided tails ). Using the following command you can get the median (along with a lot of other information). summarize los, detail 25 th percentile Note that the mean is significantly larger than the median this is because of the right-skewed distribution 50 th percentile (median) 75 th percentile Length of stay ------------------------------------------------------------- Percentiles Smallest 1% 3 0 5% 3 0 10% 4 1 Obs 4668 25% 5 1 Sum of Wgt. 4668 50% 6 Mean 8.430805 Largest Std. Dev. 7.654034 75% 9 102 90% 14 108 Variance 58.58424 95% 20 112 Skewness 5.756123 99% 42 114 Kurtosis 54.32911 From this output, you can obtain the median and inter-quartile range (25 th to 75 th percentile) for the length of stay after CABG in Maryland: median length of stay, 6 days (IQR, 5 to 9 days). 5

Tabulating dichotomous or categorical variables The tab command in STATA is very useful for finding the proportion of observations in a certain category. Consider death in the hospital (0 = alive, 1= dead) a dichotomous variable. The proportion dead can be found using the following generic command: tab varname What was the mortality rate after CABG in Maryland? tab died STATA Output: Died during hospitaliza tion Freq. Percent Cum. Alive 4,530 97.19 97.19 Dead 131 2.81 100.00 Total 4,661 100.00 Task: Use the above command to determine the proportion of patients with Medicare as the primary payer? [Hint: replace the variable died in the above command with the variable of interest, pay1 ]. 6

EXTRA EXERCISES: Continue on and perform these extra exercises if you finish the lab early or you can perform these exercises on your own after the course. LABELING VARIABLES: Labeling variables in STATA is tricky. For some reason, it requires two steps: 1) defining the labels and 2) attaching the labels to the actual values in your dataset. I have already created labels for most variables. But as an example we will create a label for variable died, where 1=dead and 0=alive. The first step is to create or define the label. To keep it simple, when creating label names I usually add the word label to the front of the variable name. But the label name could be any word. label define labeldied 1 Dead 0 Alive Now, the second step is to label the values of the variable died. label values died labeldied Let s tabulate the variable so we can see our new labels: tab died STATA Output: died Freq. Percent Cum. Alive 4,530 97.19 97.19 Dead 131 2.81 100.00 Total 4,661 100.00 We can clearly see that the values are now labeled. But we can also add a label to the variable name as follows: label variable died Died during hospitalization 7

Task: Tabulate the variable again to make sure the label has been added. CREATING AND LABELING NEW VARIABLES: You will often need to create new variables using STATA. Next we will create a new variable dividing age (a continuous variable) into discrete categories. The command generate is used to create a new variable. For the new age variable, we will create five categories: <50 years, 50-59 years, 60-69 years, 70-80 years, and >80 years. We will call the new variable age5cat (since there are 5 categories). Type the commands in the box below: (after each line of text hit enter you will see some text in the results window but ignore it unless it s red which means an error). generate age5cat = 1 if age<50 replace age5cat = 2 if age>=50 & age<60 replace age5cat = 3 if age>=60 & age<70 replace age5cat = 4 if age>=70 & age<80 replace age5cat =5 if age>=80 To make sure the variable was created correctly, tabulate the new variable: tab age5cat age5cat Freq. Percent Cum. 1 362 7.75 7.75 2 990 21.21 28.96 3 1,398 29.95 58.91 4 1,498 32.09 91.00 5 420 9.00 100.00 Total 4,668 100.00 What do the values 1-5 mean? You may remember now, since you just typed them, but after a few weeks you may forget. Thus, we will generate labels for each category. Remember, STATA does this in two steps: 1) define the label itself and 2) attach the labels to the values of a variable. Type the commands: (this will create the label) label define lage5cat 1 <50 years 2 50-59 years 3 60-69 years 4 70-80 years 5 >80 years Type the commands: (this will attach the labels to the values) label values age5cat lage5cat 8

Also, we will label the actual variable (as opposed to the values): label variable age5cat Five age categories Now, test the labeling by making a table by typing the following command: tab age5cat Five age categories Freq. Percent Cum. <50 years 362 7.75 7.75 50-59 years 990 21.21 28.96 60-69 years 1,398 29.95 58.91 70-80 years 1,498 32.09 91.00 >80 years 420 9.00 100.00 Total 4,668 100.00 ACS Outcomes Research Course Your output should appear as above. Note that both the variable and the values now have labels. Learning how to label values and variables in STATA is very tricky. Remember that labeling the values (e.g., 1,2,3,4,etc ) requires two steps: 1) defining the label and then 2) adding the label to the values in your dataset. Labeling the variable itself as opposed to the values requires only one step. Labeling the variable allows you to give it a more descriptive name (such as Five age categories as in the example below). Now we are done with Part 1 of the laboratory. You can save any changes to your STATA dataset by going to File in the menu bar and scrolling down to Save. IMPORTING DATA INTO STATA: We have provided data for all coronary artery bypass surgery performed in Maryland during 2001 on a CD. The dataset is provided in both an Excel and STATA format. We will introduce two basic methods for getting a dataset from Excel into STATA. Copy and Paste Data: 1) Open the provided Excel file CABG.Maryland. To select all the data click in the upperleft hand box (above the 1 and to the left of the A ). Or you can manually select the entire dataset. Then go to the Edit menu and Copy the data. 2) Open STATA and the Data Editor (An empty dataset not the STATA file that already has the data). Click on the upper left hand cell (so it turns blue) and choose Edit Paste from the menu. The data should appear in the data editor along with variable names. (Remember to close the Data Editor before trying any commands). 3) Close STATA and do not save this file you might confuse this file with the STATA file used in the lab. (The STATA file provided on the CD already has variable labels.) 9

Using the insheet command: The insheet command is typed into the STATA command window and brings data directly into the Data Editor. The tricky thing about the insheet command is typing the path to the file on your computer luckily STATA can make this easy. 1) Open the Excel file and, using the Save As option, save it as a text (tab delimited) file. Save this somewhere on your computer where you ll be able to find it easily. You will encounter two dialog boxes when you save your dataset. Click OK and Yes when they appear. 2) Open STATA and type the following in your command window: insheet using your filename will go here 3) To insert the name of you re your file, go to the File option in the drop-down menu of STATA. Choose Filename and you should now be able to browse to find your new text file. 4) Use the window to find your stored datasets (CABG.Maryland.txt). Remember to change the file type to All files, so your text file will be visible. 5) Double click (or use the Open option) on your data file and the complete path will appear in your STATA command window. It should look something like the following: (But, of course, the exact path will be different for every computer.) insheet using "C:\Documents and Settings\CABG.Maryland.txt" 6) Now hit enter and STATA should import (or insheet) your text file. To make sure, open the data editor and visualize the data. Now close STATA and do not save this dataset either. 10