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

  • IGM userwritten program

    Dear friends, I am trying on the stata command, igmobil with upward mobile index using a written program.The code is as below,
     matrix C = (.25, .5*.25 \ .5*.25, .25)
    set seed 12345
    drawnorm u0 u1, n(2000) cov(C) /* normal r.v. */
     generate son = exp(u1) /* lognormal r.v.*/
     generate dad = exp(u0)
     generate son_disc = irecode(u1, -1, -0.5, 0, 0.5, 1) /*discrete r.v.*/
     generate dad_disc = irecode(u0, -1, -0.5, 0, 0.5, 1)
     drop u*
    capture program drop myindex
    program myindex, rclass
    syntax varlist(min=2 max=2 numeric) [if] [in] [, tau(real 0) s(real 0.25)]
    marksample touse
    tempvar y x ry rx diff
    tempname num den
    tokenize ‘varlist’
    quietly {
    generate ‘y’ = ‘1’ if ‘touse’
    generate ‘x’ = ‘2’ if ‘touse’
    cumul ‘y’, gen(‘ry’)
    cumul ‘x’, gen(‘rx’)
    count if (‘ry’-‘rx’) > ‘tau’ & ‘rx’ <= ‘s’ & ‘touse’
    scalar ‘num’ = r(N)
    count if ‘rx’ <= ‘s’ & ‘touse’
    scalar ‘den’ = r(N)
    return scalar UW = ‘num’/‘den’
     igmobil son dad, nosingle noinequal userwritten(myindex son dad, tau(0.1) s(0.25)) classes(4)
    It is from the official help file. But it returns some error. I do not understand what the usage of the lines

    marksample touse
    tempvar y x ry rx diff
    Thank you so much!

  • #2
    In what you have posted, the back-quote and the apostrophe characters are both wrong. For example, you should have
    generate `y' = `1' if `touse'
    generate ‘y’ = ‘1’ if ‘touse’

    You might try fixing those characters and see if that solves the problem. Beyond that, the fact that you said "it returns some error" makes it quite difficult for me or anyone else to help, since we don't know that that error is.

    Also: Are you sure you wanted to post this question in the Mata section of StataList?


    • #3
      Dear Professor Mike,

      Thank you so much for your invaluable help! I managed to execute it. I am still a bit confused about the usage of the command line,

       marksample touse  tempvar y x ry rx diff
      Why does it apply "marksample" and "tempvar" here? Thank you in advance!


      • #4
        As Mike implies, you have posted your topic in Statalist's Mata Forum, which is used for discussions of Stata's Mata language, which is different than Stata's command language, and different than Stata's matrix commands, which are what your program uses. Your question woiuld have sees a more appropriate, and much larger audience if you had posted it in Statalist's General Forum.

        With that said, I also agree with his analysis. To have Stata substitute the value of a local macro
        you need to type on your keyboard
        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".

        Your problem stems from the Stata PDF documentation and the Stata Journal - which includes the article introducing the community-contributed igmobil command - unfortunately displaying local macro references incorrectly using smart single quotes as
        which leads to no end of confusion, including yours, because such code will not work if copied and pasted into Stata. On the other hand, it does look elegant in the printed documentation.


        • #5
          I have run the program without problems.


          • #6
            Regarding the questions in post #3, the best place to look for answers are in the output of
            help marksample
            help tempvar
            and the Stata PDF documentation linked to from the top of each of those outputs.

            Perhaps you are new to using Stata's program command to write commands (as opposed to creating do-files of executable commands). If so, you should take the time to read and understand Chapter 18 Programming Stata in the Stata User's Guide PDF included with your Stata installation and accessible through Stata's Help menu. Then you'll have a better understanding of how the program you copied does its work.


            • #7
              Dear Professor Lisowski, thank you so much for your great help! Might I have another humble request? I would like to include the option of sample weight when it is survey data. Where should I add it in for the myindex program? Thank you so much!


              • #8
                The first step is understanding how the myindex program would make use of the weights. I can't advise you about that, that's a substantive question that you have to answer. Do weights make sense on the cumul command? And weights are not allowed on the count command, is that a problem?

                Once you understand that, the syntax command in myindex would be changed to include the possibility of weights, as described in
                help syntax
                and the the appropriate [weight] option would be added in the userwritten() option of the igmobil command where your myindex program is referenced.


                • #9
                  Dear Professor, sorry, I am very lost now!


                  • #10
                    What William Lisowski means is that it is not easy to add sample weights to the myindex program in a meaningful way. It is easy to amend the syntax of the program to allow sample weights but the rest of the program needs also changes to make use of these sample weights.
                    So it is up to you to decide where you need sample weights and where they make sense. Without knowing the wider context of this program, it is very difficult for us to say how you have to modify the program.
                    If you understand sample weights as the probability weights( = the probability of sampling an individual) then you cannot add sample weights because no command within the myindex program understands this kind of weight.