Andreas Reinstaller <Andreas.Reinstaller@wu-wien.ac.at> wrote,
> I have found a solution that does not strike me to be particularly
> elegant, but that works. I just use the indices of the variables in
> Stata, where NoEnt_nsc1 TO_nsc1 TO_Nace NoEmpl_nsc1 LET are the
> variables I use:
>
> In a simple do file I have this code
>
> [do-file omitted]
>
> For 12000 obs it takes about 15 minutes to run on a dual core pentium
> with 2.8ghz and Windows XP in Stata 8.2.
>
> If somebody has ideas about how to improve the speed, I should be grateful.
The code in the omitted do-file is complicated and I do not know what
it does. I have, however, performed a line-by-line translation of
the code into Mata, where it should run faster. I cannot vouch for the
fedelity of my translation. The translation reads:
------------------------------------------------ begin do-file ------
sort country time SecCode sizeclass
generate ntx=_n
generate newntx=.
tomata // <- creates Mata vectors from every variable
mata:
size = st_nobs()
for (i=1; i<=size; i++) {
nof = NoEnt_nsc1[i]
if (nof==.) nof = 0
szs = 0
if nof > 0 {
if (nof == 1) {
szs = (NoEnt_nsc1[i]*(100*(TO_nsc1[i]/NoEnt_nsc1[i])/TO_Nace[i] )^2)
}
else {
for (j=1; j<=nof; j++) {
sz = ((100*(
(TO_nsc1[i]/NoEmpl_nsc1[i]*LET[i]) + (2*j*
(TO_nsc1[i] - (
NoEnt_nsc1[i]*(TO_nsc1[i]/ NoEmpl_nsc1[i]*LET[i])
)) /(NoEnt_nsc1[i]*(NoEnt_nsc1[i]-1)))
) / TO_Nace[i])^2)
szs = szs + sz
}
}
}
newntx[i] = szs
}
end
by country time SecCode: egen Ave_HHI_nsc1=sum(newntx)
------------------------------------------------ end do-file ------
To run the above Andreas will need -tomata-. This program was discussed
in a previous Mata Matters column. To get the command, type
. ssc install tomata
In his code, Andreas has various Stata variables such as NoEnt-nsc1.
All -tomata- does is create Mata vectors from the Stata variables, so
I can refer to them in the Mata code. The vectors created a views onto
the Stata data, so each takes only a few bytes of memory.
As Nick Cox mentioned, I suspect that if we knew the formula of what
was being implemented, a more efficient way could be found to make
the calculation. Nonetheless, it looks to me as if Andreas has code
from another system that the knows works, and sometimes the easiest
thing is just to translate blindly.
-- Bill
wgould@stata.com
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/