-1

I have 133 variables on income (each variable represents a group). I want the Gini coefficients of all these groups, so I use ineqdeco in Stata. I can't compute all these coefficients by hand so I created a for loop:

gen sgini = . 
foreach var of varlist C07-V14 {
forvalue i=1/133 {
ineqdeco `var'
replace sgini[i] = $S_gini
 }
}

Also tried changing the order:

 foreach var of varlist C07-V14 {
 ineqdeco `var'
 forvalue i=1/133 {
 replace sgini[i] = $S_gini
  }
 }

And specifying i beforehand:

gen i = 1 
foreach var of varlist C07-V14 {
    ineqdeco `var'
    replace sgini[i] = $S_gini
    replace i = i+1
     }
    }

I don't know if this last method works anyway. In all cases I get the error: weight not allowed r(101). I don't know what this means, or what to do. Basically, I want to compute the Gini coefficient of all 133 variables, and store these values in a vector of length 133, so a single variable with all the coefficients stored in it.

Edit: I found that the error has to do with the replace command. I replaced this line with:

replace sgini  = $S_gini in `i'

But now it does not "loop", so I get the first value in all entries of sgini.

Nick Cox
  • 35,529
  • 6
  • 31
  • 47
pk_22
  • 288
  • 1
  • 2
  • 18
  • In which example did you use the revised replace command? I think the first two examples have other problems beyond the replace command. –  Dec 07 '15 at 16:04
  • I used them in both the first and second example. What do you think is wrong in these? Because I think it goes wrong in the varlist, because it computes the Gini of the first variable, and this 133 times, and stores this in my variable. So I guess the 'i' is correct? But the third example is not different when considering the varlist.. – pk_22 Dec 07 '15 at 16:11

1 Answers1

0

There is no obvious reason for your inner loop. If you have no more variables than observations, then this might work:

gen sgini = . 
gen varname = "" 
local i = 1 
foreach var of varlist C07-V14 {
    ineqdeco `var'
    replace sgini = $S_gini in `i' 
    replace varname = "`var'" in `i' 
    local i = `i' + 1 
}

The problems evident in your code (seem to) include:

  1. Confusion between variables and local macros. If you have much experience with other languages, it is hard to break old mental habits. (Mata is more like other languages here.)

  2. Not being aware that a loop over observations is automatic. Or perhaps not seeing that there is just a single loop needed here, the twist being that the loop over variables is easy but your concomitant loop over observations needs to be arranged with your own code.

  3. Putting a subscript on the LHS of a replace. The [] notation is reserved for weights but is illegal there in any case. To find out about weights, search weights or help weight.

Note that with this way of recording results, the Gini coefficients are not aligned with anything else. A token fix for that is to record the associated variable names alongside, as done above.

A more advanced version of this solution would be to use postfile to save to a new dataset.

Nick Cox
  • 35,529
  • 6
  • 31
  • 47