Announcement

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

  • Trying to calculate an index

    Hi,

    I'm aware that the -duncan- command exists, but I need to do this manually for some other reasons.

    So I'm trying to calculate the Duncan's dissimilarity index using this formula here:

    https://en.m.wikipedia.org/wiki/Dunc...regation_Index



    I have the variables mi, fi, M and F. However, I'm struggling how to incorporate that sigma notation in an equation for Stata. Completely at a loss here.

    Any help would be much appreciated!

  • #2
    On the assumption that the index i refers to observations:

    Code:
    egen duncan = total(abs(mi/M - fi/F))
    replace duncan = 0.5*duncan
    will create a new "variable" containing your index. I put it in quotes because this "variable" is actually a constant. Unstated in your question is whether this calculation is carried out separately for various subsets of the data, such as firms. In that case, you can get a real variable that contains the firm's value by just prefixing -by firm_id, sort:- to the -egen- command (replacing firm_id by whatever variable identifies firms.)

    If, in fact, you really need to calculate just one value for the entire data set, then I would use a different approach:

    Code:
    gen dunc = abs(mi/M - fi/F)
    summ dunc, meanonly
    local duncan_index = 0.5*`r(sum)'
    display "Duncan index = `duncan_index'"
    This will print the index value in the Results window and log file, as well as saving the value in a local macro, but not waste memory storing a copy for each observation.

    In the future, when asking for help with code, please post an example of your data set, using the -dataex- command. That way, those who want to help you will know everything necessary about your data to help you and not have to propose hypothetical names for other variables, etc.

    If you are running version 15.1 or a fully updated version 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.



    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      On the assumption that the index i refers to observations:

      Code:
      egen duncan = total(abs(mi/M - fi/F))
      replace duncan = 0.5*duncan
      will create a new "variable" containing your index. I put it in quotes because this "variable" is actually a constant. Unstated in your question is whether this calculation is carried out separately for various subsets of the data, such as firms. In that case, you can get a real variable that contains the firm's value by just prefixing -by firm_id, sort:- to the -egen- command (replacing firm_id by whatever variable identifies firms.)

      If, in fact, you really need to calculate just one value for the entire data set, then I would use a different approach:

      Code:
      gen dunc = abs(mi/M - fi/F)
      summ dunc, meanonly
      local duncan_index = 0.5*`r(sum)'
      display "Duncan index = `duncan_index'"
      This will print the index value in the Results window and log file, as well as saving the value in a local macro, but not waste memory storing a copy for each observation.

      In the future, when asking for help with code, please post an example of your data set, using the -dataex- command. That way, those who want to help you will know everything necessary about your data to help you and not have to propose hypothetical names for other variables, etc.

      If you are running version 15.1 or a fully updated version 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.



      When asking for help with code, always show example data. When showing example data, always use -dataex-.
      Hi,

      In the first method, abs() is not compatible with even.

      In the second one, it's saying "unknown function 'r ()".

      What should I do?

      EDIT: Sorted the second one out, didn't use the ASCII grave accent. My bad!
      Last edited by Alina Faruk; 08 Apr 2019, 00:44.

      Comment


      • #4
        Please copy and paste a data example and the exact code you used (http://www.statalist.org/forums/help#stata).

        Comment


        • #5
          Originally posted by Clyde Schechter View Post
          On the assumption that the index i refers to observations:

          Code:
          egen duncan = total(abs(mi/M - fi/F))
          replace duncan = 0.5*duncan
          will create a new "variable" containing your index. I put it in quotes because this "variable" is actually a constant. Unstated in your question is whether this calculation is carried out separately for various subsets of the data, such as firms. In that case, you can get a real variable that contains the firm's value by just prefixing -by firm_id, sort:- to the -egen- command (replacing firm_id by whatever variable identifies firms.)

          If, in fact, you really need to calculate just one value for the entire data set, then I would use a different approach:

          Code:
          gen dunc = abs(mi/M - fi/F)
          summ dunc, meanonly
          local duncan_index = 0.5*`r(sum)'
          display "Duncan index = `duncan_index'"
          This will print the index value in the Results window and log file, as well as saving the value in a local macro, but not waste memory storing a copy for each observation.

          In the future, when asking for help with code, please post an example of your data set, using the -dataex- command. That way, those who want to help you will know everything necessary about your data to help you and not have to propose hypothetical names for other variables, etc.

          If you are running version 15.1 or a fully updated version 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.



          When asking for help with code, always show example data. When showing example data, always use -dataex-.
          The second one worked beautifully. I got the exact same results as using -duncan-. Thanks a lot!

          Comment


          • #6
            Originally posted by Nick Cox View Post
            Please copy and paste a data example and the exact code you used (http://www.statalist.org/forums/help#stata).
            Problem sorted, I made a mistake. Thanks!

            Comment


            • #7
              Please, researchers, I need help with the creation of an index. I am working on panel data, and I needed to create an index for each country because the data is a panel, which I am finding difficult to do.
              I want to use PCA.

              Comment

              Working...
              X