capture log close quietly log using lecture7.smcl, replace cd "/Users/kristenaiemjoy/Box Sync/_Teaching/Biostat 212/Biostat212.2018/Lectures/7.Programming" set more off /*** #Lecture 7: live coding ***/ /*** open nhanes data set ***/ webuse nhanes2, clear /*** ## Stored results most stata commands store results so they can subsequently be used by other commands or programs. commands are r-class, e-class or n-class * r-class, like summarize, type "return list" after * n-class, like generate, do not store results * e-class, like logistic, type "ereturn list" after ***/ sum age return list display "Standard Error = " r(sd)/sqrt(r(N)) display "Standard Error = " %9.2f (r(sd)/sqrt(r(N))) **lets look at the relationship between sex and heart attack tab sex heartatk numlabel, add tab sex heartatk *change sex to 0 and 1 recode sex 2=0 label define sexlabel 0 "Female" 1 "Male", replace label values sex sexlabel tab sex heartatk, row chi2 return list display "p-value = " (r(p)) display "p-value = " %9.5f (r(p)) *look at risk ratio and odds ratio cs heartatk sex, or logit heartatk sex ereturn list display "Odds Ratio = " exp(_b[sex]) display "Odds Ratio = " %9.2f exp(_b[sex]) //format logit heartatk sex age display "Odds Ratio (adjusted for age) = " exp(_b[sex]) logit heartatk sex age, or binreg heartatk sex age, rr display "Risk Ratio (adjusted for age) = " exp(_b[sex]) binreg heartatk sex age, rr /*** ##Macros A macro is simply a name associated with some text or a number. It can hold a stored/returned value after running a command or any text/number your choose. 1. locals 2. globals 3. scalars ***/ /*** ### Locals Local macros have names of up to 31 characters and are known only in the current context (the console, a do file, or a program). You define a local macro using local name [=] text and you evaluate it using `name'. (Note the use of a backtick or left quote.) The second type of macro definition with an equal sign is used to store results. It instructs Stata to treat the text on the right hand side as an expression, evaluate it, and store a text representation of the result under the given name. ***/ regress bpsystol age local rsq1 e(r2) local rsq2 = e(r2) di `rsq1' // this has the current R-squared di `rsq2' // as does this regress bpsystol age weight di `rsq1' // the formula has the new R-squared di `rsq2' // this has the old R-squared /*** ### Globals Global macros have names of up to 32 characters and are visible everywhere in Stata (not only in your .do file) You define a global macro using global name [=] text and evaluate it using $name. Most programmers prefer not to use globals because you can accidently write over the name of another function/program used in Stata. You call globals with the $ sign ***/ global myName "Kristen" di "$myName" /*** ### Scalars Scalars store numbers with more precision. Scalars are stroed in the global space, so you have to be careful not to overwrite program=/function names ***/ sum age scalar SE = r(sd)/ sqrt(r(N)) di SE scalar MEAN = r(mean) di MEAN scalar CIL = MEAN - 1.96*SE scalar CIU = MEAN +1.96*SE di "95% CI:" CIL "-" CIU /*** ##Matrices To Stata, a matrix is a named entity containing an r × c rectangular array of double-precision numbers (including missing values) that is bordered by a row and a column of names. ***/ tabstat zinc copper vitaminc iron, by(diabetes) stat(n mean sd) save label define diabetes_label 1 "Diabetics" 0 "Non diabetics" label values diabetes diabetes_label tabstat zinc copper vitaminc iron, by(diabetes) stat(n mean sd) save return list matrix list r(StatTotal) matrix results1 = r(StatTotal) matrix list results1 /*** Save and display in Excel table ***/ putexcel set lecture7.xlsx, replace //open an new blank excel table putexcel A1 = matrix(results1), names nformat(number_d2) //xport matrix to excel table /*** Save and display in MarcDoc table ***/ matrix stats1 = r(Stat1) matrix stats2 = r(Stat2) local d_n : display %9.0f stats2[1,1] local d_z : display %9.2f stats2[2,1] "("%9.1f stats1[3,1] ")" local d_c : display %9.2f stats2[2,2] "("%9.1f stats1[3,2] ")" local d_v : display %9.2f stats2[2,3] "("%9.1f stats1[3,3] ")" local d_i : display %9.2f stats2[2,4] "("%9.1f stats1[3,4] ")" local nd_n : display %9.0f stats1[1,1] local nd_z : display %9.2f stats1[2,1] "("%9.1f stats1[3,1] ")" local nd_c : display %9.2f stats1[2,2] "("%9.1f stats1[3,2] ")" local nd_v : display %9.2f stats1[2,3] "("%9.1f stats1[3,3] ")" local nd_i : display %9.2f stats1[2,4] "("%9.1f stats1[3,4] ")" tbl ("Mean (SD) ", "N", "Zinc", "Copper", "Vitamin C", "Iron" \ /// "_Diabetics_", `d_n', `d_z', `d_c', `d_v', `d_i' \ /// "_Non diabetics_", `nd_n', `nd_z', `nd_c', `nd_v', `nd_i') /// , title(Table 1: Serum Zinc, Copper, Vitamin C and Iron levels (mcg/DL) for Diabetics and Non-Diabetics in NHANES) /*** ##Loops Loops are used to do repetitive tasks. Stata has commands that allow looping over sequences of numbers and various types of lists, including lists of variables. ***/ foreach var of varlist zinc copper vitaminc iron { summarize `var' local `var'_n : display r(N) local `var'_mean : display %9.2f r(mean) local `var'_sd : display %9.2f r(sd) } tbl ("Variable Name", "N", "Mean", "SD" \ /// "_Zinc_",`zinc_n',`zinc_mean', `zinc_sd' \ /// "_Copper_", `copper_n',`copper_mean', `copper_sd' \ /// "_Vitamin C_",`vitaminc_n',`vitaminc_mean', `vitaminc_sd' \ /// "_Iron_", `iron_n',`iron_mean', `iron_sd') /// , title("Table 1. Summary of _Zinc_, _Copper_, _Vitamin C_, and _Iron_") /*** ##Programs Ch. 18 of Stata User guide (V15) has a lot of useful information and examples. (On course website) Define program: program [define] pgmname [, [ nclass | rclass | eclass | sclass ] byable(recall[, noheader] | onecall) properties(namelist) sortpreserve plugin] List names of programs stored in memory: program dir Eliminate program from memory: program drop ***/ program define hello di "Hello Kristen" end hello program define median, rclass quietly summarize `1', detail local median = r(p50) display "median = " %9.2f `median' end median height program meanse, rclass quietly summarize `1' local mean = r(mean) local sem = sqrt(r(Var)/r(N)) display " mean = " %9.2f `mean' display "SE of mean = " %9.2f `sem' end meanse height markdoc lecture7, replace statax export(html) toc