{smcl} {com}{sf}{ul off}{txt}{.-} name: {res} {txt}log: {res}/Users/eric/Work/MB/ATCR/200/Labs/lab 2.smcl {txt}log type: {res}smcl {txt}opened on: {res} 8 Jul 2018, 08:54:44 {txt} {com}. . * Read in the lab1 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_revised.dta, clear {txt} {com}. . * Briefly remind yourself of what is in the dataset. 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_revised.dta {txt} obs:{res} 3,389 {txt} vars:{res} 15 6 Jul 2018 08:15 {txt} size:{res} 77,947 {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} {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 that to sum 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.72456 9.858456 17.5 80.07 {txt}{space 5}age_cat {c |}{res} 3,387 2.653971 .9791769 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} {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} 17.97 17.5 {txt} 5% {res} 19.66 17.5 {txt}10% {res} 21.07 17.53 {txt}Obs {res} 3,387 {txt}25% {res} 24.22 17.56 {txt}Sum of Wgt. {res} 3,387 {txt}50% {res} 29.51 {txt}Mean {res} 31.72456 {txt}Largest Std. Dev. {res} 9.858456 {txt}75% {res} 37.66 75.22 {txt}90% {res} 45.91 75.36 {txt}Variance {res} 97.18915 {txt}95% {res} 50.39 77.84 {txt}Skewness {res} 1.029554 {txt}99% {res} 60.04 80.07 {txt}Kurtosis {res} 3.972486 {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 also 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.86 266 {txt}{ralign 8:min} {...} {c |}{...} {res} 17.5 1 {txt}{ralign 8:p25} {...} {c |}{...} {res} 24.2 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} 80.1 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.86 17.5 24.2 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} 80.1 {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.3 28.3 37.5 80.1 {space 6} {...} {txt}{c |}{...} {res} 0 . . . . . . . {txt}{hline 7}{c +}{hline 80} {ralign 6:HIV+} {...} {c |}{...} {res} 1003 32.6 8.06 17.5 26.8 31.2 38 70.2 {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.86 17.5 24.2 29.5 37.7 80.1 {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}35.2 32.3 34.0 34.9 29.9 32.6 31.9 31.8 {txt}HIV+ {c |} {res}37.7 32.5 33.4 31.0 39.6 34.2 35.5 31.4 {txt}{hline 11}{c +}{hline 53} Moslem {c |} HIV- {c |} {res}33.6 38.8 29.1 34.1 32.6 33.4 31.3 31.2 {txt}HIV+ {c |} {res}33.5 36.3 33.8 29.8 33.8 31.1 34.1 30.4 {txt}{hline 11}{c +}{hline 53} 5 {c |} HIV- {c |} {res}24.8 34.2 30.1 28.4 26.2 32.4 29.5 30.3 {txt}HIV+ {c |} {res}38.1 27.1 35.9 30.6 31.8 32.6 35.8 30.8 {txt}{hline 11}{c +}{hline 53} 6 {c |} HIV- {c |} {res}37.4 36.3 29.9 33.9 27.8 31.1 31.0 31.2 {txt}HIV+ {c |} {res}44.5 33.3 35.7 33.0 38.6 31.6 34.9 30.1 {txt}{hline 11}{c +}{hline 53} Other {c |} HIV- {c |} {res} 28.0 28.1 26.1 23.7 29.5 31.4 {txt}HIV+ {c |} {res} 40.0 38.6 46.3 37.1 34.9 32.7 {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} 191 22 {txt}{c |}{res} 213 {txt}{c |}{res} 89.67 10.33 {txt}{c |}{res} 100.00 {txt}{hline 11}{c +}{hline 22}{c +}{hline 10} 20 to <30 {c |}{res} 1,164 419 {txt}{c |}{res} 1,583 {txt}{c |}{res} 73.53 26.47 {txt}{c |}{res} 100.00 {txt}{hline 11}{c +}{hline 22}{c +}{hline 10} 30 to <40 {c |}{res} 563 381 {txt}{c |}{res} 944 {txt}{c |}{res} 59.64 40.36 {txt}{c |}{res} 100.00 {txt}{hline 11}{c +}{hline 22}{c +}{hline 10} 40 to <50 {c |}{res} 306 151 {txt}{c |}{res} 457 {txt}{c |}{res} 66.96 33.04 {txt}{c |}{res} 100.00 {txt}{hline 11}{c +}{hline 22}{c +}{hline 10} 50+ {c |}{res} 160 30 {txt}{c |}{res} 190 {txt}{c |}{res} 84.21 15.79 {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.01 0.18 {txt}20 to <30 {c |} {res}0.16 0.35 {txt}30 to <40 {c |} {res}0.39 0.42 {txt}40 to <50 {c |} {res}0.38 0.29 {txt}50+ {c |} {res}0.19 0.12 {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.72{col 26}{space 2} 0.17{col 37}{space 5} 31.39{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} 31.00{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.16{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.67{col 26}{space 2} 0.30{col 37}{space 5} 31.08{col 51}{space 3} 32.25 {txt}{space 3}_subpop_4 {c |}{col 14}{res}{space 2} 31.13{col 26}{space 2} 0.32{col 37}{space 5} 30.51{col 51}{space 3} 31.75 {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} 31.00{col 26}{space 2} 0.31{col 37}{space 5} 30.49{col 51}{space 3} 31.50 {txt}{space 3}_subpop_2 {c |}{col 14}{res}{space 2} 35.16{col 26}{space 2} 0.39{col 37}{space 5} 34.51{col 51}{space 3} 35.81 {txt}{space 3}_subpop_3 {c |}{col 14}{res}{space 2} 31.67{col 26}{space 2} 0.30{col 37}{space 5} 31.17{col 51}{space 3} 32.16 {txt}{space 3}_subpop_4 {c |}{col 14}{res}{space 2} 31.13{col 26}{space 2} 0.32{col 37}{space 5} 30.61{col 51}{space 3} 31.65 {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.72456{col 41} .1693953{col 57} 31.39243{col 69} 32.05669{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.5{txt}, width={res}1.7877143{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.5{txt}, width={res}1.7877143{txt}) {res}{txt} {com}. . /* By default, the Y-axis shows the "density", which sums to one for all bars. Instead we can 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.5{txt}, width={res}1.7877143{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 nb in the first line, pasting those values as so-called local variables `nb' 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 nb in 5 10 25 50 {c -(} {txt} 2{com}. histogram age, fcolor(blue) percent bin(`nb') /// > title(`nb' bins) xtitle(Age) name(hanb`nb', replace) {txt} 3{com}. {c )-} {txt}(bin={res}5{txt}, start={res}17.5{txt}, width={res}12.514{txt}) {res}{txt}(bin={res}10{txt}, start={res}17.5{txt}, width={res}6.257{txt}) {res}{txt}(bin={res}25{txt}, start={res}17.5{txt}, width={res}2.5028{txt}) {res}{txt}(bin={res}50{txt}, start={res}17.5{txt}, width={res}1.2514{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.5{txt}, width={res}1{txt}) {res}{txt}(bin={res}32{txt}, start={res}17.5{txt}, width={res}2{txt}) {res}{txt}(bin={res}13{txt}, start={res}17.5{txt}, width={res}5{txt}) {res}{txt}(bin={res}7{txt}, start={res}17.5{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 continuousvariables. 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 to sex and hiv make this plot easier to understand */ . . graph box age, over(female) over(hiv) name(box_age_stratified, replace) {res}{txt} {com}. . /* 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. Here, we have to define a second local variable for the bandwidth, based on looping variable pct, because the plot name can't include decimals. 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}. local bw = `pct'/100 {txt} 3{com}. lowess n_support age, /// > bwidth(`bw') msize(tiny) lineopts(lwidth(thick)) title("") /// > name(bw`pct', replace) {txt} 4{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 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 2.smcl {txt}log type: {res}smcl {txt}closed on: {res} 8 Jul 2018, 08:55:09 {txt}{.-} {smcl} {txt}{sf}{ul off}