{smcl} {com}{sf}{ul off}{txt}{.-} name: {res} {txt}log: {res}/Users/eric/Work/MB/ATCR/200/Labs/lab 1.smcl {txt}log type: {res}smcl {txt}opened on: {res}18 Sep 2018, 06:54:15 {txt} {com}. . /* Read in the data posted on the class web site. These data come from a > study of voluntary counseling and testing (VCT) for HIV at Mulago Hospital > in Kampala, Uganda. */ . . use vct.dta, clear {txt} {com}. . *Stata lets us look at a spreadsheet of the data: */ . . browse {txt} {com}. . /* First see what variables are in the dataset. Check how many observations > and variables the dataset includes. */ . . describe {txt}Contains data from {res}vct.dta {txt} obs:{res} 3,389 {txt} vars:{res} 13 23 Jul 2018 10:11 {txt} size:{res} 71,169 {txt}{hline} storage display value variable name type format label variable label {hline} {p 0 48}{res}{bind:age }{txt}{bind: double }{bind:{txt}%12.0g }{space 1}{bind: }{bind: }{res}{res}{p_end} {p 0 48}{bind:auditc }{txt}{bind: byte }{bind:{txt}%12.0g }{space 1}{bind: }{bind: }{res}{res}{p_end} {p 0 48}{bind:cd4 }{txt}{bind: int }{bind:{txt}%11.0g }{space 1}{bind: }{bind: }{res}{res}{p_end} {p 0 48}{bind:cd4_cat }{txt}{bind: byte }{bind:{txt}%11.0g }{space 1}{bind: }{bind: }{res}{res}{p_end} {p 0 48}{bind:female }{txt}{bind: byte }{bind:{txt}%9.0g }{space 1}{bind: }{bind: }{res}{res}{p_end} {p 0 48}{bind:hiv }{txt}{bind: byte }{bind:{txt}%12.0g }{space 1}{bind: }{bind: }{res}{res}{p_end} {p 0 48}{bind:hungry }{txt}{bind: byte }{bind:{txt}%12.0g }{space 1}{bind: }{bind: }{res}{res}{p_end} {p 0 48}{bind:lastalc }{txt}{bind: byte }{bind:{txt}%20.0g }{space 1}{bind: }{bind: }{res}{res}{p_end} {p 0 48}{bind:n_adults }{txt}{bind: byte }{bind:{txt}%12.0g }{space 1}{bind: }{bind: }{res}{res}{p_end} {p 0 48}{bind:n_child }{txt}{bind: byte }{bind:{txt}%12.0g }{space 1}{bind: }{bind: }{res}{res}{p_end} {p 0 48}{bind:n_support }{txt}{bind: byte }{bind:{txt}%12.0g }{space 1}{bind: }{bind: }{res}{res}{p_end} {p 0 48}{bind:religion }{txt}{bind: byte }{bind:{txt}%10.0g }{space 1}{bind: }{bind: }{res}{res}{p_end} {p 0 48}{bind:sex }{txt}{bind: byte }{bind:{txt}%12.0g }{space 1}{bind: }{bind: }{res}{res}{p_end} {txt}{hline} Sorted by: {res}hiv {txt} {com}. . /* Now get brief summary statistics for all the variables in the dataset. The > full command we'll use is summarize, but we can shorten that to save typing. */ . . sum {txt} Variable {c |} Obs Mean Std. Dev. Min Max {hline 13}{c +}{hline 57} {space 9}age {c |}{res} 3,387 31.73267 9.846937 17.52 79.85 {txt}{space 6}auditc {c |}{res} 3,244 .6390259 1.483416 0 12 {txt}{space 9}cd4 {c |}{res} 999 329.2332 266.1177 1 1932 {txt}{space 5}cd4_cat {c |}{res} 999 1.618619 .9377913 0 3 {txt}{space 6}female {c |}{res} 3,389 .5417527 .4983272 0 1 {txt}{hline 13}{c +}{hline 57} {space 9}hiv {c |}{res} 3,389 .2959575 .4565393 0 1 {txt}{space 6}hungry {c |}{res} 3,321 3.603132 .7817434 1 4 {txt}{space 5}lastalc {c |}{res} 3,377 .9227125 .9101892 0 2 {txt}{space 4}n_adults {c |}{res} 3,384 1.664598 1.737786 0 12 {txt}{space 5}n_child {c |}{res} 3,382 1.850089 1.924868 0 12 {txt}{hline 13}{c +}{hline 57} {space 3}n_support {c |}{res} 3,372 3.907177 3.227549 0 20 {txt}{space 4}religion {c |}{res} 3,387 3.781518 1.994997 1 8 {txt}{space 9}sex {c |}{res} 3,389 1.541753 .4983272 1 2 {txt} {com}. . /* Now do a little data management to make the data easier to use. First assign some variable labels */ . . label var age "Age" {txt} {com}. label var auditc "Audit score, last 3 months" {txt} {com}. label var cd4 "CD4 count" {txt} {com}. label var cd4_cat "CD4 count" {txt} {com}. label var hiv "HIV status" {txt} {com}. label var hungry "How often household members go hungry" {txt} {com}. label var lastalc "Last time used alcohol" {txt} {com}. label var female "female sex" {txt} {com}. label var n_adults "# adults in household" {txt} {com}. label var n_child "# people <18yo in household" {txt} {com}. label var n_support "# people supported financially" {txt} {com}. label var religion "Religion" {txt} {com}. . /* Now add some missing value labels. A easy-to-remember convention is to use > the variable name as the name of the value label. (Stata doesn't have any > problem with this). Other easy-to-remember conventions will also work (e.g., > add an underscore to the variable name).*/ . . label define yesno 0 "no" 1 "yes" {txt} {com}. label values female yesno {txt} {com}. label define sex 1 "men" 2 "women" {txt} {com}. label values sex sex {txt} {com}. label define hiv 0 "HIV-" 1 "HIV+" {txt} {com}. label values hiv hiv {txt} {com}. label define cd4_cat 0 "<50" 1 "51-250" 2 "251-500" 3 ">500" {txt} {com}. label values cd4_cat cd4_cat {txt} {com}. label define hungry 1 "often" 2 "sometimes" 3 "seldom" 4 "never" {txt} {com}. label values hungry hungry {txt} {com}. label define lastalc 0 "never" 1 ">1 year ago" 2 "within last year" {txt} {com}. label values lastalc lastalc {txt} {com}. label define religion 1 "Protestant" 2 "Catholic" 3 "Saved" 4 "Moslem" 8 "Other" {txt} {com}. label values religion religion {txt} {com}. . /* You can use these commands to get information about the values labels for > a variable:*/ . . codebook religion {txt}{hline} {res}religion{right:Religion} {txt}{hline} {col 19}type: numeric ({res}byte{txt}) {ralign 22:label}: {res:religion}, but {res:2} nonmissing values are not labeled {col 18}range: [{res}1{txt},{res}8{txt}]{col 55}units: {res}1 {col 10}{txt}unique values: {res}5{col 51}{txt}missing .: {res}2{txt}/{res}3,389 {txt}{col 13}tabulation: Freq. Numeric Label {col 21}{res} 1,002{col 33} 1{col 43}{txt}Protestant {col 21}{res} 1,047{col 33} 4{col 43}{txt}Moslem {col 21}{res} 576{col 33} 5{col 43} {col 21} 679{col 33} 6{col 43} {col 21} 83{col 33} 8{col 43}{txt}Other {col 21}{res} 2{col 33} .{col 43} {txt} {com}. label list religion {txt}religion: {res} 1 Protestant 2 Catholic 3 Saved 4 Moslem 8 Other {txt} {com}. . /* Note the use of the indicator female for sex, coded 0 = no and 1 = yes. > Indicators coded like this have several advantages: > * the mean value of the indicator gives the proportion of women in the dataset > * this is the coding required for binary outcomes in logistic models > * unlike a variable for sex, coded 1 = male 2 = female, the indicator does > not require us to remember the coding and value labels > > Now take another look at what we have. The value labels should show up in the > browser. */ . . des {txt}Contains data from {res}vct.dta {txt} obs:{res} 3,389 {txt} vars:{res} 13 23 Jul 2018 10:11 {txt} size:{res} 71,169 {txt}{hline} storage display value variable name type format label variable label {hline} {p 0 48}{res}{bind:age }{txt}{bind: double }{bind:{txt}%12.0g }{space 1}{bind: }{bind: }{res}{res}Age{p_end} {p 0 48}{bind:auditc }{txt}{bind: byte }{bind:{txt}%12.0g }{space 1}{bind: }{bind: }{res}{res}Audit score, last 3 months{p_end} {p 0 48}{bind:cd4 }{txt}{bind: int }{bind:{txt}%11.0g }{space 1}{bind: }{bind: }{res}{res}CD4 count{p_end} {p 0 48}{bind:cd4_cat }{txt}{bind: byte }{bind:{txt}%11.0g }{space 1}{bind:cd4_cat }{bind: }{res}{res}CD4 count{p_end} {p 0 48}{bind:female }{txt}{bind: byte }{bind:{txt}%9.0g }{space 1}{bind:yesno }{bind: }{res}{res}female sex{p_end} {p 0 48}{bind:hiv }{txt}{bind: byte }{bind:{txt}%12.0g }{space 1}{bind:hiv }{bind: }{res}{res}HIV status{p_end} {p 0 48}{bind:hungry }{txt}{bind: byte }{bind:{txt}%12.0g }{space 1}{bind:hungry }{bind: }{res}{res}How often household members go hungry{p_end} {p 0 48}{bind:lastalc }{txt}{bind: byte }{bind:{txt}%20.0g }{space 1}{bind:lastalc }{bind: }{res}{res}Last time used alcohol{p_end} {p 0 48}{bind:n_adults }{txt}{bind: byte }{bind:{txt}%12.0g }{space 1}{bind: }{bind: }{res}{res}# adults in household{p_end} {p 0 48}{bind:n_child }{txt}{bind: byte }{bind:{txt}%12.0g }{space 1}{bind: }{bind: }{res}{res}# people <18yo in household{p_end} {p 0 48}{bind:n_support }{txt}{bind: byte }{bind:{txt}%12.0g }{space 1}{bind: }{bind: }{res}{res}# people supported financially{p_end} {p 0 48}{bind:religion }{txt}{bind: byte }{bind:{txt}%10.0g }{space 1}{bind:religion }{bind: }{res}{res}Religion{p_end} {p 0 48}{bind:sex }{txt}{bind: byte }{bind:{txt}%12.0g }{space 1}{bind:sex }{bind: }{res}{res}{p_end} {txt}{hline} Sorted by: {res}hiv {txt} {com}. sum {txt} Variable {c |} Obs Mean Std. Dev. Min Max {hline 13}{c +}{hline 57} {space 9}age {c |}{res} 3,387 31.73267 9.846937 17.52 79.85 {txt}{space 6}auditc {c |}{res} 3,244 .6390259 1.483416 0 12 {txt}{space 9}cd4 {c |}{res} 999 329.2332 266.1177 1 1932 {txt}{space 5}cd4_cat {c |}{res} 999 1.618619 .9377913 0 3 {txt}{space 6}female {c |}{res} 3,389 .5417527 .4983272 0 1 {txt}{hline 13}{c +}{hline 57} {space 9}hiv {c |}{res} 3,389 .2959575 .4565393 0 1 {txt}{space 6}hungry {c |}{res} 3,321 3.603132 .7817434 1 4 {txt}{space 5}lastalc {c |}{res} 3,377 .9227125 .9101892 0 2 {txt}{space 4}n_adults {c |}{res} 3,384 1.664598 1.737786 0 12 {txt}{space 5}n_child {c |}{res} 3,382 1.850089 1.924868 0 12 {txt}{hline 13}{c +}{hline 57} {space 3}n_support {c |}{res} 3,372 3.907177 3.227549 0 20 {txt}{space 4}religion {c |}{res} 3,387 3.781518 1.994997 1 8 {txt}{space 9}sex {c |}{res} 3,389 1.541753 .4983272 1 2 {txt} {com}. browse {txt} {com}. . /* Note that CD4 count is present only for the 30% of participants who are HIV+. > > ******************************************************************************** > Creating and modifying new variables > > To create a new variable, use the generate command; to modify it, use the > replace command, and to drop a variable, use the drop command. In making > variables, you will often need to use so-called logical operators: > equal: == > not equal: ~= or ~= > and: & > or: | > greater: > or >= > less: < or <= > Missing numeric values are denoted . (for missing character strings use ""). > Note that missing numeric values are stored as a very large number, (always > the largest in the dataset). As a result, if you are missing age values for > 2 persons in the data set, and you create a new variable that includes all > participants with age>30, you will include the 2 with missing data. > > Example: AUDIT-C is a scale measuring alcohol consumption, ranging from 0-12. > For those not currently drinking (on this survey it is the past 3 months) the > score is 0, for those drinking the score is 1-12. To make a new variable that > represents any alcohol consumption we can use this following code: */ . . gen alc_any=0 if auditc==0 {txt}(966 missing values generated) {com}. replace alc_any=1 if auditc>0 & auditc !=. {txt}(821 real changes made) {com}. label var alc_any "Any alcohol use" {txt} {com}. label values alc_any yesno // value label yesno is already part of the dataset {txt} {com}. * check to make sure this worked . tab auditc alc_any, missing {txt}Audit {c |} score, {c |} last 3 {c |} Any alcohol use months {c |} no yes . {c |} Total {hline 11}{c +}{hline 33}{c +}{hline 10} 0 {c |}{res} 2,423 0 0 {txt}{c |}{res} 2,423 {txt} 1 {c |}{res} 0 363 0 {txt}{c |}{res} 363 {txt} 2 {c |}{res} 0 134 0 {txt}{c |}{res} 134 {txt} 3 {c |}{res} 0 118 0 {txt}{c |}{res} 118 {txt} 4 {c |}{res} 0 108 0 {txt}{c |}{res} 108 {txt} 5 {c |}{res} 0 44 0 {txt}{c |}{res} 44 {txt} 6 {c |}{res} 0 13 0 {txt}{c |}{res} 13 {txt} 7 {c |}{res} 0 10 0 {txt}{c |}{res} 10 {txt} 8 {c |}{res} 0 7 0 {txt}{c |}{res} 7 {txt} 9 {c |}{res} 0 15 0 {txt}{c |}{res} 15 {txt} 10 {c |}{res} 0 4 0 {txt}{c |}{res} 4 {txt} 11 {c |}{res} 0 3 0 {txt}{c |}{res} 3 {txt} 12 {c |}{res} 0 2 0 {txt}{c |}{res} 2 {txt} . {c |}{res} 0 0 145 {txt}{c |}{res} 145 {txt}{hline 11}{c +}{hline 33}{c +}{hline 10} Total {c |}{res} 2,423 821 145 {txt}{c |}{res} 3,389 {txt} {com}. . /* The second condition ensures that missings are kept as missing. A better > way to do this is to use the recode command, which will handle the missing > values correctly: */ . . drop alc_any {txt} {com}. recode auditc (0=0 "no") (2/max=1 "yes"), gen(alc_any) {txt}(458 differences between auditc and alc_any) {com}. label var alc_any "Any alcohol use" {txt} {com}. * check to make sure this worked . tab auditc alc_any, missing {txt}Audit {c |} score, {c |} last 3 {c |} Any alcohol use months {c |} no yes . {c |} Total {hline 11}{c +}{hline 33}{c +}{hline 10} 0 {c |}{res} 2,423 0 0 {txt}{c |}{res} 2,423 {txt} 1 {c |}{res} 0 363 0 {txt}{c |}{res} 363 {txt} 2 {c |}{res} 0 134 0 {txt}{c |}{res} 134 {txt} 3 {c |}{res} 0 118 0 {txt}{c |}{res} 118 {txt} 4 {c |}{res} 0 108 0 {txt}{c |}{res} 108 {txt} 5 {c |}{res} 0 44 0 {txt}{c |}{res} 44 {txt} 6 {c |}{res} 0 13 0 {txt}{c |}{res} 13 {txt} 7 {c |}{res} 0 10 0 {txt}{c |}{res} 10 {txt} 8 {c |}{res} 0 7 0 {txt}{c |}{res} 7 {txt} 9 {c |}{res} 0 15 0 {txt}{c |}{res} 15 {txt} 10 {c |}{res} 0 4 0 {txt}{c |}{res} 4 {txt} 11 {c |}{res} 0 3 0 {txt}{c |}{res} 3 {txt} 12 {c |}{res} 0 2 0 {txt}{c |}{res} 2 {txt} . {c |}{res} 0 0 145 {txt}{c |}{res} 145 {txt}{hline 11}{c +}{hline 33}{c +}{hline 10} Total {c |}{res} 2,423 821 145 {txt}{c |}{res} 3,389 {txt} {com}. . /* We can also use recode and replace to make a more complex indicator for > unhealthy alcohol consumption, using different cutoff for men (>=4) and > women (>=3): */ . . recode auditc (min/2=0 "no") (3/max=1 "yes"), gen(alc_unh) {txt}(821 differences between auditc and alc_unh) {com}. replace alc_unh = 0 if female==0 & auditc==3 {txt}(69 real changes made) {com}. label var alc_unh "Unhealthy alcohol use" {txt} {com}. * check to make sure this worked . bysort female: tab auditc alc_unh, m {txt}{hline} -> female = no Audit {c |} score, {c |} last 3 {c |} Unhealthy alcohol use months {c |} no yes . {c |} Total {hline 11}{c +}{hline 33}{c +}{hline 10} 0 {c |}{res} 1,042 0 0 {txt}{c |}{res} 1,042 {txt} 1 {c |}{res} 166 0 0 {txt}{c |}{res} 166 {txt} 2 {c |}{res} 57 0 0 {txt}{c |}{res} 57 {txt} 3 {c |}{res} 69 0 0 {txt}{c |}{res} 69 {txt} 4 {c |}{res} 0 71 0 {txt}{c |}{res} 71 {txt} 5 {c |}{res} 0 34 0 {txt}{c |}{res} 34 {txt} 6 {c |}{res} 0 9 0 {txt}{c |}{res} 9 {txt} 7 {c |}{res} 0 8 0 {txt}{c |}{res} 8 {txt} 8 {c |}{res} 0 5 0 {txt}{c |}{res} 5 {txt} 9 {c |}{res} 0 12 0 {txt}{c |}{res} 12 {txt} 10 {c |}{res} 0 2 0 {txt}{c |}{res} 2 {txt} 11 {c |}{res} 0 2 0 {txt}{c |}{res} 2 {txt} 12 {c |}{res} 0 2 0 {txt}{c |}{res} 2 {txt} . {c |}{res} 0 0 74 {txt}{c |}{res} 74 {txt}{hline 11}{c +}{hline 33}{c +}{hline 10} Total {c |}{res} 1,334 145 74 {txt}{c |}{res} 1,553 {txt}{hline} -> female = yes Audit {c |} score, {c |} last 3 {c |} Unhealthy alcohol use months {c |} no yes . {c |} Total {hline 11}{c +}{hline 33}{c +}{hline 10} 0 {c |}{res} 1,381 0 0 {txt}{c |}{res} 1,381 {txt} 1 {c |}{res} 197 0 0 {txt}{c |}{res} 197 {txt} 2 {c |}{res} 77 0 0 {txt}{c |}{res} 77 {txt} 3 {c |}{res} 0 49 0 {txt}{c |}{res} 49 {txt} 4 {c |}{res} 0 37 0 {txt}{c |}{res} 37 {txt} 5 {c |}{res} 0 10 0 {txt}{c |}{res} 10 {txt} 6 {c |}{res} 0 4 0 {txt}{c |}{res} 4 {txt} 7 {c |}{res} 0 2 0 {txt}{c |}{res} 2 {txt} 8 {c |}{res} 0 2 0 {txt}{c |}{res} 2 {txt} 9 {c |}{res} 0 3 0 {txt}{c |}{res} 3 {txt} 10 {c |}{res} 0 2 0 {txt}{c |}{res} 2 {txt} 11 {c |}{res} 0 1 0 {txt}{c |}{res} 1 {txt} . {c |}{res} 0 0 71 {txt}{c |}{res} 71 {txt}{hline 11}{c +}{hline 33}{c +}{hline 10} Total {c |}{res} 1,655 110 71 {txt}{c |}{res} 1,836 {txt} {com}. . /* Note the abbreviated name for the unhealthy alcohol use indicator. Stata > does allow long, explicit variable names, with some limits, but they can be > tedious to use in programming; better to encode the extra information in the > variable label. Note also that all the alcohol variables we have made start > with the same stem alc_, also with a view towards making them easier to remember. > > Finally, we can use recode to categorize the continuous variable, age. In > doing this, we can use the ordering of the elements of the command to control > whether the values at each cutpoint are put into the lower or upper category. > Specifically, values equal to a cutpoint get put into the class defined by the > element where the cutpoint is first encountered. We will first code age > categories <=20, >20 to <=30, >30 to <=40, >40 to <=50, and >50. */ . . qui recode age (min/20=1 "<=20") (20/30=2 ">20 to 30") (30/40=3 ">30 to 40") /// > (40/50=4 ">40 to 50") (50/max=5 ">50"), gen(age_cat) {txt} {com}. label var age_cat "Age" {txt} {com}. * check this . tabstat age, by(age_cat) s(min max) {txt}Summary for variables: age {col 6}by categories of: age_cat (Age) {ralign 9:age_cat} {...} {c |} min max {hline 10}{c +}{hline 20} {ralign 9:<=20} {...} {c |}{...} {res} 17.52 20 {txt}{ralign 9:>20 to 30} {...} {c |}{...} {res} 20.01 30 {txt}{ralign 9:>30 to 40} {...} {c |}{...} {res} 30.01 40 {txt}{ralign 9:>40 to 50} {...} {c |}{...} {res} 40.01 49.99 {txt}{ralign 9:>50} {...} {c |}{...} {res} 50.01 79.85 {txt}{hline 10}{c +}{hline 20} {ralign 9:Total} {...} {c |}{...} {res} 17.52 79.85 {txt}{hline 10}{c BT}{hline 20} {com}. . /* To include the cutpoints in the upper category, we can reverse the order of > the elements of the command: */ . . drop age_cat {txt} {com}. qui recode age (50/max=5 "50+") (40/50=4 "40 to <50") (30/40=3 "30 to <40") /// > (20/30=2 "20 to <30") (min/20=1 "<20") , gen(age_cat) {txt} {com}. label var age_cat "Age" {txt} {com}. * check this . tabstat age, by(age_cat) s(min max) {txt}Summary for variables: age {col 6}by categories of: age_cat (Age) {ralign 9:age_cat} {...} {c |} min max {hline 10}{c +}{hline 20} {ralign 9:<20} {...} {c |}{...} {res} 17.52 19.98 {txt}{ralign 9:20 to <30} {...} {c |}{...} {res} 20 29.99 {txt}{ralign 9:30 to <40} {...} {c |}{...} {res} 30 39.99 {txt}{ralign 9:40 to <50} {...} {c |}{...} {res} 40 49.99 {txt}{ralign 9:50+} {...} {c |}{...} {res} 50.01 79.85 {txt}{hline 10}{c +}{hline 20} {ralign 9:Total} {...} {c |}{...} {res} 17.52 79.85 {txt}{hline 10}{c BT}{hline 20} {com}. . /* Put the variables in alphabetical order, compress anything stored wastefully, > get a final look at the dataet, and save the modified dataset. */ . . compress {txt}variable {bf}age_cat{sf} was {bf}{res}double{sf}{txt} now {bf}{res}byte{sf} {txt} (23,723 bytes saved) {com}. aorder {txt} {com}. . /* Finally, save the revised dataset. It's good practice never to write over > the input dataset. */ . . save vct_revised, replace {txt}file vct_revised.dta saved {com}. . /* Now we will use the revised dataset to do more exploration. */ . . use vct_revised.dta, clear {txt} {com}. . /* Briefly remind yourself of what is in the dataset. Check how many observations > and variables the dataset includes. */ . . describe {txt}Contains data from {res}vct_revised.dta {txt} obs:{res} 3,389 {txt} vars:{res} 16 18 Sep 2018 06:54 {txt} size:{res} 81,336 {txt}{hline} storage display value variable name type format label variable label {hline} {p 0 48}{res}{bind:age }{txt}{bind: double }{bind:{txt}%12.0g }{space 1}{bind: }{bind: }{res}{res}Age{p_end} {p 0 48}{bind:age_cat }{txt}{bind: byte }{bind:{txt}%9.0g }{space 1}{bind:age_cat }{bind: }{res}{res}Age{p_end} {p 0 48}{bind:alc_any }{txt}{bind: byte }{bind:{txt}%9.0g }{space 1}{bind:alc_any }{bind: }{res}{res}Any alcohol use{p_end} {p 0 48}{bind:alc_unh }{txt}{bind: byte }{bind:{txt}%9.0g }{space 1}{bind:alc_unh }{bind: }{res}{res}Unhealthy alcohol use{p_end} {p 0 48}{bind:auditc }{txt}{bind: byte }{bind:{txt}%12.0g }{space 1}{bind: }{bind: }{res}{res}Audit score, last 3 months{p_end} {p 0 48}{bind:cd4 }{txt}{bind: int }{bind:{txt}%11.0g }{space 1}{bind: }{bind: }{res}{res}CD4 count{p_end} {p 0 48}{bind:cd4_cat }{txt}{bind: byte }{bind:{txt}%11.0g }{space 1}{bind:cd4_cat }{bind: }{res}{res}CD4 count{p_end} {p 0 48}{bind:female }{txt}{bind: byte }{bind:{txt}%9.0g }{space 1}{bind:yesno }{bind: }{res}{res}female sex{p_end} {p 0 48}{bind:hiv }{txt}{bind: byte }{bind:{txt}%12.0g }{space 1}{bind:hiv }{bind: }{res}{res}HIV status{p_end} {p 0 48}{bind:hungry }{txt}{bind: byte }{bind:{txt}%12.0g }{space 1}{bind:hungry }{bind: }{res}{res}How often household members go hungry{p_end} {p 0 48}{bind:lastalc }{txt}{bind: byte }{bind:{txt}%20.0g }{space 1}{bind:lastalc }{bind: }{res}{res}Last time used alcohol{p_end} {p 0 48}{bind:n_adults }{txt}{bind: byte }{bind:{txt}%12.0g }{space 1}{bind: }{bind: }{res}{res}# adults in household{p_end} {p 0 48}{bind:n_child }{txt}{bind: byte }{bind:{txt}%12.0g }{space 1}{bind: }{bind: }{res}{res}# people <18yo in household{p_end} {p 0 48}{bind:n_support }{txt}{bind: byte }{bind:{txt}%12.0g }{space 1}{bind: }{bind: }{res}{res}# people supported financially{p_end} {p 0 48}{bind:religion }{txt}{bind: byte }{bind:{txt}%10.0g }{space 1}{bind:religion }{bind: }{res}{res}Religion{p_end} {p 0 48}{bind:sex }{txt}{bind: byte }{bind:{txt}%12.0g }{space 1}{bind:sex }{bind: }{res}{res}{p_end} {txt}{hline} Sorted by: {res}female {txt} {com}. . /* Now get brief summary statistics for all the variables in the dataset. The > full command we'll use is summarize, but we can shorten to save typing. */ . . sum {txt} Variable {c |} Obs Mean Std. Dev. Min Max {hline 13}{c +}{hline 57} {space 9}age {c |}{res} 3,387 31.73267 9.846937 17.52 79.85 {txt}{space 5}age_cat {c |}{res} 3,387 2.653085 .9790142 1 5 {txt}{space 5}alc_any {c |}{res} 3,244 .2530826 .4348449 0 1 {txt}{space 5}alc_unh {c |}{res} 3,244 .0786067 .2691653 0 1 {txt}{space 6}auditc {c |}{res} 3,244 .6390259 1.483416 0 12 {txt}{hline 13}{c +}{hline 57} {space 9}cd4 {c |}{res} 999 329.2332 266.1177 1 1932 {txt}{space 5}cd4_cat {c |}{res} 999 1.618619 .9377913 0 3 {txt}{space 6}female {c |}{res} 3,389 .5417527 .4983272 0 1 {txt}{space 9}hiv {c |}{res} 3,389 .2959575 .4565393 0 1 {txt}{space 6}hungry {c |}{res} 3,321 3.603132 .7817434 1 4 {txt}{hline 13}{c +}{hline 57} {space 5}lastalc {c |}{res} 3,377 .9227125 .9101892 0 2 {txt}{space 4}n_adults {c |}{res} 3,384 1.664598 1.737786 0 12 {txt}{space 5}n_child {c |}{res} 3,382 1.850089 1.924868 0 12 {txt}{space 3}n_support {c |}{res} 3,372 3.907177 3.227549 0 20 {txt}{space 4}religion {c |}{res} 3,387 3.781518 1.994997 1 8 {txt}{hline 13}{c +}{hline 57} {space 9}sex {c |}{res} 3,389 1.541753 .4983272 1 2 {txt} {com}. . /* Note how many people had CD4 counts. Why is this less than the number of > observations? > > To get more information about continuous variables, add the detail option to > the summarize command. */ . . sum age cd4, detail {txt}Age {hline 61} Percentiles Smallest 1% {res} 18.04 17.52 {txt} 5% {res} 19.65 17.56 {txt}10% {res} 21.15 17.57 {txt}Obs {res} 3,387 {txt}25% {res} 24.25 17.6 {txt}Sum of Wgt. {res} 3,387 {txt}50% {res} 29.53 {txt}Mean {res} 31.73267 {txt}Largest Std. Dev. {res} 9.846937 {txt}75% {res} 37.73 74.62 {txt}90% {res} 45.89 75.33 {txt}Variance {res} 96.96217 {txt}95% {res} 50.42 78.29 {txt}Skewness {res} 1.029901 {txt}99% {res} 60.01 79.85 {txt}Kurtosis {res} 3.974463 {txt}CD4 count {hline 61} Percentiles Smallest 1% {res} 5 1 {txt} 5% {res} 14 2 {txt}10% {res} 36 2 {txt}Obs {res} 999 {txt}25% {res} 130 2 {txt}Sum of Wgt. {res} 999 {txt}50% {res} 283 {txt}Mean {res} 329.2332 {txt}Largest Std. Dev. {res} 266.1177 {txt}75% {res} 463 1461 {txt}90% {res} 659 1601 {txt}Variance {res} 70818.64 {txt}95% {res} 866 1804 {txt}Skewness {res} 1.444705 {txt}99% {res} 1182 1932 {txt}Kurtosis {res} 6.518639 {txt} {com}. . . ******************************************************************************** . . /* Exploratory analysis using descriptive statistics > > The tabstat command gives easy summaries. Here we use the stat() option > to obtain the number of observations, mean, SD, minimum, 25th percentile, > median, 75th percentile, and maximum. The format option controls rounding of > the statistics. */ . . tabstat age cd4, stat(n mean sd min p25 p50 p75 max) format(%8.3g) {txt} stats {...} {c |}{...} age cd4 {hline 9}{c +}{hline 20} {ralign 8:N} {...} {c |}{...} {res} 3387 999 {txt}{ralign 8:mean} {...} {c |}{...} {res} 31.7 329 {txt}{ralign 8:sd} {...} {c |}{...} {res} 9.85 266 {txt}{ralign 8:min} {...} {c |}{...} {res} 17.5 1 {txt}{ralign 8:p25} {...} {c |}{...} {res} 24.3 130 {txt}{ralign 8:p50} {...} {c |}{...} {res} 29.5 283 {txt}{ralign 8:p75} {...} {c |}{...} {res} 37.7 463 {txt}{ralign 8:max} {...} {c |}{...} {res} 79.9 1932 {txt}{hline 9}{c BT}{hline 20} {com}. . /* Adding the option col(statistics) transposes the results. Also, we can use > the line break marker /// to tell stata that command continues after a hard > return. This is handy if you want to use a relatively narrow do-file editor > window. (Note that you can't use /// in the command window.) */ . . tabstat age cd4, stat(n mean sd min p25 p50 p75 max) /// > col(statistics) format(%8.3g) {txt}{ralign 12:variable} {...} {c |} N mean sd min p25 p50 p75 {hline 13}{c +}{hline 70} {ralign 12:age} {...} {c |}{...} {res} 3387 31.7 9.85 17.5 24.3 29.5 37.7 {txt}{ralign 12:cd4} {...} {c |}{...} {res} 999 329 266 1 130 283 463 {txt}{hline 13}{c BT}{hline 70} {ralign 12:variable} {...} {c |} max {hline 13}{c +}{hline 10} {ralign 12:age} {...} {c |}{...} {res} 79.9 {txt}{ralign 12:cd4} {...} {c |}{...} {res} 1932 {txt}{hline 13}{c BT}{hline 10} {com}. . * Add the by() option to see the statistics stratified by HIV status. . tabstat age cd4, by(hiv) stat(n mean sd min p25 p50 p75 max) /// > col(statistics) format(%8.3g) {txt}Summary for variables: age cd4 {col 6}by categories of: hiv (HIV status) {ralign 6:hiv} {...} {c |} N mean sd min p25 p50 p75 max {hline 7}{c +}{hline 80} {ralign 6:HIV-} {...} {c |}{...} {res} 2384 31.3 10.5 17.5 23.4 28.3 37.5 79.9 {space 6} {...} {txt}{c |}{...} {res} 0 . . . . . . . {txt}{hline 7}{c +}{hline 80} {ralign 6:HIV+} {...} {c |}{...} {res} 1003 32.7 8.05 17.6 26.8 31.4 38.1 70 {space 6} {...} {txt}{c |}{...} {res} 999 329 266 1 130 283 463 1932 {txt}{hline 7}{c +}{hline 80} {ralign 6:Total} {...} {c |}{...} {res} 3387 31.7 9.85 17.5 24.3 29.5 37.7 79.9 {space 6} {...} {txt}{c |}{...} {res} 999 329 266 1 130 283 463 1932 {txt}{hline 7}{c BT}{hline 80} {com}. . /* The tabstat command can only be used to stratify by one by variable at a > time; tostratify by >1 variables, use the table command. */ . . table hiv female hungry, by(religion) c(mean age) format(%8.1f) {txt}{hline 11}{c TT}{hline 53} Religion {c |} How often household members go hungry and female sex and HIV {c |} {hline 2} often {hline 1} {hline 1} someti {hline 1} {hline 1} seldom {hline 1} {hline 2} never {hline 1} status {c |} no yes no yes no yes no yes {hline 11}{c +}{hline 53} Protestant {c |} HIV- {c |} {res}34.9 32.3 33.9 34.9 29.8 32.5 31.9 31.9 {txt}HIV+ {c |} {res}38.3 32.6 33.5 31.1 39.5 34.2 35.4 31.4 {txt}{hline 11}{c +}{hline 53} Moslem {c |} HIV- {c |} {res}33.4 38.7 29.2 34.2 32.5 33.3 31.3 31.2 {txt}HIV+ {c |} {res}34.0 36.2 33.9 29.9 33.8 31.1 34.1 30.5 {txt}{hline 11}{c +}{hline 53} 5 {c |} HIV- {c |} {res}24.9 34.4 30.1 28.4 26.1 32.5 29.5 30.3 {txt}HIV+ {c |} {res}38.3 26.9 35.9 30.5 31.5 32.4 35.8 30.9 {txt}{hline 11}{c +}{hline 53} 6 {c |} HIV- {c |} {res}37.8 36.2 30.0 34.0 27.7 31.3 31.1 31.2 {txt}HIV+ {c |} {res}43.6 33.4 35.9 33.1 38.7 31.8 34.9 30.0 {txt}{hline 11}{c +}{hline 53} Other {c |} HIV- {c |} {res} 27.9 28.0 26.1 24.2 29.5 31.3 {txt}HIV+ {c |} {res} 39.6 39.5 46.5 37.1 35.0 32.8 {txt}{hline 11}{c BT}{hline 53} {com}. . * To see how categorical variables relate to each other, use the tab command. . tab age_cat hiv, row {txt} {c TLC}{hline 16}{c TRC} {c |} Key{col 18}{c |} {c LT}{hline 16}{c RT} {c |}{space 3}{it:frequency}{col 18}{c |} {c |}{space 1}{it:row percentage}{col 18}{c |} {c BLC}{hline 16}{c BRC} {c |} HIV status Age {c |} HIV- HIV+ {c |} Total {hline 11}{c +}{hline 22}{c +}{hline 10} <20 {c |}{res} 196 24 {txt}{c |}{res} 220 {txt}{c |}{res} 89.09 10.91 {txt}{c |}{res} 100.00 {txt}{hline 11}{c +}{hline 22}{c +}{hline 10} 20 to <30 {c |}{res} 1,150 412 {txt}{c |}{res} 1,562 {txt}{c |}{res} 73.62 26.38 {txt}{c |}{res} 100.00 {txt}{hline 11}{c +}{hline 22}{c +}{hline 10} 30 to <40 {c |}{res} 576 394 {txt}{c |}{res} 970 {txt}{c |}{res} 59.38 40.62 {txt}{c |}{res} 100.00 {txt}{hline 11}{c +}{hline 22}{c +}{hline 10} 40 to <50 {c |}{res} 302 141 {txt}{c |}{res} 443 {txt}{c |}{res} 68.17 31.83 {txt}{c |}{res} 100.00 {txt}{hline 11}{c +}{hline 22}{c +}{hline 10} 50+ {c |}{res} 160 32 {txt}{c |}{res} 192 {txt}{c |}{res} 83.33 16.67 {txt}{c |}{res} 100.00 {txt}{hline 11}{c +}{hline 22}{c +}{hline 10} Total {c |}{res} 2,384 1,003 {txt}{c |}{res} 3,387 {txt}{c |}{res} 70.39 29.61 {txt}{c |}{res} 100.00 {txt} {com}. tab female hiv, row {txt} {c TLC}{hline 16}{c TRC} {c |} Key{col 18}{c |} {c LT}{hline 16}{c RT} {c |}{space 3}{it:frequency}{col 18}{c |} {c |}{space 1}{it:row percentage}{col 18}{c |} {c BLC}{hline 16}{c BRC} {c |} HIV status female sex {c |} HIV- HIV+ {c |} Total {hline 11}{c +}{hline 22}{c +}{hline 10} no {c |}{res} 1,175 378 {txt}{c |}{res} 1,553 {txt}{c |}{res} 75.66 24.34 {txt}{c |}{res} 100.00 {txt}{hline 11}{c +}{hline 22}{c +}{hline 10} yes {c |}{res} 1,211 625 {txt}{c |}{res} 1,836 {txt}{c |}{res} 65.96 34.04 {txt}{c |}{res} 100.00 {txt}{hline 11}{c +}{hline 22}{c +}{hline 10} Total {c |}{res} 2,386 1,003 {txt}{c |}{res} 3,389 {txt}{c |}{res} 70.40 29.60 {txt}{c |}{res} 100.00 {txt} {com}. tab hungry hiv, row {txt} {c TLC}{hline 16}{c TRC} {c |} Key{col 18}{c |} {c LT}{hline 16}{c RT} {c |}{space 3}{it:frequency}{col 18}{c |} {c |}{space 1}{it:row percentage}{col 18}{c |} {c BLC}{hline 16}{c BRC} How often {c |} household {c |} members go {c |} HIV status hungry {c |} HIV- HIV+ {c |} Total {hline 11}{c +}{hline 22}{c +}{hline 10} often {c |}{res} 49 30 {txt}{c |}{res} 79 {txt}{c |}{res} 62.03 37.97 {txt}{c |}{res} 100.00 {txt}{hline 11}{c +}{hline 22}{c +}{hline 10} sometimes {c |}{res} 246 134 {txt}{c |}{res} 380 {txt}{c |}{res} 64.74 35.26 {txt}{c |}{res} 100.00 {txt}{hline 11}{c +}{hline 22}{c +}{hline 10} seldom {c |}{res} 209 112 {txt}{c |}{res} 321 {txt}{c |}{res} 65.11 34.89 {txt}{c |}{res} 100.00 {txt}{hline 11}{c +}{hline 22}{c +}{hline 10} never {c |}{res} 1,841 700 {txt}{c |}{res} 2,541 {txt}{c |}{res} 72.45 27.55 {txt}{c |}{res} 100.00 {txt}{hline 11}{c +}{hline 22}{c +}{hline 10} Total {c |}{res} 2,345 976 {txt}{c |}{res} 3,321 {txt}{c |}{res} 70.61 29.39 {txt}{c |}{res} 100.00 {txt} {com}. tab alc_any hiv, row {txt} {c TLC}{hline 16}{c TRC} {c |} Key{col 18}{c |} {c LT}{hline 16}{c RT} {c |}{space 3}{it:frequency}{col 18}{c |} {c |}{space 1}{it:row percentage}{col 18}{c |} {c BLC}{hline 16}{c BRC} Any {c |} alcohol {c |} HIV status use {c |} HIV- HIV+ {c |} Total {hline 11}{c +}{hline 22}{c +}{hline 10} no {c |}{res} 1,753 670 {txt}{c |}{res} 2,423 {txt}{c |}{res} 72.35 27.65 {txt}{c |}{res} 100.00 {txt}{hline 11}{c +}{hline 22}{c +}{hline 10} yes {c |}{res} 540 281 {txt}{c |}{res} 821 {txt}{c |}{res} 65.77 34.23 {txt}{c |}{res} 100.00 {txt}{hline 11}{c +}{hline 22}{c +}{hline 10} Total {c |}{res} 2,293 951 {txt}{c |}{res} 3,244 {txt}{c |}{res} 70.68 29.32 {txt}{c |}{res} 100.00 {txt} {com}. . /* Taking advantage of the 0-1 coding of the HIV indicator, we can use the > table command get HIV prevalence by several other variables at once; here we > do just age and sex. */ . . table age_cat female, c(mean hiv) format(%8.2f) {txt}{hline 10}{c TT}{hline 11} {c |} female sex Age {c |} no yes {hline 10}{c +}{hline 11} <20 {c |} {res}0.03 0.18 {txt}20 to <30 {c |} {res}0.15 0.35 {txt}30 to <40 {c |} {res}0.40 0.42 {txt}40 to <50 {c |} {res}0.35 0.29 {txt}50+ {c |} {res}0.20 0.13 {txt}{hline 10}{c BT}{hline 11} {com}. . ******************************************************************************** . . /* We can also use the Stata means command to calculate confidence intervals > for the means of continuous variables, both overall and within strata. */ . . mean age, cformat(%8.2f) {res} {txt}Mean estimation{col 35}Number of obs{col 51}= {res} 3,387 {txt}{hline 13}{c TT}{hline 11}{hline 11}{hline 14}{hline 12} {col 14}{c |} Mean{col 26} Std. Err.{col 38} [95% Con{col 51}f. Interval] {hline 13}{c +}{hline 11}{hline 11}{hline 14}{hline 12} {space 9}age {c |}{col 14}{res}{space 2} 31.73{col 26}{space 2} 0.17{col 37}{space 5} 31.40{col 51}{space 3} 32.06 {txt}{hline 13}{c BT}{hline 11}{hline 11}{hline 14}{hline 12} {com}. mean age, over(female hiv) cformat(%8.2f) {res} {txt}Mean estimation{col 35}Number of obs{col 51}= {res} 3,387 {txt}Over: female hiv _subpop_1: {res}no HIV- {txt}_subpop_2: {res}no HIV+ {txt}_subpop_3: {res}yes HIV- {txt}_subpop_4: {res}yes HIV+ {txt}{hline 13}{c TT}{hline 11}{hline 11}{hline 14}{hline 12} {col 1} Over{col 14}{c |} Mean{col 26} Std. Err.{col 38} [95% Con{col 51}f. Interval] {hline 13}{c +}{hline 11}{hline 11}{hline 14}{hline 12} {res}age {txt}{c |} {space 3}_subpop_1 {c |}{col 14}{res}{space 2} 30.99{col 26}{space 2} 0.31{col 37}{space 5} 30.39{col 51}{space 3} 31.60 {txt}{space 3}_subpop_2 {c |}{col 14}{res}{space 2} 35.17{col 26}{space 2} 0.39{col 37}{space 5} 34.39{col 51}{space 3} 35.94 {txt}{space 3}_subpop_3 {c |}{col 14}{res}{space 2} 31.68{col 26}{space 2} 0.30{col 37}{space 5} 31.10{col 51}{space 3} 32.27 {txt}{space 3}_subpop_4 {c |}{col 14}{res}{space 2} 31.15{col 26}{space 2} 0.32{col 37}{space 5} 30.53{col 51}{space 3} 31.77 {txt}{hline 13}{c BT}{hline 11}{hline 11}{hline 14}{hline 12} {com}. . * To get, say, 90% confidence intervals, we can use the level option . mean age, over(female hiv) level(90) cformat(%8.2f) {res} {txt}Mean estimation{col 35}Number of obs{col 51}= {res} 3,387 {txt}Over: female hiv _subpop_1: {res}no HIV- {txt}_subpop_2: {res}no HIV+ {txt}_subpop_3: {res}yes HIV- {txt}_subpop_4: {res}yes HIV+ {txt}{hline 13}{c TT}{hline 11}{hline 11}{hline 14}{hline 12} {col 1} Over{col 14}{c |} Mean{col 26} Std. Err.{col 38} [90% Con{col 51}f. Interval] {hline 13}{c +}{hline 11}{hline 11}{hline 14}{hline 12} {res}age {txt}{c |} {space 3}_subpop_1 {c |}{col 14}{res}{space 2} 30.99{col 26}{space 2} 0.31{col 37}{space 5} 30.48{col 51}{space 3} 31.50 {txt}{space 3}_subpop_2 {c |}{col 14}{res}{space 2} 35.17{col 26}{space 2} 0.39{col 37}{space 5} 34.52{col 51}{space 3} 35.81 {txt}{space 3}_subpop_3 {c |}{col 14}{res}{space 2} 31.68{col 26}{space 2} 0.30{col 37}{space 5} 31.19{col 51}{space 3} 32.18 {txt}{space 3}_subpop_4 {c |}{col 14}{res}{space 2} 31.15{col 26}{space 2} 0.32{col 37}{space 5} 30.63{col 51}{space 3} 31.67 {txt}{hline 13}{c BT}{hline 11}{hline 11}{hline 14}{hline 12} {com}. . /* The means command could also be used to get means and confidence intervals > for binary variables coded 0-1, but a better approach is the ci prop command, > with the exact option. This avoids confidence limits less than 0 or more > than 1. */ . . ci prop hiv, exact {txt}{col 58}{hline 2} Binomial Exact {hline 2} Variable {c |} Obs Proportion Std. Err. [95% Conf. Interval] {hline 13}{c +}{hline 63} hiv {c |}{col 16}{res} 3,389{col 29} .2959575{col 41} .0078411{col 57} .2806231{col 69} .3116447{txt} {com}. ci prop hiv, exact level(90) {txt}{col 58}{hline 2} Binomial Exact {hline 2} Variable {c |} Obs Proportion Std. Err. [90% Conf. Interval] {hline 13}{c +}{hline 63} hiv {c |}{col 16}{res} 3,389{col 29} .2959575{col 41} .0078411{col 57} .2830462{col 69} .3091298{txt} {com}. . /* The ci command does not allow stratification, but we can get around that > using the bysort prefix: */ . . bysort female: ci prop hiv, exact {txt}{hline} -> female = no {col 58}{hline 2} Binomial Exact {hline 2} Variable {c |} Obs Proportion Std. Err. [95% Conf. Interval] {hline 13}{c +}{hline 63} hiv {c |}{col 16}{res} 1,553{col 29} .2433999{col 41} .0108895{col 57} .2222362{col 69} .2655381{txt} {hline} -> female = yes {col 58}{hline 2} Binomial Exact {hline 2} Variable {c |} Obs Proportion Std. Err. [95% Conf. Interval] {hline 13}{c +}{hline 63} hiv {c |}{col 16}{res} 1,836{col 29} .3404139{col 41} .0110587{col 57} .3187364{col 69} .3626026{txt} {com}. . * The ci command also works for continous and Poisson-distributed count varibles . ci means age {txt} Variable {c |} Obs Mean Std. Err. [95% Conf. Interval] {hline 13}{c +}{hline 63} age {c |}{col 16}{res} 3,387{col 29} 31.73267{col 41} .1691974{col 57} 31.40093{col 69} 32.06441{txt} {com}. ci means n_adults, poisson {txt}{col 58}{hline 2} Poisson Exact {hline 2} Variable {c |} Exposure Mean Std. Err. [95% Conf. Interval] {hline 13}{c +}{hline 63} n_adults {c |}{col 17}{res} 3384{col 29} 1.664598{col 41} .0221789{col 57} 1.621409{col 69} 1.708646{txt} {com}. . ******************************************************************************** . . /* Exploratory data analysis using graphical means. First make a histogram of > the age distribution. We can use the name option so that multiple graphs can > be shown in the viewer. The replace option is needed to prevent Stata from > stopping if the graph already exists -- again, this is useful if you are > rerunning a do-file to debug it. */ . . histogram age, fcolor(blue) name(hist_age_00, replace) {txt}(bin={res}35{txt}, start={res}17.52{txt}, width={res}1.7808571{txt}) {res}{txt} {com}. . /* Now add titles to the histogram. */ . . histogram age, fcolor(blue) title(Histogram of age) /// > xtitle(Age) name(hist_age_01, replace) {txt}(bin={res}35{txt}, start={res}17.52{txt}, width={res}1.7808571{txt}) {res}{txt} {com}. . /* By default, the Y-axis shows the "density", which sums to one for all bars. > Alternatively, we could get a histogram of frequencies. */ . . histogram age, fcolor(blue) frequency /// > title(Histogram of age) xtitle(Age) name(hist_age_02, replace) {txt}(bin={res}35{txt}, start={res}17.52{txt}, width={res}1.7808571{txt}) {res}{txt} {com}. . /* Now combine the two histograms into a single plot. Note that he histograms > are identical in shape, and both show that age is somewhat skewed to the right. */ . . graph combine hist_age_01 hist_age_02, name(hist_age_combined, replace) {res}{txt} {com}. . /* To save this as a PDF file, we can use the following command. Other options > include saving as png, wmf, tif, eps (encapsulated postscript), and svg > (scalable vector graphics), which some journals prefer. See help graph export > for more information. */ . . graph export hist_age_combined.pdf, replace {txt}(file /Users/eric/Work/MB/ATCR/200/Labs/hist_age_combined.pdf written in PDF format) {com}. . /* Now we will change the number of bars in the histogram, leaving Stata to > decide on their widths. > > We will do this using "loops." Specifically, the program will loop over the 4 > values of nbins (i.e., # of bins) in the first line, pasting those values as > so-called local variables `nbins' into the histogram command in several places. > Learning to use loops can make programming a lot less tedious. You will need > to run the four lines of code defining the loop together. */ . . foreach nbins in 5 10 25 50 {c -(} {txt} 2{com}. histogram age, fcolor(blue) percent bin(`nbins') /// > title("`nbins' bins") xtitle(Age) name(hanb`nbins', replace) {txt} 3{com}. {c )-} {txt}(bin={res}5{txt}, start={res}17.52{txt}, width={res}12.466{txt}) {res}{txt}(bin={res}10{txt}, start={res}17.52{txt}, width={res}6.233{txt}) {res}{txt}(bin={res}25{txt}, start={res}17.52{txt}, width={res}2.4932{txt}) {res}{txt}(bin={res}50{txt}, start={res}17.52{txt}, width={res}1.2466{txt}) {res}{txt} {com}. graph combine hanb5 hanb10 hanb25 hanb50, name(hist_age_bins, replace) {res}{txt} {com}. . /* As an alternative, we can vary the width of the bins, again using a loop. */ . . foreach bw in 1 2 5 10 {c -(} {txt} 2{com}. histogram age, fcolor(blue) percent width(`bw') /// > title(bin width `bw') xtitle(Age) name(habw`bw', replace) {txt} 3{com}. {c )-} {txt}(bin={res}63{txt}, start={res}17.52{txt}, width={res}1{txt}) {res}{txt}(bin={res}32{txt}, start={res}17.52{txt}, width={res}2{txt}) {res}{txt}(bin={res}13{txt}, start={res}17.52{txt}, width={res}5{txt}) {res}{txt}(bin={res}7{txt}, start={res}17.52{txt}, width={res}10{txt}) {res}{txt} {com}. graph combine habw1 habw2 habw5 habw10, name(hist_age_widths, replace) {res}{txt} {com}. . ******************************************************************************** . . /* Box plots are also useful for assessing the distribution of continuous > variables. The box runs from the 25th to the 75th percentile, and includes a > line at the median. The so-called whiskers extend 1.5 IQRs from the bottom > and top of the box, or to the most extreme value, whichever is closer to the > box. Any outliers more than 1.5 IQRs from the box are plotted separately. */ . . graph box age, name(box_age, replace) {res}{txt} {com}. . /* The boxplot also shows that age is somewhat right skewed, as we saw in the > histograms. > > We can stratify the boxplot by up to three other factors. Note that the value > labels we added in lab 1 to sex and hiv make this plot easier to understand */ . . graph box age, over(sex) over(hiv) name(box_age_stratified, replace) {res}{txt} {com}. . /* Finally, Q-Q (quantile-quantile) plots are a good way to assess Normality. > We will use a loop to look at them within groups, using some new commands to > control the loops and title the plots: */ . . qui levelsof sex, local(sl) // extracts distinct values of sex {txt} {com}. foreach i in `sl' {c -(} {txt} 2{com}. local sex : label(sex) `i' // gets value label for level i of sex {txt} 3{com}. forvalues j = 0/1 {c -(} {txt} 4{com}. local stat : label(hiv) `j' // gets value label for level j of hiv {txt} 5{com}. qnorm age if sex==`i' & hiv==`j', /// > title("`stat' `sex'") name(qq_age`i'`j', replace) {txt} 6{com}. {c )-} {txt} 7{com}. {c )-} {res}{txt} {com}. graph combine qq_age10 qq_age11 qq_age20 qq_age21, name(qq_age, replace) {res}{txt} {com}. . /* With the exception of HIV positive men, these plots all show the same right > skewness we observed in the box plots and histograms. */ . . ******************************************************************************** . . /* Now we will use scatter plots to look at the association of a continuous > outcome with a continuous predictor. */ . . scatter n_support age, name(scatter_age, replace) {res}{txt} {com}. . /* Now we add a Lowess smooth through the data. Essentially this is a regression > of n_support on age, without making the assumption that the regression line is linear. The bwidth option controls the smoothness of the line. The default is > 0.8, which means that Stata uses the nearest 80% of the data to determine the > vertical location of the smooth at each value of the independent variable. The > lineopts() option allows us to control the thickness of the line, and could > also be used to reset its color and whether the line is solid, dashed, etc. > > We will again using loops to explore the bandwidth. The looping variable is a > percentage, because the plot name can't include decimals. This means that we > have to use some arcane code to convert `pct' to a proportion for use in the > bw() option in the lowess command. Note that the first line of the loop uses > a somewhat different syntax than the previous loops. */ . . foreach pct of numlist 80(-20)20 10 1 {c -(} {txt} 2{com}. lowess n_support age, /// > bwidth(`=`pct'/100') msize(tiny) lineopts(lwidth(medthick)) title("") /// > name(bw`pct', replace) {txt} 3{com}. {c )-} {res}{txt} {com}. graph combine bw80 bw60 bw40 bw20 bw10 bw1, name(lowess_combined, replace) {res}{txt} {com}. . /* Reducing the bandwith imposes less smoothness, but clearly the smallest > bandwidth is going too far! > > We can also focus on a certain age range by adding an if statement. */ . . lowess n_support age if age<60, /// > msize(tiny) lineopts(lwidth(medthick)) title("") /// > name(lowess_restricted, replace) {res}{txt} {com}. . /* Finally, we can use tabstat and box plots to look at the conditional > distribution of a continuous outcome when the independent variable is > categorical. */ . . tabstat n_support, by(hungry) stat(n mean sd min p25 p50 p75 max) format(%8.3g) {txt}Summary for variables: n_support {col 6}by categories of: hungry (How often household members go hungry) {ralign 9:hungry} {...} {c |} N mean sd min p25 p50 p75 {hline 10}{c +}{hline 70} {ralign 9:often} {...} {c |}{...} {res} 77 4.38 3.36 0 1 4 6 {txt}{ralign 9:sometimes} {...} {c |}{...} {res} 378 4.18 3.18 0 2 4 6 {txt}{ralign 9:seldom} {...} {c |}{...} {res} 319 4.2 3.34 0 2 4 6 {txt}{ralign 9:never} {...} {c |}{...} {res} 2530 3.81 3.21 0 1 3 5 {txt}{hline 10}{c +}{hline 70} {ralign 9:Total} {...} {c |}{...} {res} 3304 3.91 3.22 0 1 3 5 {txt}{hline 10}{c BT}{hline 70} {ralign 9:hungry} {...} {c |} max {hline 10}{c +}{hline 10} {ralign 9:often} {...} {c |}{...} {res} 13 {txt}{ralign 9:sometimes} {...} {c |}{...} {res} 16 {txt}{ralign 9:seldom} {...} {c |}{...} {res} 20 {txt}{ralign 9:never} {...} {c |}{...} {res} 20 {txt}{hline 10}{c +}{hline 10} {ralign 9:Total} {...} {c |}{...} {res} 20 {txt}{hline 10}{c BT}{hline 10} {com}. . graph box n_support, over(hungry) name(ins_alc, replace) {res}{txt} {com}. . /* Now close the log file.*/ . . log close {txt}name: {res} {txt}log: {res}/Users/eric/Work/MB/ATCR/200/Labs/lab 1.smcl {txt}log type: {res}smcl {txt}closed on: {res}18 Sep 2018, 06:54:49 {txt}{.-} {smcl} {txt}{sf}{ul off}