Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Re: your response to stata list

From   Maarten Buis <[email protected]>
To   [email protected]
Subject   st: Re: your response to stata list
Date   Wed, 25 Jul 2012 09:49:06 +0200

It is convention on Statalist to ask follow up question on the list
and not privately, that way that part of the discussion will also be
available in the archive.

The reason for the error message is that you cannot have a -mata-
<stuff> -end- session inside a Stata loop. What you can do is put the
Mata commands inside a Mata function and inside the Stata loop call
-mata you_mata_function()-. See:

However, I think the easier solution for you would be to use Roger
Newson's -parmby- command, which is part of the -parmest- package.

*--------------- begin example ---------------
// get the -parmby- command
// I have it already so it is commented out
*ssc install parmest

sysuse cancer, clear
stset studytime, failure(died)
parmby "stcox age", by(drug) eform norestore
*--------------- end example -----------------

Also see:
Roger B. Newson (2012) "From resultssets to resultstables in Stata".
The Stata Journal, 12(2): 191-213.

Roger B. Newson (2003) "Confidence intervals and p–values for delivery
to the end user". The Stata Journal, 3(3):245-269.

Hope this helps,

On Tue, Jul 24, 2012 at 10:09 PM, yaacov lawrence wrote:
> thank you for your rapid response on stata-list.
> your code works wonders, but I have now the problem of incorporating mata
> code into a stata do file.
> for some reason the code below does not work.
> the code should go through each value of "newgroup", performing a cox
> analysis and filling the p values and HR into a matrix.
> for some reason the loop gets stuck, possible by the word 'end'
> I confess to feeling very out of my depth....

> ------------
> egen newgroup = group(new_fips)
> sum newgroup
> matrix results = J(`r(max)',5,.)
> matrix colnames results = group zipcode #patients HR P-value
> *go though each zip code  and see how they improved over time (i.e. run a
> cox analysis)
> foreach i of num 1/`r(max)' {
> matrix results[`i',1]=`i'
> quietly sum fips if group==`i'
> matrix results[`i',2]= r(mean)
> quietly count if group==`i'
> matrix results[`i',3]= r(N)
> stcox year_group if newgroup==`i'
> mata
> b = st_matrix("e(b)")
> V = st_matrix("e(V)")
> hr = exp(b)
> se = diagonal(cholesky(diag(V)))'
> z = b:/se
> p = 2*normal(-abs(z))
> st_numscalar("HR",hr)
> st_numscalar("P",p)
> end
> matrix results[`i',4]= HR
> matrix results[`i',5]= P
> }
> matrix list results
> --

Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index