Dear All, The question is from here. The data is
Code:
clear
input byte(familyid gender) int income ratio
1 1 200 0
1 0 150 0
2 1 300 0.25
2 0 250 0.25
2 0 350 0.25
2 1 400 0.25
3 0 280 1
3 1 220 1
3 0 350 1
4 0 300 0.5
4 1 200 0.5
4 1 400 0.5
end
For each family (familyid), I'd like to calculate a (female with higher-income) `ratio'.
  1. Note that, gender=1 denotes male while gender=0 denotes female.
  2. For familyid=1, one male and one female, and the income of female is less than that of male, as a result, the ratio=0.
  3. For familyid=2, two males and two females. The female with income 250 is less than both male's income, thus the probability is 0/2, and the female with income 350 is higher than one of the male's income, thus the probability is 1/2. The final ratio is then (0/2+1/2)/2=1/4=0.25.
  4. Similarly, for familyid=3, the ratio=(1/1+1/1)/2=1, and for familyid=4, the ratio=(0/1+1/1)/2=0.5.
Any suggestions for doing this is highly appreciated.