Stata%208,%20Programing - PowerPoint PPT Presentation

About This Presentation
Title:

Stata%208,%20Programing

Description:

Stata 8, Programing – PowerPoint PPT presentation

Number of Views:250
Avg rating:3.0/5.0
Slides: 22
Provided by: heins1
Category:

less

Transcript and Presenter's Notes

Title: Stata%208,%20Programing


1
Stata 8, Programing
  • Hein Stigum
  • Presentation, data and programs at
  • http//folk.uio.no/heins/

2
Programing
  • Programing examples
  • Get and use results from commands
  • Automate tasks
  • Define functions
  • Define new commands
  • Two languages
  • Stata Macro language For small problems
  • Mata For large problem

3
Stata Macro Language
4
Scalar
  • Scalar (numbers)
  • scalar a2 define
  • display a display content
  • gen x2ax use
  • Will not work in plots
  • Only numbers

5
Macro
  • Local Macro (number or string)
  • local a2 define
  • display a display content
  • gen x2ax use
  • Global Macro (number or string)
  • global a2 define
  • display a display content
  • gen xax2 use
  • Global has a longer life than local

6
Matrix
  • Matrix (matrix of numbers)
  • matrix AJ(2,3,0) define 23 matrix of 0s
  • matrix list A display content
  • matrix A2,112 change element
  • matrix rownames Amean1 mean2 set names
  • matrix colnames Alow med high set names

Only numbers No vectors
7
Use returned results
Run command
Look for returned results
Use returned results
Or put in macro
global mr(mean)
8
Example Density plot with extra info
9
Commands for previous plot
Run command
summarize weight, detail
Put in macro
global Nr(N) Number of observations global
p10r(p10) 10-th percentile global
p50r(p50) global p90r(p90)
Use macros
twoway (kdensity weight) , note(NN)
xlabel(minmax p10 p50 p90, format(5.0f))
10
Get regression results
Run regression
logistic lbw sex age
Look for returned results
ereturn list
Put in matrices
matrix be(b) vector of coefficients, constant
last matrix Ve(V) variance-covariance
matrix Alternative global b1
_bsex sex-coefficient global
se1_sesex sex-standard error
Use macros
matrix varvecdiag(e(V)) variance global
ci1exp(b1,1-1.96sqrt(var1,1)) lower CI
for sex
11
Loops
  • Over variables
  • Over index (for-loop)

foreach var of varlist sex mage gest
summarize var'
forvalues i1(1) 5 display i'
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 AJ(3,3,0) local i1 foreach var of
varlist sex mage gest logistic lbw
var' matrix Ai',1exp(_bvar') local i
13
Mata
14
Mata commands
  • Start and stop
  • mata start Mata
  • end stop Mata
  • Help
  • help mata
  • help m4 intro list 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 y1 display x and y and y1
  • zxy' xy-transposed
  • x1 element by element
  • Differences from macro language

16
Mata commands cont.
  • Matrix
  • AJ(2,3,0) 2 by 3 matrix of 0s
  • A1,214 change element
  • A1,. 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

17
Mata read and set
  • Read or set
  • Ast_matrix("r(V)") 1. read from Stata
  • st_matrix("X",A) 2. set or reset in Stata
  • sst_matrixrowstripe("r(V)") row names
  • mst_global(m") global macro
  • st_addvar("double",(y", x)) add variables
  • st_store((1,n),(y", x),(y1,x1)) store values

where
Stata name
Mata name
18
Mata commands cont.
  • Run Stata commands
  • stata(cmd) run Stata commands
  • stata(mean xstrofreal(i)) mean x1
  • stata(mean xstrofreal(i),detail) mean x1,
    detail
  • Remember spaces!

19
Example plot regression results
  • Idea
  • coeff and se as vectors,
  • calculate CI
  • store vectors as data
  • Plot
  • scatter and rcap
  • Plot elements
  • _y 1-4
  • _point OR or coeff
  • _low CI
  • _high CI

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
  • _yy 1-6
  • _val valid
  • _mis missing
  • _tot total

21
Example Bi-and multivariable table
Write a Comment
User Comments (0)
About PowerShow.com