Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# Re: st: loops for regions

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: loops for regions Date Thu, 6 Sep 2012 15:30:20 +0100

```It's your choice, but you should be able to see that Mata is closer to
the mathematics you want to implement.

Nick.

On Thu, Sep 6, 2012 at 3:26 PM, Chiara Mussida <cmussida@gmail.com> wrote:
> Thanks Nick. I will test your suggestion in Stata, since I'm not too
> confident with mata.
>
> On 06/09/2012, Nick Cox <njcoxstata@gmail.com> wrote:
>> Thanks. A quick stab at this is that you have two vectors x1 and x2 and go
>>
>> . mata :
>> ------------------------------------------------- mata (type end to
>> exit) -----------------------------------------
>> :
>> : x1 = (10,10,10,10,10)
>>
>> : x2 = (12,14,16,18,20)
>>
>> : sum_x1 = sum(x1)
>>
>> : sum_x2 = sum(x2)
>>
>> : a = x2 / sum_x2
>>
>> : b = (log(x2) - log(x1)) :- (log(sum_x2) - log(sum_x1))
>>
>> : sqrt(sum(a :* (b:^2)))
>>   .1763637091
>>
>> : end
>>
>> The main thing is not whether I have understood the details but that
>> this is a calculator problem; it is just more convenient for a Stata
>> user to use Mata for a calculator.
>>
>> In Stata it could be something like (completely untested code)
>>
>> sum x1, meanonly
>> local sum_x1 = r(sum)
>> sum x2, meanonly
>> local sum_x2 = r(sum)
>>
>> gen work = x2 / `sum_x2'
>> replace work = work * (((log(x2) - log(x1)) - (log(`sum_x2') -
>> log(`sum_x1')))
>> su work, meanonly
>> di sqrt(r(sum))
>>
>> Nick
>>
>> On Thu, Sep 6, 2012 at 8:05 AM, Chiara Mussida <cmussida@gmail.com> wrote:
>>> The reference is :
>>> D.M. Lilien, Sectoral shifts and cyclical unemployment, Journal of
>>> Political Economy, 90 (4) (1982), pp. 777–793.
>>>
>>> The index is defined on page 787. It is a bit more complicated than
>>> the herfindahl index.
>>>
>>>
>>>
>>> On 06/09/2012, Nick Cox <njcoxstata@gmail.com> wrote:
>>>> Did you try
>>>>
>>>> findit lilien
>>>>
>>>> Someone may recognise this, but you give no reference or definition.
>>>>
>>>> Nick
>>>>
>>>> On Wed, Sep 5, 2012 at 9:52 PM, Chiara Mussida <cmussida@gmail.com>
>>>> wrote:
>>>>> tried to write my own file since the next step will be, as i hope, to
>>>>> compute a Lilien index for each region. I tried to search for a stata
>>>>> program but i did not succeed. Does anybody know whether it exists?
>>>>> Thanks
>>>>>
>>>>> On 05/09/2012, Nick Cox <njcoxstata@gmail.com> wrote:
>>>>>> The first line confuses the -if- command and the -if- qualifier.
>>>>>>
>>>>>> I didn't look in detail at later code, as there should be no need to
>>>>>> write your own code, as many programs exist. -search inequality- for a
>>>>>>
>>>>>> In fact, in many ways this is a calculator problem easily tackled with
>>>>>> Mata.
>>>>>>
>>>>>> . mata
>>>>>> : freq = (0, 0, 3, 9, 9)
>>>>>> : pr = freq :/ sum(freq)
>>>>>> : sum(pr:^2)
>>>>>>   .387755102
>>>>>>
>>>>>> In fact, zero frequencies map to zero squared proportions and can be
>>>>>> omitted.
>>>>>>
>>>>>> This index, although often attributed to Herfindahl by economists who
>>>>>> know only their only literature, was in essence invented decades
>>>>>> before by Gini. I don't have the reference in my head, but I think it
>>>>>> is in Bishop, Y., Fienberg, S.E. and Holland, P.W. 1975. Discrete
>>>>>> multivariate analysis. Cambridge, MA: MIT Press. Please trump me by
>>>>>> providing a yet earlier reference.
>>>>>>
>>>>>> Nick
>>>>>>
>>>>>> On Wed, Sep 5, 2012 at 5:15 PM, Chiara Mussida <cmussida@gmail.com>
>>>>>> wrote:
>>>>>>
>>>>>>> I want to generate a variable herf1 which takes the value of the
>>>>>>> below
>>>>>>> index for region 1, and thereafter repeat this loop for all the
>>>>>>> region
>>>>>>> in my dataset. The problem is that with the below command I get the
>>>>>>> herf1 index for region 1, which is identycal in value (I tried) to
>>>>>>> the
>>>>>>> herf2 for region2 whether I repeat this llop for region2, by starting
>>>>>>> with "if reg==2, and so forward for all the regions.
>>>>>>>
>>>>>>> if reg==1 {
>>>>>>> tab cat12_2, gen(categ)
>>>>>>> forvalues k = 1 2 to 12 {
>>>>>>> sum categ`k', meanonly
>>>>>>>         gen share`k'=r(mean) if reg==1 /*shares of each occ cat on
>>>>>>> the
>>>>>>> total
>>>>>>> occupation, time t*/
>>>>>>> }
>>>>>>> }
>>>>>>>
>>>>>>> gen herf1=(share1)^2 + (share2)^2 + (share3)^2 + (share4)^2 +
>>>>>>> (share5)^2 + (share6)^2 + (share7)^2 + (share8)^2 ///
>>>>>>> + (share9)^2 + (share10)^2 + (share11)^2 + (share12)^2  /*Herfindhal
>>>>>>> Index Region1*/
>>>>>>> the variable reg takes 1 for region1, 2 for region2...up to 20 for
>>>>>>> region20. In other words, Stata does not compute the specific index
>>>>>>> for each region, but the same index region by region.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```