{smcl} {com}{sf}{ul off}{txt}{.-} name: {res} {txt}log: {res}/Users/eric/Work/MB/ATCR/200/Labs/Lab 1 exercises 2018.smcl {txt}log type: {res}smcl {txt}opened on: {res} 5 Jul 2018, 05:33:02 {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 Uganda. */ . . use lab1.dta, clear {txt} {com}. . /* We will 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 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 sex "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). */ . . label define sex 1 "Male" 2 "Female" {txt} {com}. label values sex sex {txt} {com}. label define hiv 0 "HIV-" 1 "HIV+" {txt} {com}. label values hiv hiv {txt} {com}. . * Stata lets us look at a spreadsheet of the data . . browse {txt} {com}. . /* Exploratory data analysis -- descriptive statistics. > > First see what variables are in the dataset. Check how many observations > and variables the dataset includes. */ . . describe {txt}Contains data from {res}lab1.dta {txt} obs:{res} 3,389 {txt} vars:{res} 12 4 Jul 2018 08:10 {txt} size:{res} 44,057 {txt}{hline} storage display value variable name type format label variable label {hline} {p 0 48}{res}{bind:age }{txt}{bind: byte }{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}AUDITC, past 3 mos{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: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}Sex{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.72808 9.850006 18 80 {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 9}hiv {c |}{res} 3,389 .2959575 .4565393 0 1 {txt}{hline 13}{c +}{hline 57} {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}{space 3}n_support {c |}{res} 3,372 3.907177 3.227549 0 20 {txt}{hline 13}{c +}{hline 57} {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}. . /* 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 18 {txt} 5% {res} 20 18 {txt}10% {res} 21 18 {txt}Obs {res} 3,387 {txt}25% {res} 24 18 {txt}Sum of Wgt. {res} 3,387 {txt}50% {res} 30 {txt}Mean {res} 31.72808 {txt}Largest Std. Dev. {res} 9.850006 {txt}75% {res} 38 75 {txt}90% {res} 46 75 {txt}Variance {res} 97.02261 {txt}95% {res} 50 78 {txt}Skewness {res} 1.030799 {txt}99% {res} 60 80 {txt}Kurtosis {res} 3.975972 {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}. . /* 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.85 266 {txt}{ralign 8:min} {...} {c |}{...} {res} 18 1 {txt}{ralign 8:p25} {...} {c |}{...} {res} 24 130 {txt}{ralign 8:p50} {...} {c |}{...} {res} 30 283 {txt}{ralign 8:p75} {...} {c |}{...} {res} 38 463 {txt}{ralign 8:max} {...} {c |}{...} {res} 80 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 18 24 30 38 {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 {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 18 23 28 38 80 {space 6} {...} {txt}{c |}{...} {res} 0 . . . . . . . {txt}{hline 7}{c +}{hline 80} {ralign 6:HIV+} {...} {c |}{...} {res} 1003 32.6 8.05 18 27 31 38 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 18 24 30 38 80 {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 stratify by one by variable at a time; to > stratify by >1 variables, use the table command.*/ . table hiv sex hungry, by(religion) c(mean age) format(%8.1f) {txt}{hline 11}{c TT}{hline 69} Religion {c |} How often household members go hungry and Sex and HIV {c |} {hline 4} Often {hline 3} {hline 2} Sometimes {hline 1} {hline 3} Seldom {hline 3} {hline 4} Never {hline 3} status {c |} Male Female Male Female Male Female Male Female {hline 11}{c +}{hline 69} Protestant {c |} HIV- {c |} {res}35.0 32.2 33.9 34.9 29.9 32.6 31.9 31.9 {txt}HIV+ {c |} {res}38.0 32.4 33.4 31.0 39.6 34.2 35.5 31.4 {txt}{hline 11}{c +}{hline 69} Catholic {c |} HIV- {c |} {res}33.5 38.7 29.2 34.2 32.5 33.4 31.3 31.2 {txt}HIV+ {c |} {res}33.7 36.3 33.9 29.8 33.8 31.1 34.1 30.5 {txt}{hline 11}{c +}{hline 69} Saved {c |} HIV- {c |} {res}24.5 34.4 30.1 28.4 26.1 32.4 29.5 30.3 {txt}HIV+ {c |} {res}38.0 27.0 36.0 30.6 31.7 32.4 35.8 30.9 {txt}{hline 11}{c +}{hline 69} Moslem {c |} HIV- {c |} {res}37.7 36.2 30.0 34.0 27.7 31.2 31.1 31.2 {txt}HIV+ {c |} {res}44.0 33.4 35.9 32.9 38.5 31.8 34.9 30.1 {txt}{hline 11}{c +}{hline 69} Other {c |} HIV- {c |} {res} 28.0 28.0 26.2 24.0 29.5 31.3 {txt}HIV+ {c |} {res} 40.0 39.0 46.0 37.0 34.8 32.7 {txt}{hline 11}{c BT}{hline 69} {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}18{txt}, width={res}1.7714286{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}18{txt}, width={res}1.7714286{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}18{txt}, width={res}1.7714286{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}. . * save this as a PDF file . 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 your programming a lot less tedious. Recall that 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}18{txt}, width={res}12.4{txt}) {res}{txt}(bin={res}10{txt}, start={res}18{txt}, width={res}6.2{txt}) {res}{txt}(bin={res}25{txt}, start={res}18{txt}, width={res}2.48{txt}) {res}{txt}(bin={res}50{txt}, start={res}18{txt}, width={res}1.24{txt}) {res}{txt} {com}. graph combine hanb5 hanb10 hanb25 hanb50, name(hist_age_bins, replace) {res}{txt} {com}. graph export hist_age_bins.pdf, replace {txt}(file /Users/eric/Work/MB/ATCR/200/Labs/hist_age_bins.pdf written in PDF format) {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}62{txt}, start={res}18{txt}, width={res}1{txt}) {res}{txt}(bin={res}31{txt}, start={res}18{txt}, width={res}2{txt}) {res}{txt}(bin={res}13{txt}, start={res}18{txt}, width={res}5{txt}) {res}{txt}(bin={res}7{txt}, start={res}18{txt}, width={res}10{txt}) {res}{txt} {com}. graph combine habw1 habw2 habw5 habw10, name(hist_age_widths, replace) {res}{txt} {com}. graph export hist_age_widths.pdf, replace {txt}(file /Users/eric/Work/MB/ATCR/200/Labs/hist_age_widths.pdf written in PDF format) {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 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}. graph export box_age_stratified.pdf, replace {txt}(file /Users/eric/Work/MB/ATCR/200/Labs/box_age_stratified.pdf written in PDF format) {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 up to 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 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}. graph export lowess_combined.pdf, replace {txt}(file /Users/eric/Work/MB/ATCR/200/Labs/lowess_combined.pdf written in PDF format) {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(thick)) title("") /// > name(lowess_restricted, replace) {res}{txt} {com}. graph export lowess_restricted.pdf, replace {txt}(file /Users/eric/Work/MB/ATCR/200/Labs/lowess_restricted.pdf written in PDF format) {com}. . /* Finally we can also 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}. graph export ins_alc.pdf, replace {txt}(file /Users/eric/Work/MB/ATCR/200/Labs/ins_alc.pdf written in PDF format) {com}. . /* Now close the log file.*/ . . log close {txt}name: {res} {txt}log: {res}/Users/eric/Work/MB/ATCR/200/Labs/Lab 1 exercises 2018.smcl {txt}log type: {res}smcl {txt}closed on: {res} 5 Jul 2018, 05:33:30 {txt}{.-} {smcl} {txt}{sf}{ul off}