* Biostat 212 Lecture 7 * Code from practice slides cd "/Users/crystal/Box/_Biostat212_2021/Week7/Lecture/Demo" use "acupuncture.dta", clear **** *practice #1 slide 8 * What is the 95% CI around the mean post-study headache score in the intervention group? ci mean posths if group==1 *alternatively: mean posths if group==1 mean posths if group==1, level(95) * What is the 95% CI around the mean post-study headache score in the control group? ci mean posths if group==0 *alternatively: mean posths if group==1 mean posths if group==1, level(95) **** *practice #2 slide 14 * Check how the categorical variables sex, migraine, group, and response are coded using a local macro. local catvar "sex migraine group response" codebook `catvar' * Side note * Tempting to use tab but it won't work--> have to use a loop (more to come!) * Also need to run local macro with each separate "do"! local catvar "sex migraine group response" foreach var in `catvar'{ tab `var' } local catvar "sex migraine group response" foreach var in `catvar' { di "`var'" //create heading for each table tab `var' //create tables } * Compare 2 regression models for the continuous outcome “posths” on group and age in one model, and group, age, and sex in the second. Create local macros for the 2 sets of covariates and run linear regression models using the macros. local model1var "group age" regress posths `model1var' local model2var "group age sex" regress posths `model2var' **** * practice #3 slide 19 * What percentage of study participants are female, report migraines, and showed >35% improvement in headache score by intervention group? foreach var of varlist sex migraine response { di "group x `var'" tab group `var', row } *alternatively: foreach var in sex migraine response { di "group x `var'" tab group `var', row } * What are the 95% CIs around the mean baseline headache score, post-study headache score, and one-year headache score in the intervention and control groups? foreach var of varlist basehs posths oneyrhs { ci mean `var' if group==1 ci mean `var' if group==0 } *alternatively foreach x in basehs posths oneyrhs { mean `x' if group ==1 mean `x' if group ==0 } **** * slide 18 example webuse nhanes2, clear ** Foreach loop example foreach var of varlist age height weight bpsystol { sum `var' if region==1 histogram `var' if region==1 } *alternatively: foreach var in age height weight bpsystol { sum `var' if region == 1 histogram `var' if region == 1 } * This is equivalent to: sum age if region==1 histogram age if region==1 sum height if region==1 histogram height if region==1 sum weight if region==1 histogram weight if region==1 sum bpsystol if region==1 histogram bpsystol if region==1 ***************** /* KD curse example */ **** practice slide 30 use "KDcurse_data.dta", clear * Giants win probability by year* tabstat win, by(year) stat(mean) *Giants win probability by era * tabstat win, by(era) stat(mean) *Giants win probabililty by home vs away* tabstat win, by(home) stat(mean) **** practice slide 32 *Risk ratio* cs win KD **** practice slide 38 *Odds ratio* cc win KD mhodds win KD *Odds ratio adjusting for home vs away* cc win KD, by(home) mhodds win KD, by(home) **** practice slide 41 *logistic regression - use any of the following: logistic win KD logit win KD, or glm win KD, family(binomial) link(logit) eform *adjust for day vs night - use any of the following: logistic win KD daygame logit win KD daygame, or glm win KD daygame, family(binomial) link(logit) eform mhodds win KD daygame ******* extra code /* Graph Giants win loss over era */ graph bar win, over(era) graph export "KDcursegraph", as(png) replace putdocx clear putdocx begin putdocx paragraph, halign(center) putdocx image "KDcursegraph" putdocx save "thecurseisreal.docx", replace /* Confidence interval */ ci means attendance /* For loop example: run CC win by: year, KD, era, day of week (dow) */ *encode dow* codebook dow encode dow, gen(dow_cat) codebook dow_cat foreach var in home dow_cat { display "`var'" cc win KD, by(`var') }