Download presentation
Presentation is loading. Please wait.
Published byValentine Aldous Rice Modified over 9 years ago
1
Dec-15H.S.1 Stata 8, Programing Hein Stigum Presentation, data and programs at: http://folk.uio.no/heins/
2
Dec-15H.S.2 Programing Programing examples –Get and use results from commands –Automate tasks –Define functions –Define new commands Two languages –Stata Macro languageFor small problems –MataFor large problem
3
Dec-15H.S.3 Stata Macro Language
4
Dec-15H.S.4 Scalar Scalar (numbers) scalar a=2define display adisplay content gen x2=a*xuse Will not work in plots Only numbers
5
Dec-15H.S.5 Macro Local Macro (number or string) local a=2define display `a’display content gen x2=`a’*xuse Global Macro (number or string) global a=2define display $adisplay content gen x$a=x^2use Global has a “longer life” than local
6
Dec-15H.S.6 Matrix Matrix (matrix of numbers) matrix A=J(2,3,0)define 2*3 matrix of 0’s matrix list Adisplay content matrix A[2,1]=12change element matrix rownames A=mean1 mean2set names matrix colnames A=low med highset names Only numbers No vectors
7
Dec-15H.S.7 Use returned results Run command: Look for returned results: Use returned results: global m=r(mean) Or put in macro:
8
Dec-15H.S.8 Example: Density plot with extra info
9
Dec-15H.S.9 Commands for previous plot Run command: Put in macro: Use macros: summarize weight, detail global N=r(N)Number of observations global p10=r(p10)10-th percentile global p50=r(p50) global p90=r(p90) twoway (kdensity weight), note(N=$N) xlabel(minmax $p10 $p50 $p90, format(%5.0f))
10
Dec-15H.S.10 Get regression results Run regression: Put in matrices: Use macros: logistic lbw sex age matrix b=e(b)vector of coefficients, constant last matrix V=e(V)variance-covariance matrix Alternative: global b1= _b[sex]sex-coefficient global se1=_se[sex]sex-standard error matrix var=vecdiag(e(V))variance global ci1=exp(b[1,1]-1.96*sqrt(var[1,1])) lower CI for sex Look for returned results: ereturn list
11
Dec-15H.S.11 Loops Over variables Over index (for-loop) foreach var of varlist sex mage gest { summarize `var' } forvalues i=1(1) 5 { display `i' }
12
Dec-15H.S.12 Loops example: Crude ORs Version 1 Version 2, collect results in a matrix foreach var of varlist sex mage gest { logistic lbw `var' } matrix A=J(3,3,0) local i=1 foreach var of varlist sex mage gest { logistic lbw `var' matrix A[`i',1]=exp(_b[`var']) local ++i }
13
Dec-15H.S.13 Mata
14
Dec-15H.S.14 Mata commands Start and stop –matastart Mata –endstop Mata Help –help mata –help m4 introlist of functions –help mata max()given function
15
Mata commands cont. Vector x=(1,2,3)line vector y=(4\5\6)column vector x; y; y[1]display x and y and y 1 z=x+y'x+y-transposed x:+1element by element Differences from macro language: Dec-15H.S.15
16
Mata commands cont. Matrix A=J(2,3,0)2 by 3 matrix of 0’s A[1,2]=14change element A[1,.]1. line A[.,1]1. column A[(1\3),(1,2)]submatrix: line 1 and 3, col 1 and 2 Strong syntax, direct access to all submatrices Dec-15H.S.16
17
Mata read and set Read or set A=st_matrix("r(V)") 1. read from Stata st_matrix("X",A)2. set or reset in Stata s=st_matrixrowstripe("r(V)")row names m=st_global(”m")global macro st_addvar("double",(“y", “x”))add variables st_store((1,n),(“y", “x”),(y1,x1))store values Dec-15H.S.17 whereStata name Mata name
18
Mata commands cont. Run Stata commands stata(”cmd”) run Stata commands stata(”mean x”+strofreal(i)) mean x1 stata(”mean x”+strofreal(i)+”,detail”) mean x1, detail Remember spaces! Dec-15H.S.18
19
Dec-15H.S.19 Example: plot regression results Idea coeff and se as vectors, calculate CI store vectors as data Plot scatter and rcap Plot elements _y1-4 _pointOR or coeff _lowCI _highCI
20
Dec-15H.S.20 Example: plot valid and missing Idea tabstat…, stat(N) missing relative to 1. store vectors as data Plot bar, rbar, scatter with mlabel Plot elements _yy1-6 _valvalid _mismissing _tottotal
21
Dec-15H.S.21 Example: Bi-and multivariable table
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.