Top Banner
Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009
24

Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009.

Stata Review: Part II

Biost/Epi 536 Discussion SectionOctober 13, 2009

Page 2: 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 .

Page 3: Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009.

Indicator (Dummy) VariablesExample: bmicat

Xb0 =

1 underweight

0 otherwise

Xb1 =

1 normal

0 otherwise

Xb2 =

1 overweight

0 otherwise

Xb3 =

1 obese

0 otherwise

Page 4: Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009.

Generating Indicator (Dummy) VariablesOption 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!=.

Page 5: Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009.

Generating Indicator (Dummy) VariablesOption 1: Use generate (gen) command

. list bmicat underwt normwt overwt obese in 31/40 +-------------------------------------------------+ | bmicat underwt normwt overwt obese | |-------------------------------------------------| 31. | Normal 0 1 0 0 | 32. | Overweight 0 0 1 0 | 33. | . . . . . | 34. | Overweight 0 0 1 0 | 35. | Underweight 1 0 0 0 | |-------------------------------------------------| 36. | Normal 0 1 0 0 | 37. | Overweight 0 0 1 0 | 38. | Obese 0 0 0 1 | 39. | Overweight 0 0 1 0 | 40. | Underweight 1 0 0 0 | +-------------------------------------------------+

Page 6: Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009.

Generating Indicator (Dummy) VariablesOption 2: Use tabulate command with generate option

tabulate bmicat, generate(bmigrp)

. tabulate bmicat, generate(bmigrp)

BMI |(categorica | l) | Freq. Percent Cum.------------+-----------------------------------Underweight | 10 18.18 18.18 Normal | 17 30.91 49.09 Overweight | 17 30.91 80.00 Obese | 11 20.00 100.00------------+----------------------------------- Total | 55 100.00

Page 7: Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009.

Generating Indicator (Dummy) VariablesOption 2: Use tabulate command with generate option

. list bmicat bmigrp1-bmigrp4 in 31/40 +-----------------------------------------------------+ | bmicat bmigrp1 bmigrp2 bmigrp3 bmigrp4 | |-----------------------------------------------------| 31. | Normal 0 1 0 0 | 32. | Overweight 0 0 1 0 | 33. | . . . . . | 34. | Overweight 0 0 1 0 | 35. | Underweight 1 0 0 0 | |-----------------------------------------------------| 36. | Normal 0 1 0 0 | 37. | Overweight 0 0 1 0 | 38. | Obese 0 0 0 1 | 39. | Overweight 0 0 1 0 | 40. | Underweight 1 0 0 0 | +-----------------------------------------------------+

Page 8: Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009.

Graphing in Stata 10

Page 9: Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009.

Creating HistogramsStata command: hist

Example: Histogram of height, by sex

hist height, by(sex)

Page 10: Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009.

0.0

2.0

4.0

6

50 60 70 80 50 60 70 80

0 1D

ensi

ty

heightGraphs by sex

Page 11: Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009.

Creating HistogramsStata command: histAttach value labels to variable(s) of interestUse 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)

Page 12: Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009.

0.0

2.0

4.0

6

50 60 70 80 50 60 70 80

female male

Den

sity

height(in)

Distributions of height by sex

Page 13: Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009.

Creating Box PlotsStata 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)

Page 14: Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009.

50

60

70

80

female male

hei

ght(

in)

Boxplots of height by sex

Page 15: Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009.

Creating Box PlotsStata command: graph boxNow 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)

Page 16: Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009.

5060

7080

heig

ht(in

)

female male

Boxplots of height by sex

Page 17: Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009.

Creating Scatter PlotsStata command: scatter

Example: Scatter plot of height and weight

scatter height weight

Page 18: Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009.

50

60

70

80

hei

ght

100 150 200weight

Page 19: Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009.

Creating Scatter PlotsStata 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)

Page 20: Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009.

50

60

70

80

100 150 200weight

height heightlowess height weight lowess height weight

Page 21: Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009.

Creating Scatter PlotsStata command: scatterUse 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 “lowessfemales” 4 “lowess males”))xtitle(weight(lbs)) ytitle(height(in))title(Height vs. weight by sex)xlab(100(25)200) ylab(50(5)80)

Page 22: Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009.

50

55

60

65

70

75

80

hei

ght (

in)

100 125 150 175 200weight (lbs)

females maleslowess females lowess males

Height vs. weight by sex

Page 23: Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009.

Combining GraphsStata 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)

Page 24: Stata Review: Part II Biost/Epi 536 Discussion Section October 13, 2009.

0.0

2.0

4.0

6D

ensi

ty

50 55 60 65 70 75height 5060

7080

heig

ht

distribution of height