Announcement

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

  • How to store results saved in r() as new variable

    Hi, I am running the following package from SSC.

    Code:
    sort year country
    by year country : duncan occupation gender
    The results are saved in r()

    Code:
    return list
    
    scalars:
                      r(c) =  25
                      r(N) =  2228
    
    matrices:
                      r(D) :  2 x 2
    How can i save the indices stored as a new variable?

    I am calculating occupational sex segregation, by survey year and country.


    Last edited by Sherine Maui; 24 Jul 2021, 10:02.

  • #2
    You cannot save it this way, because every next round overwrites the saved results from the previous round.

    You either have to use -statsby- to collect the results, or the user written -runby-, or you need to write a loop of the type described in this FAQ: https://www.stata.com/support/faqs/d...ry-statistics/

    Comment


    • #3
      Joro gives excellent advice. Your specific problem is that the results you want most, I guess, are in a matrix. So, you need to write code to extract what you want from the matrix.

      Comment


      • #4
        Thank you Joro and Nick for the helpful advice. I have tried to write a loop and it runs with no error:

        Code:
        egen category = group(country year)          
        local r =   r(r)
        
            forvalues i = 1/`r' {
            preserve
            qui keep if category==`i'
            local year = year[1]
            local country = country[1]      
            dis as text "-------------------------"
            dis as res "Factor: female (0) vs male (1)"
            dis as text "Stratified by:"
            dis as text "Country: " as res "`country'" as text " Year: " as res `year'     
            qui duncan occupation gender                                      
            matrix R = r(D)
            dis as text "Duncan's index: "  as res R[2,1]
            dis as text "Num. categories: " as res r(c)
            dis as text "Num. observations: " as res  r(N)
            restore
            local duncan_index_`i'  = R[2,1]
            }
            
        
            gen duncan1 = .
            
            forvalues j = 1/`r' {
            qui replace duncan1 = `duncan_index_`j'' if category==`j'
            }

        Comment


        • #5
          Are you reporting on your great success, or there is a "but I do not get what I want" statement missing?

          In other words Is what you have done the correct solution to the problem?

          Originally posted by Sherine Maui View Post
          Thank you Joro and Nick for the helpful advice. I have tried to write a loop and it runs with no error:

          Code:
          egen category = group(country year)
          local r = r(r)
          
          forvalues i = 1/`r' {
          preserve
          qui keep if category==`i'
          local year = year[1]
          local country = country[1]
          dis as text "-------------------------"
          dis as res "Factor: female (0) vs male (1)"
          dis as text "Stratified by:"
          dis as text "Country: " as res "`country'" as text " Year: " as res `year'
          qui duncan occupation gender
          matrix R = r(D)
          dis as text "Duncan's index: " as res R[2,1]
          dis as text "Num. categories: " as res r(c)
          dis as text "Num. observations: " as res r(N)
          restore
          local duncan_index_`i' = R[2,1]
          }
          
          
          gen duncan1 = .
          
          forvalues j = 1/`r' {
          qui replace duncan1 = `duncan_index_`j'' if category==`j'
          }

          Comment


          • #6
            I do not think that what you have written is doing the job.

            Try and see whether this code is not doing what you want:

            Code:
            egen category = group(country year)
            gen duncan_index = .
            gen num_categories = .
            gen num_obs = .
            summ category, meanonly
            
            forvalues i = 1/`r(max)' {
            qui duncan occupation gender if category==`i'
            matrix R = r(D)
            replace duncan1 = R[2,1] if category==`i'
            replace num_categories = r(c) if category==`i'
            replace num_obs =  r(N) if category==`i'
            }

            Comment


            • #7
              First time trying to loop and overcomplicated my code. Yours does what I need. Thank you Joro.

              Comment


              • #8
                You are welcome.

                I cut out your "display" statements to simplify, and because you have all the information now saved.

                Once you have all the information saved in the variables you can prepare something that a call a List Table, and display the values of the index and other quantities by country and year. Like this:

                Code:
                sort country year
                egen tag = tag(country year)
                
                list country year duncan_index num_categories num_obs if tag
                This will report the results succinctly.

                Note also that I have a type (I guess you found the typo if you say that the code works), but for the progeny, the code should be:

                Code:
                egen category = group(country year)
                gen duncan_index = .
                gen num_categories = .
                gen num_obs = .
                summ category, meanonly
                
                forvalues i = 1/`r(max)' {
                qui duncan occupation gender if category==`i'
                matrix R = r(D)
                replace duncan_index = R[2,1] if category==`i'
                replace num_categories = r(c) if category==`i'
                replace num_obs =  r(N) if category==`i'
                }
                what is in red was the typo above.

                Originally posted by Sherine Maui View Post
                First time trying to loop and overcomplicated my code. Yours does what I need. Thank you Joro.

                Comment

                Working...
                X