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

# Re:Re: st: How to code |x_i-x_j| in stata?

 From "LI Mengjia" <[email protected]> To [email protected] Subject Re:Re: st: How to code |x_i-x_j| in stata? Date Fri, 11 Oct 2013 21:03:57 +0800 (CST)

```Dear Robert,

Many thanks to your detailed explanation! I spent some time to understand each line and find this part very useful:

local K = `N'
forvalues i = 1/`K' {
bys prvcid: gen xp`i' = abs(x[_n] - x[_n+`i'])
}

Now I'm stuck by my data in which the ind is a string variable (some contain a letter  while some are pure numbers. This makes it difficult to get the number of industries.

Thanks and best regards,
Amy

At 2013-10-10 15:52:52,"Roberto Ferrer" <[email protected]> wrote:
>Amy,
>
>You do not give details on the paper nor do you explain the structure
>of your database.  I have made some assumptions to compute the
>locational Gini coefficient, but not exactly as you specified it. In
>the middle of the code there are some things that might help you.
>
>Basically, I use -fillin- and a loop to do the subtractions. The
>downside is that it creates N-1 variables  (where N is number of
>areas) so on a big database it may give trouble (not too difficult to
>adjust I think). I've littered the code with -list- so the changes are
>visible in each step. I'm sure someone can come up with a better way
>of doing it but I believe it works for this example at least.
>
>/*
>Based on:
>
>MEASURING AGGLOMERATION: AN EXPLORATORY SPATIAL ANALYSIS APPROACH APPLIED TO
>THE CASE OF PARIS AND ITS SURROUNDING.
>Rachel Guillain and Julie Le Gallo.
>www.real.illinois.edu/d-paper/06/06-t-10.pdf?
>*/
>
>*----------------------------- input -------------------------------------------
>clear
>
>input area       ind             sh2  sh1
>1                1               0.7  0.70
>1                2               0.7  0.15
>1                3               0.7  0.05
>1                4               0.7  0.10
>1                5               0.7  1
>2                1               0.2  0.20
>2                2               0.2  0.25
>2                3               0.2  0.95
>2                4               0.2  0.90
>3                1               0.1  0.10
>3                2               0.1  0.60
>end
>list, sepby(area)
>
>/*
>ind: industry
>sh1: Area i's share of employment in industry m
>sh2: Area i's share of total employment
>
>Notice industry 1 is distributed identically to that of total employment and
>that industry 5 is totally concentrated in area 1. Therefore, Gini should be
>0 and 0.5, for the respective industries.
>*/
>
>*--------------- Preliminary computations for locational Gini ------------------
>
>* Number of areas (assume area coding starts at 1 and has no jumps)
>summarize area, mean
>local N = r(max)
>
>* Compute X
>gen x = sh1/sh2
>drop sh*
>list, sepby(area)
>
>*----------------------- What you want (I think) -------------------------------
>
>* Prepare data for subtractions
>fillin area ind
>drop _fillin
>list, sepby(area)
>
>sort ind x
>list, sepby(ind)
>
>* Subtractions (should do N-1 loops)
>local K = `N'-1
>forvalues i = 1/`K' {
>    by ind: gen xp`i' = abs(x[_n] - x[_n+`i'])
>    if (`i' == 1) {
>        * Correct for industry that appears only in one area.
>        by ind: replace xp`i' = x if x[2] == .
>    }
>}
>list, sepby(ind)
>
>* Compute the double sum
>egen subtot = rowtotal(xp*)
>by ind: egen tot = total(subtot)
>list, sepby(ind)
>
>* ------------------ Rest of computations for locational Gini ------------------
>
>*Compute sum of x
>by ind: egen xsum = total(x)
>list, sepby(ind)
>
>* Drop unnecessary
>by ind: keep if _n == 1
>drop area xp* subtot
>list
>
>* Compute numerator
>gen numerator = 1/(`N'-1) * tot
>
>* Compute Locational Gini Coefficient
>gen G = numerator/xsum
>list
>
>On Thu, Oct 10, 2013 at 4:09 AM, LI Mengjia <[email protected]> wrote:
>> Dear statalist,
>>
>> I'm trying to calculate a "locational Gini coefficient" (rg) by using the formula in a reference paper in the following:
>>
>> rg=(sum|x_i-x_j|)/(2*n^2*u)
>>
>> where n is the total number of industries in an area, i and j are just two industries in n industries, x is the market share of any industry, and u is the mean of x.
>>
>> I have not idea how to put x_i-x_j into stata.
>>
>> Thanks and best regards,
>> Amy
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>> *   http://www.ats.ucla.edu/stat/stata/
>*
>*   For searches and help try:
>*   http://www.stata.com/help.cgi?search
>*   http://www.stata.com/support/faqs/resources/statalist-faq/
>*   http://www.ats.ucla.edu/stat/stata/

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