Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009
Indicator (Dummy) Variables Created from an existing categorical variable (e.g., bmicat ) Assigned value of 0 or 1 1, if the condition is true 0, if the condition is false bmicat BMI (categorical) type: numeric (float) label: bmicat_label range: [0,3] units: 1 unique values: 4 missing.: 5/60 tabulation: Freq. Numeric Label 10 0 Underweight 17 1 Normal 17 2 Overweight 11 3 Obese 5.
Indicator (Dummy) Variables Example: bmicat X b0 = 1underweight 0otherwise X b1 = 1normal 0otherwise X b2 = 1overweight 0otherwise X b3 = 1obese 0otherwise
Generating Indicator (Dummy) Variables Option 1: Use generate ( gen ) command gen underwt = (bmicat==0) if bmicat!=. gen normwt = (bmicat==1) if bmicat!=. gen overwt = (bmicat==2) if bmicat!=. gen obese = (bmicat==3) if bmicat!=.
Generating Indicator (Dummy) Variables Option 1: Use generate ( gen ) command. list bmicat underwt normwt overwt obese in 31/ | bmicat underwt normwt overwt obese | | | 31. | Normal | 32. | Overweight | 33. |..... | 34. | Overweight | 35. | Underweight | | | 36. | Normal | 37. | Overweight | 38. | Obese | 39. | Overweight | 40. | Underweight |
Generating Indicator (Dummy) Variables Option 2: Use tabulate command with generate option tabulate bmicat, generate(bmigrp). tabulate bmicat, generate(bmigrp) BMI | (categorica | l) | Freq. Percent Cum Underweight | Normal | Overweight | Obese | Total |
Generating Indicator (Dummy) Variables Option 2: Use tabulate command with generate option. list bmicat bmigrp1-bmigrp4 in 31/ | bmicat bmigrp1 bmigrp2 bmigrp3 bmigrp4 | | | 31. | Normal | 32. | Overweight | 33. |..... | 34. | Overweight | 35. | Underweight | | | 36. | Normal | 37. | Overweight | 38. | Obese | 39. | Overweight | 40. | Underweight |
Graphing in Stata 10
Creating Histograms Stata command: hist Example: Histogram of height, by sex hist height, by(sex)
Creating Histograms Stata command: hist Attach value labels to variable(s) of interest Use formatting options Example revisited: Histogram of height, by sex hist height, by(sex, title(“Distribution of height by sex”) note(“”)) xtitle(“height(in)”) scheme(s1mono)
Creating Box Plots Stata command: graph box Example: Box plot of height, by sex graph box height, by(sex, title(Boxplots of height by sex) note(“”)) ytitle(height(in)) scheme(s1mono)
Creating Box Plots Stata command: graph box Now using over option Example: Box plot of height, by sex graph box height, over(sex) title(“Boxplots of height by sex”) ytitle(“height(in)”) scheme(s1mono)
Creating Scatter Plots Stata command: scatter Example: Scatter plot of height and weight scatter height weight
Creating Scatter Plots Stata command: scatter Example: Scatter plot of height and weight by sex, with lowess smoothing twoway (scatter height weight if sex==0) /// (scatter height weight if sex==1) /// (lowess height weight if sex==0) /// (lowess height weight if sex==1)
Creating Scatter Plots Stata command: scatter Use formatting options Example revisited: Scatter plot of height and weight by sex, with lowess smoothing twoway(scatter height weight if sex==0,ms(D)) (scatter height weight if sex==1, ms(Oh)) (lowess height weight if sex==0) (lowess height weight if sex==1),scheme(s2mono) legend(col(2)order(1 “females” 2 “males” 3 “lowess females” 4 “lowess males”)) xtitle(weight(lbs)) ytitle(height(in)) title(Height vs. weight by sex) xlab(100(25)200) ylab(50(5)80)
Combining Graphs Stata command: graph combine Example: Histogram and box plot of height hist height, scheme(s1mono) name(hist) graph box height, scheme(s1mono) name(box) graph combine hist box, scheme(s1mono) title(distribution of height)