Announcement

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

  • #16

    Code:
    corr r `X' [fw=hhw], c
    is my best guess at the problematic command. Very likely, you have several variables whose names begin with r.

    Code:
    describe r*
    will tell you.

    Comment


    • #17
      Hi there,

      I'm having the same error message but am having trouble using these suggestions to solve my problem. Here's my task: I am looping a list of keywords through the "lookfor" command and appending the return list to an empty local macro.

      I can't figure out how to add in code the way you all do, it actually froze my computer when I tried clicking some of the "source" button- i'm not sure if that was the right one so forgive me. Please keep in mind that this portion of the code is embedded in another loop that loops through 20 different years of data and I'm trying to identify common variables. I'm pretty sure that the last two lines are causing the "varlist not allowed" error r(101). Thanks. Here goes:


      lookfor unique
      return list
      local savevars=r(varlist)

      local keywords "hunger pension food"

      foreach keyword of local keywords{
      print("`keyword'")
      lookfor `keyword'
      quietly return list
      savevars= "`savevars'"+" "+ r(varlist)
      }

      Comment


      • #18
        I've not seen the print command in an age but at best it prints a file. Perhaps your code should end

        Code:
        foreach keyword of local keywords {
            display "`keyword'"
            lookfor `keyword'
            local savevars  `savevars'  `r(varlist)'
        }
        The most obvious error is omitting the command local.

        quietly return list asks for a display and then suppresses it, so does no harm but does nothing useful either.

        Comment


        • #19
          Thanks Nick Cox. This worked. I ended up with:

          lookfor unique
          return list
          local savevars=r(varlist)

          local keywords "hunger pension food"
          foreach keyword of local keywords{
          quietly display "`keyword'"
          quietly lookfor `keyword'
          local savevars `savevars' r(varlist)
          }

          Comment


          • #20
            If that's your final code it needs fixing. Each time around the loop the literal text r(varlist) will get added to the local macro.


            Code:
            local savevars `savevars' `r(varlist)'


            Comment


            • #21
              Hi,

              My goal is to run a loop that is similar to #11 and #12.

              I have the following global list of x variables:

              Code:
               global X "age fem sah hc mob sym"
              I then use the following loop:

              Code:
              set trace on 
              
              . foreach x of global X {
                2. qui {
                3. scal b_`x' = _b[`x']
                4. corr rank `x' [aw=dw_w6], c
                5. sca cov_`x' = r(cov_12)
                6. sum `x' [aw=dw_w6]
                7. sca elas_`x' = (b_`x'*r(mean))
                8. sca CI_`x' = 8*cov_`x'
                9. sca CI_`x' = 2*cov_`x'/r(mean)
               10. sca con_`x' = 4*(elas_`x'*CI_`x')
               11. sca prcnt_`x' = (con_`x'/CI_`x')*100
               12. }
               13. di “`x' elasticity:”, elas_`x'
               14. di “`x' concentration index:”, CI_`x'
               15. di “`x' contribution:”, con_`x'
               16. di “`x' percentage contribution:”, prcnt_`x'
               17. matrix Aaa = nullmat(Aaa)\///
               18. (elas_`x', CI_`x', con_`x', prcnt_`x')
               19. }
              The trace shows me that after line 13, "age is an invalid name". If I use the pre-specified code as shown by #12, I get the error "CI not found", like was the case for #12.
              Steps 1-3 like in #11 work just fine, it is the loop in particular that I cannot seem to get to work.

              Thanks in advance for the help!






              Comment


              • #22
                In the future, please post your example data using dataex as well as the code as seen in your do file, not the Stata terminal.

                Your issue here is that you need to use
                Code:
                `=CI_`x''
                See the reproducible example
                Code:
                sysuse auto, clear
                cls
                foreach x of var wei head {
                qui corr pri `x'
                
                sca pearr_`x' = r(rho)
                
                scatter pri `x', caption(`=pearr_`x'')
                
                di "correlation is `=pearr_`x''"
                }
                Welcome to Statalist, Siebe Dibbets

                Comment


                • #23
                  Originally posted by Jared Greathouse View Post
                  In the future, please post your example data using dataex as well as the code as seen in your do file, not the Stata terminal.

                  Your issue here is that you need to use
                  Code:
                  `=CI_`x''
                  See the reproducible example
                  Code:
                  sysuse auto, clear
                  cls
                  foreach x of var wei head {
                  qui corr pri `x'
                  
                  sca pearr_`x' = r(rho)
                  
                  scatter pri `x', caption(`=pearr_`x'')
                  
                  di "correlation is `=pearr_`x''"
                  }
                  Welcome to Statalist, Siebe Dibbets
                  Thanks for the warm welcome, Jared. Here is an example of the data (y-variable and x-variables used for global):

                  Code:
                  * Example generated by -dataex-. To install: ssc install dataex
                  clear
                  input byte y1skc int age byte(fem sah) float(hc mob sym)
                  0 77 1 1 0 4 1
                  0 77 0 3 1 2 0
                  0 89 0 4 1 2 0
                  0 69 1 4 1 1 0
                  0 89 1 3 3 1 1
                  0 79 1 5 1 5 4
                  0 81 1 2 2 0 1
                  0 82 0 2 1 2 0
                  0 81 0 3 1 0 0
                  0 71 1 2 1 0 0
                  end
                  label values y1skc dummi
                  label def dummi 0 "Not selected", modify
                  label values age age
                  label values fem gender
                  label def gender 0 "male", modify
                  label def gender 1 "female", modify
                  label values sah rate
                  label def rate 1 "Excellent", modify
                  label def rate 2 "Very good", modify
                  label def rate 3 "Good", modify
                  label def rate 4 "Fair", modify
                  label def rate 5 "Poor", modify
                  I was able to incorporate your suggestion (use `=CI_`x'') into the probit model I am trying to estimate.

                  Code:
                  *MEASURING HORIZONTAL EQUITY
                  
                  qui probit y2unskc $X $Z [pw=dw_w6]
                  foreach z of global Z {
                  gen copy_`z’=`z’
                  qui sum `z’ [aw=dw_w6]
                  replace `z’ = r(mean)
                  }
                  predict yhat
                  foreach z of global Z {
                  replace `z’ = copy_`z’
                  drop copy_`z’
                  }
                  sum m_yhat [aw=dw_w6]
                  gen yst = y-yhat + r(mean)
                  
                  *EXPLAINING HORIZONTAL EQUITY
                  
                  dprobit y1skc $X $Z [pw=dw_w6]
                  matrix dfdx=e(dfdx)
                  
                  sca need=0
                  foreach x of global X{
                  qui{
                      mat b_`x' = dfdx[1,"`x'"]
                      sca b_`x' = b_`x'[1,1]
                      corr r `x' [aw=dw_w6], c
                      sca cov_`x' = r(cov_12)
                      sum `x' [aw=dw_w6]
                      sca m_`x' = r(mean)
                      sca elas_`x' = (b_`x'*m_`x')/m_y
                      sca CI_`x' = 2*cov_`x'/m_`x'
                      sca con_`x' = elas_`x'*`=CI_`x''
                      sca prcnt_`x' = con_`x'/`=CI_`x''
                      sca need=need+con_`x'
                  }
                  di "`x' elasticity:", elas_`x'
                  di "`x' concentration index:", CI_`x'
                  di "`x' contribution:", con_`x'
                  di "`x' percentage contribution:", prcnt_`x'
                  }
                  di "Inequality due to need factors:", need

                  In measuring horizontal equity, I got the result for yhat (need-predicted utilization of skilled care) after removal of "m_".

                  Similarly, the loop for explaining horizontal equity won't be run as it says "m_y" cannot be found.

                  It does display the concentration index of the variables if I substitute "m_y" with "y1skc" (my y-variable), but then the elasticity and contribution are displayed as missing. It won't run m_y1skc either.

                  So, it seems that the elasticity is not produced for some reason, which makes that the contribution isn't either, since that is a product of x' concentration index and its elasticity with respect to y. Does it have something to do with this "m_", like suggested by the errors encountered by running the code for measurement of horizontal equity, or is it something else?

                  Any help will be much appreciated!







                  Comment


                  • #24
                    I'll look at this in a few minutes, soon as I've gotten up, gotten dressed, and made coffee

                    Comment


                    • #25
                      I can't replicate any of this. All of the outcomes are false, so my code doesn't even get past the logit regression.
                      Code:
                      * Example generated by -dataex-. To install: ssc install dataex
                      clear
                      qui {
                      input byte y1skc int age byte(fem sah) float(hc mob sym)
                      0 77 1 1 0 4 1
                      0 77 0 3 1 2 0
                      0 89 0 4 1 2 0
                      0 69 1 4 1 1 0
                      0 89 1 3 3 1 1
                      0 79 1 5 1 5 4
                      0 81 1 2 2 0 1
                      0 82 0 2 1 2 0
                      0 81 0 3 1 0 0
                      0 71 1 2 1 0 0
                      end
                      label values y1skc dummi
                      label def dummi 0 "Not selected", modify
                      label values age age
                      label values fem gender
                      label def gender 0 "male", modify
                      label def gender 1 "female", modify
                      label values sah rate
                      label def rate 1 "Excellent", modify
                      label def rate 2 "Very good", modify
                      label def rate 3 "Good", modify
                      label def rate 4 "Fair", modify
                      label def rate 5 "Poor", modify
                      }
                      
                       loc xs age fem sah hc mob sym
                      
                      *MEASURING HORIZONTAL EQUITY
                      
                      qui probit y1 `xs'
                      foreach x of loc xs {
                      gen copy_`x'=`x'
                      qui sum `x'
                      replace `x' = r(mean)
                      }
                      You also don't provide the variables in the global Z. So for us to meaningfully come to a solution, I need a fully reproducible example with data and code. That is, you need to provide a minimal worked example which gets us to the same point and the same error code.

                      Comment


                      • #26
                        Originally posted by Jared Greathouse View Post
                        I can't replicate any of this. All of the outcomes are false, so my code doesn't even get past the logit regression.
                        Code:
                        * Example generated by -dataex-. To install: ssc install dataex
                        clear
                        qui {
                        input byte y1skc int age byte(fem sah) float(hc mob sym)
                        0 77 1 1 0 4 1
                        0 77 0 3 1 2 0
                        0 89 0 4 1 2 0
                        0 69 1 4 1 1 0
                        0 89 1 3 3 1 1
                        0 79 1 5 1 5 4
                        0 81 1 2 2 0 1
                        0 82 0 2 1 2 0
                        0 81 0 3 1 0 0
                        0 71 1 2 1 0 0
                        end
                        label values y1skc dummi
                        label def dummi 0 "Not selected", modify
                        label values age age
                        label values fem gender
                        label def gender 0 "male", modify
                        label def gender 1 "female", modify
                        label values sah rate
                        label def rate 1 "Excellent", modify
                        label def rate 2 "Very good", modify
                        label def rate 3 "Good", modify
                        label def rate 4 "Fair", modify
                        label def rate 5 "Poor", modify
                        }
                        
                        loc xs age fem sah hc mob sym
                        
                        *MEASURING HORIZONTAL EQUITY
                        
                        qui probit y1 `xs'
                        foreach x of loc xs {
                        gen copy_`x'=`x'
                        qui sum `x'
                        replace `x' = r(mean)
                        }
                        You also don't provide the variables in the global Z. So for us to meaningfully come to a solution, I need a fully reproducible example with data and code. That is, you need to provide a minimal worked example which gets us to the same point and the same error code.
                        Thank you for getting back to me, Jared. I see why you weren't able to reproduce my errors. I've been able to solve them by applying a few steps I had overlooked in the guidelines. Nevertheless grateful for the time!

                        Comment

                        Working...
                        X