Hello, i am trying to create an index of ethnic fractiolization using this formula:

Where s ij is the share of group I (i=1…N) in country j. Shows the probability that two randomly selected individuals from a nations population belongs to different ethnic groups.
The ethnic group is groupid, and size is the size of said ethnic group at that time. I tried to do :
bysort statename from:gen test =1-sum((size^2))
bysort statename from: egen test1=1-sum(size^2)
But those didn't work.
Where s ij is the share of group I (i=1…N) in country j. Shows the probability that two randomly selected individuals from a nations population belongs to different ethnic groups.
The ethnic group is groupid, and size is the size of said ethnic group at that time. I tried to do :
bysort statename from:gen test =1-sum((size^2))
bysort statename from: egen test1=1-sum(size^2)
But those didn't work.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str27 statename int(from to) long groupid float size "United States" 1946 1965 1000 .691 "United States" 1946 1965 2000 .125 "United States" 1946 1965 3000 .124 "United States" 1946 1965 4000 .036 "United States" 1946 1965 5000 .0078 "United States" 1946 1965 6000 .0042 "United States" 1966 2008 1000 .691 "United States" 1966 2008 2000 .125 "United States" 1966 2008 3000 .124 "United States" 1966 2008 4000 .036 "United States" 1966 2008 5000 .0078 "United States" 1966 2008 6000 .0042 "United States" 2009 2013 1000 .66 "United States" 2009 2013 2000 .15 "United States" 2009 2013 3000 .124 "United States" 2009 2013 4000 .044 "United States" 2009 2013 5000 .0078 "United States" 2009 2013 6000 .005 "Canada" 1946 1981 1000 .59 "Canada" 1946 1981 2000 .28 "Canada" 1946 1981 4000 .02 "Canada" 1982 2013 1000 .59 "Canada" 1982 2013 2000 .23 "Canada" 1982 2013 4000 .028 "Cuba" 1946 1959 1000 .641 "Cuba" 1946 1959 2000 .359 "Cuba" 1960 2013 1000 .641 "Cuba" 1960 2013 2000 .359 "Haiti" 1946 2013 1000 1 "Dominican Republic" 1946 2013 1000 1 "Jamaica" 1962 2013 1000 1 "Trinidad and Tobago" 1962 1986 1000 .4 "Trinidad and Tobago" 1962 1986 2000 .375 "Trinidad and Tobago" 1987 1991 1000 .4 "Trinidad and Tobago" 1987 1991 2000 .375 "Trinidad and Tobago" 1992 1995 1000 .4 "Trinidad and Tobago" 1992 1995 2000 .375 "Trinidad and Tobago" 1996 2002 1000 .4 "Trinidad and Tobago" 1996 2002 2000 .375 "Trinidad and Tobago" 2003 2010 1000 .4 "Trinidad and Tobago" 2003 2010 2000 .375 "Trinidad and Tobago" 2011 2013 1000 .354 "Trinidad and Tobago" 2011 2013 2000 .342 "Mexico" 1946 1974 1000 .801 "Mexico" 1946 1974 3000 .142 "Mexico" 1946 1974 9000 .05 "Mexico" 1946 1974 2000 .007 "Mexico" 1975 1997 1000 .801 "Mexico" 1975 1997 3000 .142 "Mexico" 1975 1997 9000 .05 "Mexico" 1975 1997 2000 .007 "Mexico" 1998 2013 1000 .801 "Mexico" 1998 2013 3000 .142 "Mexico" 1998 2013 9000 .05 "Mexico" 1998 2013 2000 .007 "Guatemala" 1946 1954 2000 .515 "Guatemala" 1946 1954 1000 .48 "Guatemala" 1946 1954 4000 .003 "Guatemala" 1946 1954 3000 .002 "Guatemala" 1955 1995 2000 .515 "Guatemala" 1955 1995 1000 .48 "Guatemala" 1955 1995 4000 .003 "Guatemala" 1955 1995 3000 .002 "Guatemala" 1996 2013 2000 .515 "Guatemala" 1996 2013 1000 .48 "Guatemala" 1996 2013 4000 .003 "Guatemala" 1996 2013 3000 .002 "Honduras" 1946 1974 6000 .91 "Honduras" 1946 1974 7000 .07 "Honduras" 1946 1974 10000 .016 "Honduras" 1975 1989 6000 .91 "Honduras" 1975 1989 7000 .07 "Honduras" 1975 1989 10000 .016 "Honduras" 1990 2013 6000 .91 "Honduras" 1990 2013 7000 .07 "Honduras" 1990 2013 10000 .016 "El Salvador" 1946 1994 1000 .9 "El Salvador" 1946 1994 2000 .1 "El Salvador" 1995 2013 1000 .9 "El Salvador" 1995 2013 2000 .1 "Nicaragua" 1946 1987 1000 .86 "Nicaragua" 1946 1987 2000 .09 "Nicaragua" 1946 1987 3000 .035 "Nicaragua" 1946 1987 5000 .002 "Nicaragua" 1988 2013 1000 .86 "Nicaragua" 1988 2013 2000 .09 "Nicaragua" 1988 2013 3000 .035 "Nicaragua" 1988 2013 5000 .002 "Costa Rica" 1946 1953 1000 .937 "Costa Rica" 1946 1953 2000 .019 "Costa Rica" 1946 1953 3000 .017 "Costa Rica" 1954 1991 1000 .937 "Costa Rica" 1954 1991 2000 .019 "Costa Rica" 1954 1991 3000 .017 "Costa Rica" 1992 2013 1000 .937 "Costa Rica" 1992 2013 2000 .019 "Costa Rica" 1992 2013 3000 .017 "Panama" 1946 1959 1000 .8 "Panama" 1946 1959 2000 .14 "Panama" 1946 1959 3000 .04 end
Comment