/* Week 6: Tables and basic stats Lecture Notes for week 6 */ clear capture log close cd "/Users/amandairish/Desktop/Biostat 212/Week 6" *log using "output/lecture6notes.smcl",replace use "acupuncture.dta", clear /* Demo tabstat */ tabstat basehs oneyrhs, by(group) stat(mean sd min max) save return list /* We see 3 matrices, each corresponding to a level of "group". We can save these matrices to use later and to manipulate them. */ matrix controlres = r(Stat1) //save new matrix called 'controlres' matlist controlres //display matrix /* We can flip the matrix so the variables become the rownames */ matrix controlres=controlres' // the ' symbol transposes the matrix matlist controlres /* Let's save the acupuncture results as well */ matlist r(Stat2) //we want to transpose this as well matrix acures=r(Stat2)' //let's transpose the original matlist acures /* We can use putdocx to put these tables into Word */ putdocx clear putdocx begin putdocx table ControlTable1=matrix(controlres), rownames colnames layout(autofitcontents) title("Control Group") putdocx table AcupunctureTable1=matrix(acures), rownames colnames title("Acupuncture Group") putdocx save "Lecture6.docx", replace /* Combine into one matrix */ matrix combined=controlres \ acures matlist combined putdocx begin putdocx table CombinedTable=matrix(combined), rownames colnames title("Combined Table") layout(autofitcontents) putdocx save "Lecture6.docx", append /* Practice - slide 21 */ tabstat posths, by(group) stat(mean sd min max) save matrix overall=r(StatTotal) * putdocx clear if hadn't initiated putdocx yet putdocx begin putdocx table posthstab=matrix(overall), rownames colnames title("PostHS overall") putdocx save "Lecture6.docx", append /* Collapse: modifies the data! */ collapse (mean) meanbase=basehs meanpost=posths mean1yr=oneyrhs (sd) sdbase=basehs sdpost=posths sd1yr=oneyrhs (min) minbase=basehs minpost=posths min1yr=oneyrhs (max) maxbase=basehs maxpost=posths max1yr=oneyrhs, by(group) putdocx begin putdocx table summarytable=data(*), title("Summary Tab") varnames putdocx save "Lecture6.docx",append //add to previous output /* Demo with tab for categorical variables */ use "acupuncture.dta",clear tab sex migraine, row matcell(sexmigraine) matlist sexmigraine //note that tab options like , row or , col do NOT get saved into the matrix of results for tab putdocx begin putdocx table sexmigrainetab = matrix(sexmigraine), rownames colnames putdocx save "Lecture6.docx", append putdocx begin putdocx table sexmigrainetab = matrix(sexmigraine), rownames colnames putdocx table sexmigrainetab(2,1) = ("Male") putdocx table sexmigrainetab(3,1) = ("Female") putdocx table sexmigrainetab(1,2) = ("No migraine") putdocx table sexmigrainetab(1,3) = ("Migraine") putdocx save "Lecture6.docx", append // this looks better, but still don't have percentages /* Practice - slide 29 */ scatter chronicity age || lfit chronicity age regress chronicity age /* Using estimates and putdocx to export table to Word Run two linear regressions: 1: outcome=posths predictor=group; 2: outcome=posths predictors: age & group */ use "acupuncture.dta",clear /* Step 1: label variables */ lab var age "Age(yrs)" lab var group "Treatment Assignment" lab def group2 0"Control Group" 1"Acupuncture Group" lab val group group2 lab var posths "Follow-up Headache Score" /* Step 2: Run first regression */ regress posths group ereturn list estimates store model1 //this stores the results in as a matrix called "model1" /* Step 3: Run second regression */ regress posths group age //we are now adding age as a predictor estimates store model2 //store results as "model2" /* Step 4: create estimates table */ estimates table model1 model2 //basic table *Make it look a bit nicer* estimates table model1 model2, b(%10.2f) star stats(N) varlabel allbaselevels /* putdocx */ putdocx begin putdocx table regoutput=etable putdocx save "Lecture6.docx",append /* putexcel: export matrix created above to excel */ *Regenerate the matrices from earlier* /* Demo tabstat */ tabstat basehs posths oneyrhs, by(group) stat(mean sd min max) save return list /* We see 3 matrices, each corresponding to a level of "group". We can save these matrices to use later and to manipulate them. */ matrix controlres = r(Stat1) //save new matrix called 'controlres' matlist controlres //display matrix /* We can flip the matrix so the variables become the rownames */ matrix controlres=controlres' // the ' symbol transposes the matrix matlist controlres /* Let's save the acupuncture results as well */ matlist r(Stat2) //we want to transpose this as well matrix acures=r(Stat2)' //let's transpose the original matlist acures /* Export matrices to excel with putexcel*/ putexcel set "Lecture6Notes", sheet(example) replace putexcel A1=matrix(controlres), names nformat(number_d2) putexcel A10=matrix(acures), names nformat(number_d2)