Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Decomposing Wagstaff Concentration Index

    Dear All,

    I'm using Stata 16 and I'm trying to decompose my concentration index using the following independent variables: equivalised_income, race, gender, geo educ_m (mother education), empl_m (mother employment), wt_m (mother weight). I get an error message saying that my varlist is not allowed, with code r(101). Obese is a binary variable

    My commands are as follows

    Code:
    conindex obese [aweight=wt], rankvar(wquintile) wagstaff bounded limits (1 2)
    I get the results for conindex.

    Global X is as follows;

    Code:
    global X quintile feedingscheme race gender geo educ_m empl_m  wt_m
    qui regr obese $X [pw=wt] 
    sum obese [aw=wt]
    sca m_obese=r(mean)
    No problem here

    Then when i try the below I get the error message that says varlist not allowed
    Code:
    foreach x of global X { 
    qui {  
    sca b_`x’ = _b[`x’]    
    corr rank `x’ [pw=wt], c  
    sca cov_`x’ = r(cov_12)      
    sum `x’ [aw=wt] 
    sca elas_`x’ = (b_`x’*r(mean))/m_obese  
    sca CI_`x’ = 2*cov_`x’/r(mean)   
    sca con_`x’ = elas_`x’*CI_`x’ 
    sca prcnt_`x’ = con_`x’/CI   
    } 
    di “`x’ elasticity:”, elas_`x’ 
    di “`x’ concentration index:”, CI_`x’ 
    di “`x’ contribution:”, con_`x’ 
    di “`x’ percentage contribution:”, prcnt_`x’
    }
    Results:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(equivalized_income feedingscheme) byte(race gender geo) float(empl_m educ_m wt_m)
    1035.9819 . 2 2 1 . . .
    254.44856 . 2 1 1 . . .
    254.44856 . 2 2 1 . . .
    254.44856 2 2 2 1 1 1 2
    254.44856 2 2 2 1 1 1 2
     546.3166 . 2 1 1 . . .
     546.3166 . 2 2 1 . . .
     546.3166 . 2 1 1 . . .
     202.3135 . 2 2 1 . . .
     202.3135 2 2 1 1 1 1 2
     933.5438 . 2 2 1 . . .
    117.27608 . 2 1 1 . . .
    117.27608 . 2 1 1 . . .
     740.8214 . 2 2 1 . . .
     740.8214 . 2 1 1 2 1 3
     740.8214 2 2 2 1 2 1 3
     740.8214 2 2 2 1 2 1 3
    547.75574 . 2 2 1 . . .
    547.75574 1 2 2 1 2 1 3
    547.75574 2 2 1 1 2 1 3
    547.75574 2 2 2 1 . . .
    547.75574 . 2 1 1 . . .
     940.7536 . 2 1 1 . . .
     940.7536 2 2 2 1 . . .
     940.7536 2 2 1 1 . . .
     940.7536 2 2 2 1 . . .
     940.7536 . 2 2 1 . . .
     940.7536 2 2 2 1 . . .
     965.0203 . 2 1 1 1 1 4
     965.0203 . 2 2 1 . . .
     965.0203 . 2 2 1 . . .
     965.0203 2 2 1 1 2 1 2
     965.0203 2 2 2 1 2 1 2
     965.0203 . 2 2 1 2 1 2
     965.0203 . 2 1 1 2 1 2
    126.10336 2 2 1 1 . . .
    126.10336 2 2 1 1 . . .
    126.10336 . 2 1 1 . . .
    126.10336 2 2 1 1 . . .
    300.64355 . 2 2 1 . . .
    300.64355 . 2 2 1 . . .
    300.64355 . 2 2 1 . . .
    300.64355 2 2 2 1 1 1 4
    300.64355 . 2 1 1 . . .
    300.64355 . 2 1 1 . . .
    300.64355 2 2 1 1 . . .
    300.64355 2 2 1 1 1 1 4
    300.64355 2 2 2 1 1 1 2
    300.64355 2 2 2 1 1 1 4
    300.64355 2 2 1 1 . . .
    299.02875 2 2 1 1 . . .
    299.02875 1 2 2 1 . . .
    299.02875 . 2 1 1 . . .
    299.02875 1 2 2 1 . . .
    299.02875 2 2 2 1 . . .
    299.02875 . 2 2 1 . . .
    299.02875 . 2 1 1 . . .
    299.02875 2 2 2 1 . . .
    299.02875 2 2 1 1 . . .
    299.02875 . 2 1 1 . . .
    299.02875 . 2 1 1 . . .
    299.02875 . 2 2 1 . . .
    299.02875 . 2 1 1 . . .
    299.02875 . 2 1 1 1 2 2
     420.0078 . 2 2 1 . . .
     420.0078 . 2 1 1 . . .
     420.0078 . 2 2 1 1 1 2
     706.9612 . 2 2 1 . . .
     706.9612 . 2 2 1 1 2 2
     706.9612 2 2 2 1 1 2 2
     706.9612 2 2 1 1 1 2 2
     412.6564 . 2 1 1 1 1 1
     412.6564 . 2 2 1 . . .
     412.6564 . 2 2 1 . . .
     721.2489 2 2 1 1 2 1 4
     721.2489 1 2 2 1 2 1 4
     721.2489 . 2 2 1 2 1 4
     721.2489 . 2 2 1 . . .
     721.2489 2 2 2 1 2 1 4
     721.2489 . 2 1 1 1 3 3
     721.2489 2 2 2 1 2 1 4
     742.3904 . 2 2 1 1 1 4
     742.3904 . 2 2 1 1 1 4
     742.3904 . 2 2 1 . . .
     742.3904 . 2 2 1 1 2 3
     742.3904 2 2 1 1 1 2 3
     742.3904 . 2 2 1 1 2 4
     742.3904 1 2 2 1 1 2 4
     424.4521 . 2 2 1 1 1 4
     424.4521 . 2 1 1 . . .
     424.4521 1 2 2 1 1 1 4
     424.4521 . 2 2 1 . . .
     424.4521 . 2 2 1 1 2 3
     424.4521 . 2 2 1 1 2 3
     424.4521 2 2 1 1 1 1 4
     424.4521 2 2 2 1 1 1 4
     424.4521 2 2 2 1 1 1 4
     424.4521 . 2 1 1 1 1 4
     277.2007 . 2 2 1 . . .
     277.2007 1 2 1 1 1 2 3
    end
    label values feedingscheme feedingscheme5
    label def feedingscheme5 1 "nfscheme", modify
    label def feedingscheme5 2 "fscheme", modify
    label values race race5
    label def race5 2 "African", modify
    label values gender gender5
    label def gender5 1 "Male", modify
    label def gender5 2 "Female", modify
    label values geo geo5
    label def geo5 1 "Rural", modify

    Please advise.

    Regards
    Nthato


  • #2
    Replace
    Code:
    qui {
    with
    Code:
    {
    so that you can, perhaps, determine which command is failing.

    Comment


    • #3
      Thanks William for the response.

      I tried your advise but the commands are reacting as before. Actually, they do not run from qui {

      Is there perhaps something that I'm supposed to run before decomposing the concentration index?

      Comment


      • #4
        I suspect you did not correctly implement the change I had in mind. Let me try again with a slightly different change, and let me recommend that you begin by reading
        Code:
        help quietly
        to understand that the quietly block of code suppresses all informative output, and should be reserved for use on code that is running without errors.

        Replace your third block of code from post #1 with the following, where the text in red is the only change made to the code,
        Code:
        foreach x of global X { 
        noisily {  
        sca b_`x’ = _b[`x’]    
        corr rank `x’ [pw=wt], c  
        sca cov_`x’ = r(cov_12)      
        sum `x’ [aw=wt] 
        sca elas_`x’ = (b_`x’*r(mean))/m_obese  
        sca CI_`x’ = 2*cov_`x’/r(mean)   
        sca con_`x’ = elas_`x’*CI_`x’ 
        sca prcnt_`x’ = con_`x’/CI   
        } 
        di “`x’ elasticity:”, elas_`x’ 
        di “`x’ concentration index:”, CI_`x’ 
        di “`x’ contribution:”, con_`x’ 
        di “`x’ percentage contribution:”, prcnt_`x’
        }

        Comment


        • #5
          Hi William,

          I replaced qui with noisily and I still get the same error message that says varlist is not allowed. Global X gets defined but commands after that don't. Is it perhaps my variables that have a problem?
          Last edited by Nthato Ras; 20 Jun 2022, 13:22.

          Comment


          • #6
            Try this.
            Code:
            macro list X
            foreach x of global X {
            macro list _x
            noisily {  
            sca b_`x’ = _b[`x’]    
            corr rank `x’ [pw=wt], c  
            sca cov_`x’ = r(cov_12)      
            sum `x’ [aw=wt]
            sca elas_`x’ = (b_`x’*r(mean))/m_obese  
            sca CI_`x’ = 2*cov_`x’/r(mean)  
            sca con_`x’ = elas_`x’*CI_`x’
            sca prcnt_`x’ = con_`x’/CI  
            }
            di “`x’ elasticity:”, elas_`x’
            di “`x’ concentration index:”, CI_`x’
            di “`x’ contribution:”, con_`x’
            di “`x’ percentage contribution:”, prcnt_`x’
            }
            After you run it, rather than describing what you think has happened, possibly leaving out crucial details in the output whose importance you do not recognize, instead copy from the Stata Results window everything starting with the macro list X command and continuing to the end, and then paste it into your next reply here, surrounding it with code delimiters [CODE] and [/CODE] as you did in post #1 to present your code.
            Last edited by William Lisowski; 20 Jun 2022, 14:20. Reason: Corrected the second macro list command - to list local macros a leading underscore is required in front of the name

            Comment


            • #7
              Hi William,

              I went through the commands with a colleague and found that the problem was with the quotations - they were not STATA friendly so I amended them. And as obese is a binary variable, I ran a probit regression. After running the commands as per your advice #6, I got an error messsage "feedingscheme invalid name". I removed it to test the other independent variables and got the same message. Running the regression on its own works,

              About the variables

              Obese is a dependent binary variable. Feedingscheme and adogender is also a binary variable - whether a child participates in the school feeding program or not. adorace has 4 categories. quintile 5 categories

              Stating with Wagstaff CI

              Code:
              conindex obese [aweight=wt], rankvar(wquintile) wagstaff bounded limits (0 1)
              Decomposing

              Code:
              sca CI = r(CI)
              global X feedingscheme adorace adogender quintile
              qui dprobit obese $X[pw=wt], robust
              qui sum obese [aw=wt]
              sca m_obese=r(mean)
              
              foreach x of global X { 
              qui {  
              sca b_`x' = _b[`x']    
              corr rank `x' [aw=wt], c  
              sca cov_`x' = r(cov_12)      
              sum `x' [aw=wt] 
              sca elas_`x' = (b_`x'*r(mean))/m_obese  
              sca CI_`x' = 2*cov_`x'/r(mean)   
              sca con_`x' = elas_`x'*CI_`x' 
              sca prcnt_`x' = con_`x'/CI   
              } 
              di “`x' elasticity:”, elas_`x' 
              di “`x' concentration index:”, CI_`x'
              di “`x' contribution:”, con_`x' 
              di “`x' percentage contribution:”, prcnt_`x'
              }
              The results:

              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input float feedingscheme byte(race gender geo quintile)
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 0 1 .
              . 0 0 1 .
              . 0 0 1 .
              . 0 0 1 .
              . 0 0 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              0 3 0 1 5
              . 3 0 . .
              . 3 0 1 .
              . 3 0 1 .
              . 3 0 1 .
              . 0 0 1 .
              . . . . .
              . . . . .
              . . . . .
              . . . . .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 3 0 1 .
              . . . . .
              . . . . .
              . . . . .
              . . . . .
              0 1 1 1 1
              . 1 1 1 .
              . 1 1 1 .
              . 1 1 1 .
              . 1 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 1 1 1 .
              . 1 1 1 .
              . 1 1 1 .
              . 1 1 1 .
              . 1 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 0 1 1 .
              . 1 1 1 .
              . 1 1 1 .
              . 1 1 1 .
              . 1 1 1 .
              . 1 1 1 .
              . 1 1 1 .
              . 1 1 1 .
              . 1 1 1 .
              . 1 1 1 .
              . 1 1 1 .
              . 1 0 1 .
              . 1 0 1 .
              . 1 0 1 .
              . 1 0 1 .
              . 1 0 1 .
              0 1 0 0 1
              1 1 0 1 1
              1 1 0 1 3
              . 1 0 1 .
              . 1 0 1 .
              . 1 0 1 .
              . 1 0 1 .
              . 1 0 1 .
              . 1 0 1 .
              . 1 0 1 .
              0 1 1 1 4
              0 1 1 1 3
              . 1 1 1 .
              . 1 1 1 .
              . 1 1 1 .
              end
              label values feedingscheme feedingscheme5
              label def feedingscheme5 0 "nfscheme", modify
              label def feedingscheme5 1 "fscheme", modify
              label values race race5
              label def race5 0 "White", modify
              label def race5 1 "African", modify
              label def race5 3 "Asian_Indian", modify
              label values gender gender5
              label def gender5 0 "Male", modify
              label def gender5 1 "Female", modify
              label values geo geo5
              label def geo5 0 "Rural", modify
              label def geo5 1 "Urban", modify
              Please advise where I am going wrong.

              Thanks
              Nthato

              Comment


              • #8
                You don't tell us which command is producing the error message. As I suggested in post #6, "quietly" should not be used in code that is not working, because it prevents the output of information that could help locate the problem.

                Replace each of the three "qui" in your code with "noisily" and try again.

                Comment


                • #9
                  Hi William,

                  The whole thing runs but ends with the message saying "feedingscheme is an invalid name". Below are the results

                  sca CI = r(CI)

                  . global X feedingscheme adorace adogender quintile

                  . dprobit obese $X[pw=wt], robust

                  (sum of wgt is 11.70994088474708)
                  Iteration 0: log pseudolikelihood = -5070.8401
                  Iteration 1: log pseudolikelihood = -4990.6154
                  Iteration 2: log pseudolikelihood = -4989.9362
                  Iteration 3: log pseudolikelihood = -4989.9361

                  Probit regression, reporting marginal effects Number of obs = 18075
                  Wald chi2(4) = 39.71
                  Prob > chi2 = 0.0000
                  Log pseudolikelihood = -4989.9361 Pseudo R2 = 0.0160

                  ------------------------------------------------------------------------------
                  | Robust
                  obese | dF/dx Std. Err. z P>|z| x-bar [ 95% C.I. ]
                  ---------+--------------------------------------------------------------------
                  feedin~e*| -.0307196 .0102161 -3.26 0.001 .601916 -.050743 -.010696
                  adorace | .0029533 .0083706 0.35 0.725 1.11292 -.013453 .019359
                  adogen~r*| .0093582 .0089304 1.06 0.291 .520279 -.008145 .026861
                  quintile | .0137517 .0029832 5.30 0.000 4.07653 .007905 .019599
                  ---------+--------------------------------------------------------------------
                  obs. P | .0807282
                  pred. P | .0774942 (at x-bar)
                  ------------------------------------------------------------------------------
                  (*) dF/dx is for discrete change of dummy variable from 0 to 1
                  z and P>|z| correspond to the test of the underlying coefficient being 0

                  . sum obese [aw=wt]

                  Variable | Obs Weight Mean Std. Dev. Min Max
                  -------------+-----------------------------------------------------------------
                  obese | 20,341 13.2014426 .0827354 .2754886 0 1

                  . sca m_obese=r(mean)

                  .
                  . foreach x of global X {
                  2. {
                  3. sca b_`x' = _b[`x']
                  4. corr rank `x' [aw=wt], c
                  5. sca cov_`x' = r(cov_12)
                  6. sum `x' [aw=wt]
                  7. sca elas_`x' = (b_`x'*r(mean))/m_obese
                  8. sca CI_`x' = 2*cov_`x'/r(mean)
                  9. sca con_`x' = elas_`x'*CI_`x'
                  10. sca prcnt_`x' = con_`x'/CI
                  11. }
                  12. di “`x' elasticity:”, elas_`x'
                  13. di “`x' concentration index:”, CI_`x'
                  14. di “`x' contribution:”, con_`x'
                  15. di “`x' percentage contribution:”, prcnt_`x'
                  16. }
                  (sum of wgt is 11.70994088474708)
                  (obs=18,075)

                  | rank feedin~e
                  -------------+------------------
                  rank | .085184
                  feedingsch~e | -.056647 .239626


                  Variable | Obs Weight Mean Std. Dev. Min Max
                  -------------+-----------------------------------------------------------------
                  feedingsch~e | 20,207 12.9771066 .5967131 .4905696 0 1
                  feedingscheme invalid name
                  r(198);

                  end of do-file

                  r(198);

                  Comment


                  • #10
                    There are a number of lessons from this, some for you, some for those who post questions on Statalist, and some for those who answer questions on Statalist.

                    The first lesson, for you, is that in the same way that you had to change the smart single quote in
                    Code:
                     `x 
                    to the standard single quote
                    Code:
                     `x' 
                    you need to change the smart double quotes in
                    Code:
                     di ...
                    to the standard double quotes
                    Code:
                     di "..."
                    At the bottom of this reply I have posted a fuller explanation. The bottom line is that you have to take great care when copying code from a PDF, or when composing code in an editor that makes "smart quotes" automatically.

                    The second lesson, for those like me who answer questions here, is that if your first step is to copy code from the post and paste it into Stata's Do-file editor, and you are running Stata 17, then this sort of problem will be hidden from your vision because Stata 17 and later quietly corrects "smart quotes" when you paste. I'm chagrined that I repeatedly overlooked this problem for that reason.

                    The third lesson, for all who post questions, is that if your code is copied from some other source - such as the Stata Journal, perhaps - then it is useful to tell us that is the case, and to give a citation to the publication. The reason for that is there's an implicit assumption that the code you post reflects your abilities with Stata, and - because experienced programmers don't need a recitation of points they are long since famliar with - some new-user mistakes, like these, are overlooked by those trying to help. You did tell us you are using Stata 16; I neglected to think through the implications of that.

                    And the fourth lesson, again for all who post questions, is a reiteration that "quietly" should be replaced by "noisily" if your program fails and you cannot tell where it failed at. In this case, I could see that the code had successfully passed the summarize command, and the very subtle error message that begins with a smart quote before the variable name then led me to the problem.

                    About typography

                    To have Stata substitute the value of a local macro
                    Code:
                     shortcut
                    you need to type on your keyboard
                    Code:
                     `shortcut'
                    where the first character is (on an American English keyboard) the "left single quote" beneath the tilde (~) character below the ESC key, and the final character is the usual "single quote" ("apostrophe") just to the left of the RETURN key. Neither of these are "smart quotes" as understood in programs like Microsoft Word, and in particular the first character is correctly titled the "grave accent".

                    Note that the Stata PDF documentation and Stata Journal, and perhaps other publications, display local macro references incorrectly using smart single quotes as
                    Code:
                     ‘shortcut’
                    which – in releases of Stata prior to Stata 17 – will not work if copied and pasted into Stata. Stata 17 includes a preference setting (on by default) to replace pasted curly quotes with ASCII quotes, which corrects the pasted code.
                    Last edited by William Lisowski; 23 Jun 2022, 07:51.

                    Comment


                    • #11
                      After reading William Lisowski 's post #10 , I can reproduce Nthato Ras 's error without needing Ras's data or variable names or any of Ras's detailed elasticity calculations which use macros. Using Stata 16, my code is

                      Code:
                      version
                      sysuse auto, clear
                      scalar elas_price = 1.5
                      di “price elasticity:”, elas_price
                      Here is the result:
                      Code:
                      . version
                      version 16.0
                      
                      . sysuse auto, clear
                      (1978 Automobile Data)
                      
                      . scalar elas_price = 1.5
                      
                      . di “price elasticity:”, elas_price
                      “price invalid name
                      r(198);
                      The error generated by my -display- command is identical to the one displayed in Nthato Ras 's output in post #9, probably generated by line number 12 in that code which reads:

                      Code:
                      12. di “`x' elasticity:”, elas_`x'
                      Stata 16 says that the character string - “price - is an invalid name. When I follow William Lisowski 's advice to replace the "curly" double-quotes in the -display- command with the standard ASCII quotes found on American keyboards, the error disappears as follows:

                      Code:
                      . version
                      version 16.0
                      
                      . di "price elasticity:", elas_price
                      price elasticity: 1.5
                      To see how I eliminated the error, look closely at the difference in my two display commands. The first command uses curly double-quotes, which versions of Stata prior to 17 do not recognize as a legitimate character, while the second -display- command uses the standard ASCII double quotes and works in all versions of Stata. I'm pleased to learn from this thread about Stata's previous failure to recognize the "curly" single and double quotes and about Stata 17's innovation of replacing these characters with their ASCII equivalents during some copy-paste operations. Also see:

                      Code:
                      help quotes

                      Last edited by Mead Over; 23 Jun 2022, 13:20.

                      Comment


                      • #12
                        Thank you William!

                        It worked!!!!!

                        Comment


                        • #13
                          I tried the same programme for my study where: dependent_var (binary); independent_variable (categorical) the program considers rank as a variable and output shoe variable rank not found.

                          HTML Code:
                          conindex obs [aweight=wt], rankvar(v191) wagstaff bounded limits (0 1) 
                          
                          sca CI = r(CI)
                          global x v025 v151 s116 v106
                          
                          regr obs $x [pw=wt], robust
                          
                          sum obs [aw=wt]
                          sca m_obs=r(mean)
                          
                          
                          foreach x of global x { 
                          qui {  
                          sca b_`x' = _b[`x']    
                          corr   rank `x' [aw=wt], c  
                          sca cov_`x' = r(cov_12)      
                          sum `x' [aw=wt] 
                          sca elas_`x' = (b_`x'*r(mean))/m_obs  
                          sca CI_`x' = 2*cov_`x'/r(mean)   
                          sca con_`x' = elas_`x'*CI_`x' 
                          sca prcnt_`x' = con_`x'/CI   
                          } 
                          di "`x' elasticity:", elas_`x' 
                          di "`x' concentration index:", CI_`x'
                          di "`x' contribution:", con_`x' 
                          di "`x' percentage contribution:", prcnt_`x'
                          }
                          Best regards,
                          Mukesh

                          Comment

                          Working...
                          X