* BIOSTAT 212: Introduction to Statistical Computing in Clinical Research * SUMMER 2018, TICR * Commands used in lecture ****************************************************************************** * SESSION 1: Introduction, setting up .do files, immediate commands, basic exploring ****************************************************************************** clear all //clears all open data, variables, labels, matrices, memory and close all open files, graph windows, etc. pwd // 'print working directory.' use it to check the current working directory. cd "/folder/path/here" //change working directory to where you want to save/import files from capture log close // Close any existing log files log using lab1.smcl//open a log file log using lab1.smcl, replace //open an exisitng log file and replace it log using lab1.smcl, append //open an existing log file and append it help dta_examples //information of built-in "system" data sets sysuse "filename.dta" // open a built-in "system" like cancer.dta sysuse "filename.dta", clear // open a built-in "system" like cancer.dta and clear out any open data sets di //use stata like a calculator di log(125) di 3+4 di 10000/8 di runiform(1,1000) //random number between 1 and a 1000 browse // browse data in data editor br // browse shorthand browse "varlist" //browse specific variables describe // describes dataset and variables des //shorthand summarize //summarize data set (CONTINUOUS VARIABLES) sum //shorthand sum "varlist" //summarize specific variables count // counts the number of observations tabulate "varname" // tabulate 1 variable (CATEGORICAL VARIBALES) tab //shorthand tab "varname1" "varname2" // tabulate 2 variables tab "varname1" "varname2", row // tabulate 2 variables with row percentages tab "varname1" "varname2", col // tabulate 2 variables with column percentages log close //save and close log file ****************************************************************************** * SESSION 2: Exploring ****************************************************************************** webuse "datasetname" //download dataset from the web ds // check out variable names only db "command" //open dialog box for a command db tabulate //for example list "varname1" "varname2" //list specific variables list in 1/3 //list first 3 variables list in -50/l //list last 50 variables gsort- "varname" // sort descending gsort+ "varname" // sort ascending codebook "varname" // shows the contents of the variable, range inspect "varname" //shows mini histogram summarize //summarize data set (CONTINUOUS VARIABLES) sum //shorthand sum "varlist" //summarize specific variables sum "varname", detail //detailed summary with median and percentiles bysort "varname1": sum "varname2" //summarize variable2 by a categorical variable1 duplicates report //report any duplicate observations duplicates report "varname" //report duplicates for a specific variable isid "varname" //does varname uniquely identify variables. no return value means "yes" ****************************************************************************** * SESSION 3: Data cleaning ****************************************************************************** import delimited acupuncture_raw.csv, clear //Import "raw" data **change variable names rename "old_varname" "new_varname" rename f1 hfreq_bl //example *Add variable label label variable "varname" "labelname (can have spaces)" label variable hfreq_bl "headache frequency baseline" //example *Add value labels label define "labelname" "#" "label" "#" "label" … label values "varname" "labelname" label define grouplabel 1 intervention 0 control //example label values group grouplabel //example **check for outliers codebook "varname" inspect "varname" histogram "varname" summarize "varname" spikeplot "varname" spikeplot age //example *Replace outliers replace "varname" = "#/character" if "condition" replace age = 52 if id==363 //example **missing data misstable sum tab "varname", missing *convert alternative missing data sorage types (-99) to stata missing datat format (.) mvdecode _all, mv(-99) //replace in entire data set, change -99 to however missing datat5 is imported mvdecode varname, mv(-99) //replace for specific variable **creating new variables generate "new_varname" = "exp" gen newid = _n //generate running id gen blank = . //generate blank vairable generate "new_varname"=("condition") //generate new binary variable. 1 if meets condition 0 if does not meet condition gen severemigraine = (migraine==1 & basehs>50) //example xtile "new_varname" = "varname" //generate quantile from a continuous variable xtile hs_quartile = basehs, nq(4) //example (quartiles of baseline headache score) egen "new_varname" = "function(varname)" //Complex function generations egen meanhs = rowmean(basehs posths oneyrhs) //example new variable that is mean of basehs posths and oneyrhs recode "old_varname" (min/# = 0 "value label")(#/# = 1 "value label")(#/max = 2 "value label"), gen("new_varname") //generate categorical variable from continuous recode age (min/49 = 1 "less than 50") (50/max = 2 "50 and older"), gen(age2) //example **convert string to numeric destring "varname", generate("new_varname") //convert string into numeric variables encode "varname", generate("new_varname") //Encode string into numeric variable with labels encode withdrawal_reason, gen(dropout) //example **drop/keep observations drop if "condition" keep if "condition" drop if age>100 //example ****************************************************************************** * SESSION 4: Figures ****************************************************************************** *Histogram sysuse sp500 histogram volume, frequency *Bar Chart sysuse nlsw88, clear graph hbar wage, over(occ, sort(1)) by(union) *Box plot sysuse bplong graph box bp, over(when) over(sex) *Dot plot webuse dotgr dotplot g1r1-g1r10, title("Tumor volume, cu mm") *Contour plot/ heat map sysuse sandstone twoway contour depth northing easting, level(10) scolor(green) ecolor(red) *Scatter plot sysuse uslifeexp2 scatter le year, connect(l) *Scatter + linear fit sysuse auto, clear twoway lfitci mpg weight, stdf || scatter mpg weight || , xscale(log) *combining plots websute nhanes2 hist zinc, name (zinc_plot) hist copper, name (cop_plot) gramph combine zinc_plot cop_plot *Lecture example_ Figure 1 use "acupuncture.dta", clear twoway /// scatter posths acuptreatments if migraine==0 /// , msize(tiny) mcolor(orange) || /// lfit posths acuptreatments if migraine==0 /// , lcolor(orange) || /// scatter posths acuptreatments if migraine==1 /// , msize(tiny) mcolor(green) || /// lfit posths acuptreatments if migraine==1 /// , lcolor(green) || /// , title (Figure 1:Acupuncture treatments and headache score) /// note (Lecture 7 practice) /// scheme(s1mono) /// ytitle(Post-study headache score) /// xtitle(Number of acupuncture treatments) /// legend(order(2 "No migraine" 4 "Migraine")) /// saving("figure1.gph", replace) *Lecture example_ Figure 1 use acupuncture.dta, clear twoway /// kdensity posths if no_acu==0 /// , lcolor(gray) || /// kdensity posths if no_acu==1 /// , lcolor(black) || /// , yscale(off) /// graphregion(fcolor(white) lcolor(none)) /// legend(order(1 "1 or more acupuncture treatments" 2 "No acupuncture treatments")size(small)) /// ytitle("Density") /// xtitle("Post-study headache score") /// title(Figure 2: Distribution of headache score) /// saving("figure2.gph", replace) ****************************************************************************** * SESSION 5: Stats ****************************************************************************** numlabel, add //add underlying number to categorical variabes *Descriptive statistics sysuse auto, clear tabstat price weight mpg rep78, by(foreign) stat(mean sd min max) nototal long col(stat) use acupuncture.dta, clear tabstat posths, by(group) stat(mean sd min max) *Confidence intervals sysuse auto ci means mpg price, level(90) use acupuncture.dta, clear bysort group: ci mean posths *TTEST sysuse auto ttest mpg==20 use acupuncture.dta, clear graph bar (mean) basehs, over(sex) ttest basehs, by(sex) *ANOVA webuse systolic oneway systolic drug, tabulate use acupuncture.dta, clear xtile chron_3 = chronicity, nq(3) graph bar (mean) basehs, over(chron_3) oneway basehs chron_3, tabulate *CHI2 sysuse cancer tab drug died, row chi2 use acupuncture.dta, clear graph bar (mean) migraine, over(sex) tab sex migraine, row chi2 *LINEAR REGRESSION sysuse auto regress mpg weight use acupuncture.dta, clear scatter chronicity age || lfit chronicity age regress chronicity age ****************************************************************************** * SESSION 6: Epi ****************************************************************************** use acupuncture.dta, clear *Set up (data cleaning) recode posths (min/19.99 = 0 "no") (20/max = 1 "yes") (.=.), gen(posths_sev) label variable posths_sev "severe headache" *Risk ratio and risk difference cs posths_sev no_acu di (76/(76+84))/(58/(58+108)) binreg posths_sev no_acu, rr glm posths_sev no_acu, family(binomial) link(log) eform *odds ratio cc posths_sev no_acu cs posths_sev no_acu, or di (76/84)/(58/108) logistic posths_sev no_acu logit posths_sev no_acu, or binreg posths_sev no_acu, or glm posths_sev no_acu, family(binomial) link(logit) eform *outreg2 tables (need to install outreg2 first) //ssc install outreg2 outreg2 using myreg, eform word //export to word outreg2 using myreg, eform replace excel ctitle(odds ratio) //export to excel outreg2 using myreg, eform replace tex(pretty) ctitle(odds ratio) //export tex file, copy and paste to LaTeX editor *tabout tables (need to install tabout first) //ssc install tabout2 tabout group using table1.csv, replace /// cell(mean basehs mean oneyrhs mean posths) format(1p 1p 1p) /// style(csv) sum clab("Baseline" "Post" "1yrPost") npos(col) ****************************************************************************** * SESSION 7: Stored results/Matrices/Loops/ Simple Program ****************************************************************************** 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 sex 0 "Female" 1 "Male", replace label values sex sex 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 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 `rsq1' // 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 loop to get the SE of 3 variables: foreach var of varlist age bmi bpsystol { sum `var' scalar SE_`var' = r(sd)/sqrt(r(N)) } di SE_age di SE_bmi di SE_bpsystol **for each loop to create markdoc table: 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_") *Simple program. Save as an .ado file in your .ado directory to call it whenever you open stata program define hello display "hi there" end