470 likes | 929 Views
Stata Intro Mixed Models. Hein Stigum Presentation, data and programs at: http://folk.uio.no/heins/. Why Stata. Pro Aimed at epidemiology Many methods, growing Graphics Structured, Programmable Coming soon to a course near you Con Memory>file size Copy tables. Use. Interface.
E N D
Stata Intro Mixed Models Hein Stigum Presentation, data and programs at: http://folk.uio.no/heins/
Why Stata • Pro • Aimed at epidemiology • Many methods, growing • Graphics • Structured, Programmable • Coming soon to a course near you • Con • Memory>file size • Copy tables H.S.
Interface H.S.
Do Editor • New • Ctrl-8, or: • Run • Mark commands, Ctrl-D to do (execute) H.S.
Do-file example H.S.
Syntax • Syntax [bysort varlist:] command [varlist] [if exp] [in range][, opts] • Examples • mean age • mean age if sex==1 • bysort sex: summarize age • summarize age ,detail H.S.
Import data • Using SPSS 14.0 • Save as, Stata Version 8 SE H.S.
Use and save data • Open data • set memory 200m • use “C:\Course\Myfile.dta”, clear • Describe • describe describe all variables • list x1 x2 in 1/20 list obs nr 1 to 20 • Save data • save “C:\Course\Myfile.dta” ,replace H.S.
Age square generate ageSqr=age^2 Young/Old generate old=0 if (age<=50) replace old=1 if (age>50) Observation numbers gen id=_n gen lag=age[ _n-1] Alternatives generate old=(age>50) generate old=(age>50) if age<. Generate, replace H.S.
Missing • Obs!!! • Missing values are large numbers • age>30 will include missing. • age>30 & age<. will not. • Test • replace x=0 if (x==.) • Remove • drop if age==. • Change • replace educ=. if educ==99 H.S.
Calculater • Display • dis 26/3 • dis exp(1.2) • Store results • scalar se=sqrt( 0.8*(1-0.8)/60 ) • dis se H.S.
Help • General • help command • findit keyword search Stata+net • Examples • help table • findit aflogit H.S.
Summing up • Use do files • Mark, Ctrl-D to do (execute) • Syntax • command [varlist] [if exp] [in range] [, options] • Missing • age>30 & age<. • generate old=(age>50) if age<. • Help • help describe H.S.
Books Web: http://www.stata.com/bookstore A Gentle Introduction to Stata by Alan C. Acock A visual guide to Stata graphics by M.N. Mitchell Multilevel and longitudinal modeling using Stata by S. Rabe-Hesketh, A. Skrondal H.S.
Twoway density • Syntax • graph twoway (plot1, opts) (plot2, opts), opts • One plot • kdensity x • Two plots, boys and girls compared twoway ( kdensity weight if sex==1, lcolor(blue) ) /// ( kdensity weight if sex==2, lcolor(red) ) H.S.
twoway ( kdensity weight if sex==1, lcolor(blue) ) /// • ( kdensity weight if sex==2, lcolor(red) ) H.S.
Twoway scatter • Syntax • graph twoway (plot1, opts) (plot2, opts), opts • Examples • scatter y x • twoway (scatter y x) (lfit y x) Fitlines with CI H.S.
Descriptives H.S.
Central tendency and dispersion Mean and standard deviation: Mean with confidence interval: H.S.
Frequency and proportion Frequency: Proportion with CI: H.S.
equal proportions? Crosstables Are boys bullied as much as girls? H.S.
Tables for epidemiologists • Data • Must be 0/1 • Long format. Wide format • Commands • cc Case-control • mcc Matched case-control • Example • cc disease exposed, by(sex) Stratified MH-OR • Calculator (i=immideate) • cci 10 90 5 95 OR H.S.
Logistic regression Being bullied H.S.
Syntax • Estimation • logistic y x1 x2 logistic regression • xi: logistic y x1 i.c1 categorical c1 • Post estimation • predict yf, pr predict probability • Manage models • estimates store m1 save model • est table m1, eform show OR H.S.
Bivariate, dummies Generate dummies gen Island= (country==2) if country<. gen Norway= (country==3) gen Finland= (country==4) gen Denmark= (country==5) H.S.
Model 1: outcome and exposure Alternative commands: xi:logistic bullied i.country use xi: i.var for categorical variables xi:logistic bullied i.country , coef coefs instead of OR's xi:logistic bullied i.country if sex!=. & age!=. do if sex and age not missing H.S.
Estimate associations: m1=m2 Predict: m2 best Model 2: Add confounders H.S.
Model 3: interaction lincom age+1*agesex effect of age for boys lincom age+2*agesex effect of age for girls H.S.
Regression Summary • Estimation • regress y x1 x2 linear regression • logistic y x1 x2 logistic regression • xi:regress y x1 i.x2 categorical x2 • Manage results • estimates store m1 store results • estimates table m1 m2 table of results • estimates stats m1 m2 statistics of results • Post estimation • predict y, xb linear prediction • predict res, resid residuals • lincom b0+2*b3 linear combination • Help • help logistic postestimation H.S.
Mixed ModelsMultilevel modelsPanel dataRepeated measurements Jun-14 H.S. H.S. 34
Long and wide data Wide data reshape wide bp, i(id) j(occ) reshape long bp, i(id) j(occ) Long data H.S.
Correlated measures • Two measures per person: W1 W2 symmetry W1 W2 Measure the same? • Matched Case-Control mcc expCase expContr Matched OR
Multilevel data • Panel data • xt xsectional time data • help xt
Setup and describe • Set panel data • xtset school pupils nested in schools • xtset id time times nested in subjects • Describe panel data • xtdes describe data and missing • xtsum bp summarize bloodpressure • xttab ht tabulate hypertension • xtline bp plot bp versus time for each id • Lag and lead • replace bp=bp[ _n+1] if id==1 H.S.
Logistic regression methods • Fixed effects models • logit y x1 x2, or • Conditional fixed effects models • clogit y x1 x2, group(id) or • Random intercept models • xtlogit y x1 x2, i(id) or • Mixed effects models • xtmelogit y x1 x2 || id: x1 , or • Population average effects • xtgee y x1 x2, i(id) t(time) fam(bin) link(logit) robust eform H.S.