- UCLA: "How can I access information stored after I run a command in Stata (returned results)?"
- The Stata Blog: Drukker (2015). Programming an estimation command in Stata: Where to store your stuff
- Stackoverflow (2014).Saving coefficients and standard errors as variables
- Lembcke (2009). Advanced Stata Topics
- SSCC. An Introduction to Mata
- Stata commands are grouped into 4 major categories: r-class, e-class, s-class, and n-class commands. Also a c-class contains the values of system parameters and settings, along with certain constants.
- The commands produce the statistical results are either r-class or e-class. e-class commands produce the estimation results, others are belong to r-class.
- After submitting "contrast", Stata generates a L matrix (r(L)), you can check the contrast coefficients using "matrix list r(L)".
- If don't know what results are outputted, use "return list" or "ereturn list" to find them. The scalar results from a r-class can be used with the "r(...)" and scalar results from e-class command can be used with "e(...)". Here, "..." is the name showed using "return list" or "ereturn list". The use of results in matrix form is a little tricky. "_b[...]" or "_se[...]" have to be used; here, "..." is the variable name of a coefficient in the model. The results for a constant is used as "_b[_cons]" for beta coefficient or "_se[_cons]" for standard error. A matric results can also converted into a matrix: "mat B=e(b)", then "disp B[rowno, colon]".
- To show variance-covariance matrix, use: "estat vce" or just simple "matlist e(V)", and to show correlation, use: "estat vce, correlation".
- You can "estimate store" and "estimate restore" a set of estimates with a name in memory, in such way, the following command will not be erased. If want to save and use it as a permanent file, you can use "estimate save" and "estimate use".
- A single number can been converted into scalar, for example, "scalar xyz=_b[agecat]". However, the scalar has to be used with a pseudofunction scalar(), for example, "display scalar(xyz)" (more info)
- The e(V) and e(b) matrices can be converted into variables of a dataset using "svmat" (convert variables into matrix using "mkmat"), which is similar to "putmat and getmat" of mata (matrix ref.):
- mat D = e(b)', e(b)'
- svmat double D, name(coef)
- mat se1=vecdiag(e(V))
- mat se2=vecdiag(e(V))
- mat SE = se1, se2
- svmat SE, name(se)
- The "ereturn display" can use the e(V) and e(b) matrices to return a r-class matrix "r(table)"
- "margins" also gives e-class results:
- webuse dollhill3,clear
- poisson deaths i.smokes##c.agecat, exposure(pyears)
- est store tempreg
- margins smokes, gen(dhat) predict(ir) // undocumented gen()
- mean dhat1 // for smokes = 0
- scalar dhat1=_b[dhat1] // .00810452
- margins smokes, eydx(agecat) predict(ir) post
- scalar eydxsmokes0=_b[0.smokes] // 1.046826
- est restore tempreg
- margins smokes, dydx(agecat) predict(ir) post
- scalar dydxsmokes0=_b[0.smokes] // .00848402
- disp scalar(dydxsmokes0)/scalar(dhat1) // gives 1.046826
- Gould(2010).Mata Matters; (2011).Mata, the missing manual. Baum(2009).Using Mata to work more effectively in Stata
- putmat and getmat - Put Stata variables into Mata and vice versa
- mata r2=(1\2\3)
- mata b=st_matrix("e(b)")'
- mata se=sqrt(diagonal(st_matrix("e(V)")))
- getmata r2 b se, force
- vwls b r2, sd(se)
- reg b r2
- Rename "rowname" and "colname" of a matrix
matrix BB = e(b)
matrix colnames BB = "1.race" "2.race" "3.race"
ereturn repost b = BB, rename
matrix VV = e(V)
matrix colnames VV = "1.race" "2.race" "3.race"
matrix rownames VV = "1.race" "2.race" "3.race"
ereturn repost V = VV
end
- total heartatk [pw=swgt], over(race)
- estmatrename
- lincom (_b[3.race]-_b[1.race])/2
- test _b[1.race]=_b[2.race]
- contrast {race 1 -1 0}
- contrast p(1).race
- Convert ln(RR) into RR and percent change
- webuse dollhill3
- poisson deaths smokes i.agecat,exposure(pyears) irr margins agecat, predict(ir) post
- qui nlcom (lnRR21:ln((_b[2.agecat]/_b[1.agecat])))(lnRR31:ln((_b[3.agecat]/_b[1.agecat]))) (lnRR41:ln((_b[4.agecat]/_b[1.agecat]))), post
- ereturn disp,eform(RR) cformat(%5.2f) pformat(%5.4f)
- mat rtable=r(table)'
- mat RR=rtable[1...,"b"],rtable[1...,"ll".."ul"]
- mata st_matrix("pctable",(st_matrix("RR"):-1):*100)
- mat coln pctable=RR LL UL
- matlist pctable, format(%10.2f)r