Announcement

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

  • Storing results from duncan command into a new variable

    Dear all,

    I am using a community-written command called duncan by Ben Jann.

    I am trying to calculate the Duncan's dissimilarity index for each region in each year. I have individual data of people - their occupation, gender, region and year from the survey.

    To get all the Duncan's values for each Region in each year I use:

    Code:
    by REGION SURVEYYEAR: duncan OCCUPATION GENDER
    This gives me all the indexes by region and year - but I don't know how to store this all as one variable. I read up the help sheet and it says that the value is stored in r(c) or something - but I'm not sure how I can create a variable which contains the index value for the observation depending on which region and Year that observation is from. When I type r(c) I only get the value for the latest Region Year value it runs.

    Any help will be very much appreciated!
    Last edited by John Ash; 15 May 2020, 11:34.

  • #2
    This command by Ben Jann (not Jaan) is from SSC. I have not used it, which means no more than it says. Possibly statsby may help to save scalars, but the bigger deal is what you (intend to) do with matrices produced. -- unless the matrix produced is (1, 1). As you don't given example data or results it is hard to guess.


    Comment


    • #3
      Thank you for the reply. Will edit the name on the last post.

      I intend to store the single results - i.e. the single value for the Duncan's dissimilarity index for that particular observation depending on the person's year & region -as a new variable.
      PERSON Gender Region YEAR DUNCAN
      1 M 1 2019 0.5
      2 F 1 2019 0.5
      3 M 1 2017 0.4
      4 F 1 2017 0.4
      5 F 2 2017 0.39
      6 M 3 2019 0.86
      7 F 3 2019 0.86
      I don't think a matrix storage will be useful for me.

      Comment


      • #4
        Try something like the following (untested). The idea is to loop over the combinations of year and region and calculate the dissimilarity index for each separately. I've used -egen- to create a new variable to loop over. Alternatively you could loop directly over year and over region.

        Code:
        egen year_region = group(YEAR REGION) label
        sum year_region
        local max = r(max)
        ge dissindex = .
        forval x = 1/`max'  {
            duncan  OCCUPATION GENDER if year_region == `x'
            replace dissindex = r(D) if year_region == `x'
        }

        Comment


        • #5
          Thanks a lot for that! I ended up doing it all manually - but will be great to try out in the future for a similar problem.

          Comment

          Working...
          X