Announcement

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

  • Excluding one item from the macro each time the loop is performed

    Hello everyone,

    This is my first time posting on the forum. Having read the FAQ, I must still apologize in prior for any unintended mistakes.

    I am using Stata/SE 14.0 for Mac on macOS High Sierra. I am working on a dataset consist of roughly 15,000 observations. It is from a birth cohort study on which I am studying the effect of sexual orientation on health outcome. I am running a multiple imputation model before the analyses due to high missingness. My question arised when I am running regression models for every variable I intend to impute, in order to determine whether to use linear regression or predictive mean matching (pmm) in the imputation model (effectively I am checking the distribution of residuals from the "dryrun"s of my imputation model).

    My diagnostic regressions look like this:
    Code:
    regress depression9 depression10 depression11 depression12 sexuality i.sex i.nonwhite i.ses edumum
    predict rep2, residuals
    pnorm rep2
    qnorm rep2
    
    regress depression10 depression9 depression11 depression12 sexuality i.sex i.nonwhite i.ses edumum 
    predict rep2, residuals
    pnorm rep2
    qnorm rep2
    
    regress depression11 depression9 depression10 depression12 sexuality i.sex i.nonwhite i.ses edumum  
    predict rep2, residuals
    pnorm rep2
    qnorm rep2
    
    regress depression12 depression9 depression10 depression11 sexuality i.sex i.nonwhite i.ses edumum  
    predict rep2, residuals
    pnorm rep2
    qnorm rep2
    I have simplified my codes to make them easier to read without missing my main point. The actual list of imputed variables are much longer than this and it is not very clear to read, so I wish to use a loop for this. However, instead of what "foreach" allows me to do, I do not want all the variables in the macro to repeat identically everytime the loop runs. The variable list should change slightly every time, i.e. one of the variable has to serve as the dependent variable (and hence NOT be one of the independent variables). What I envisioned is something like this:

    Code:
    loc imputed "depression9 depression10 depression11 depression12"
    loc regular "sexuality i.sex i.nonwhite i.ses edumum"
    foreach x of loc imputed {
    regress `x' `"`imputed' - `x'"' `regular'
    }
    Apparently this is incorrect and I was shown an error message as follow:
    Code:
    "depression9 depression10 depression11 depression12 - depression invalid name
    To be concise, I am wondering if there is a way for the foreach function to, while running, omit one variable from the macro at a time?
    Thank you very much.

    best regards,
    Kai-Yuan

  • #2
    You're reaching for syntax to subtract (meaning, remove) one list from another and it is to be found under help macrolists

    You could do this more concisely than below, but the cost would be clarity.

    Code:
    loc imputed "depression9 depression10 depression11 depression12"
    loc regular "sexuality i.sex i.nonwhite i.ses edumum"
    foreach y of loc imputed {
        local others : list imputed - y 
        regress `y' `others' `regular'  
    }
    To show what I mean:

    Code:
    loc imputed "depression9 depression10 depression11 depression12"
    loc regular "sexuality i.sex i.nonwhite i.ses edumum"
    foreach y of loc imputed {
        regress `y' `: list imputed - y'  `regular'  
    }
    There are at least two problems with your attempted syntax.

    1. The compound double quotes don't belong in the regress syntax.

    2. A - sign could be legal as part of a varlist but won't in that context subtract one macro's contents from another.

    Comment


    • #3
      Dear Nick,

      Thank you so much for the very helpful instructions. Both of them worked perfectly.
      I have two further inquires if you don't mind the additional trouble:

      1. Is my understanding correct that, while double quotes around a macname do denote the content of the macro, and one would assume that one can then start to manipulate these content (as they have now become a string of items), it actually does not allow such manipulations. This applies not only to the - sign but also to any other calculation.

      2. Comparing the two codes you provided, the more concise codes give the definition of the local others when the local appears for the first time in the codes. This seems to be a feature unique to macro lists as in the case of defining a macro with local, as shown in the example below:
      Code:
      loc others "sexuality i.sex i.nonwhite i.ses"
      regress depression9 `others'
      One will not be allowed to run the code below as a concise substitute:

      Code:
      regress depression9 ` "sexuality i.sex i.nonwhite i.ses" '

      Thank you again.


      best regards,
      Kai-Yuan

      Comment


      • #4
        On your questions:

        1. Some manipulations are allowed inside quotation marks. Here are some examples.


        Code:
        . local a Buda
        
        . local b pest
        
        . di "`a'`b'"
        Budapest
        
        . di "`= 2 + 2'"
        4
        2. Quotation marks are needed some of the time when displaying or supplying strings. It depends mostly on whether there is ambiguity over what is wanted.

        Consider

        Code:
        . local frog = 42
        
        . di "`frog'"
        42
        
        . di `frog'
        42
        
        . local toad "y"
        
        . di `toad'
        y not found
        r(111);
        
        . di "`toad'"
        y
        In the first example, there is no doubt that 42 should be displayed, so the quotation marks do no harm but are not essential.

        In the second example, it makes a big difference whether you want the literal contents of the macro to be shown or whether you want to see whatever is named within the macro (it could be a variable or scalar name: when I ran that code I had no such variable or scalar in memory).

        Your second example raises several questions, not least why you think that syntax might be either legal or helpful!

        Code:
         regress depression9 sexuality i.sex i.nonwhite i.ses
        would be fine to Stata, presuming such variables exist, but the quotation marks " " are alien to regress and the macro quotation marks are pointless because there is no macro name or expression to be evaluated between them,
        Last edited by Nick Cox; 12 Apr 2018, 09:02.

        Comment


        • #5
          Dear Nick,

          Thank you again for the very patient and thorough answers. I have always found it challenging fully understanding and then properly using the double and single quotations. Your examples have helped a great deal!

          Thank you again,
          Kai-Yuan

          Comment

          Working...
          X