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

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

 From Roberto Ferrer To Stata Help Subject Re: st: How to code |x_i-x_j| in stata? Date Thu, 10 Oct 2013 08:52:52 +0100

```Amy,

You do not give details on the paper nor do you explain the structure
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 == .
}
}
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 <limengjia626@163.com> 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/
```