Announcement

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

  • Fixing the logic of a (slightly) complex if/then statement

    Hi All,

    I am writing an ado. file that performs a task following a linear multilevel model (either mixed or meglm with family(gaussian)).

    As such, I have an error message that evaluates whether the prior estimation model was either of these cases:

    Code:
    webuse productivity
    
    // using mixed
    mixed gsp private emp hwy water other unemp || region: || state:
    
    // using meglm
    meglm gsp private emp hwy water other unemp || region: || state:
    Code:
            if ("`e(cmd)'" != "mixed") | ("`e(cmd2)'" != "meglm" & "`e(model)'" != "linear") {
                    di as err "the prior estimation model was not {bf:mixed} or {bf:meglm} with a linear model"
                    exit 301
             }
    Well, this error message doesn't work because it will get tripped up on either the first part ("`e(cmd)'" != "mixed") when using meglm or the second part, when using mixed

    Code:
    when using mixed, the second part produces the error because there is no "`e(cmd2)'":
    
    if ("mixed" != "mixed") | ("" != "meglm" & "" != "linear")
    Code:
    when using meglm, the first part produces the error because e(cmd) == gsem (and I purposely am not allowing gsem at this stage of development)
     
    if ("gsem" != "mixed") | ("meglm" != "meglm" & "linear" != "linear")
    I also tried nesting the "if" statement using the two parts, but that didn't work either...

    Any help would be appreciated!

    Thanks!

    Ariel

  • #2
    Well, you want a negation. It's the literal translation of when you want the error to message: "If the model is not mixed or meglm with family(gaussian), then issue an error."

    Code:
    if ( ! (("`e(cmd)'"=="mixed") | (`"e(cmd2)"'=="meglm" & "`e(model)'"=="linear")) ) {
        di as err "the prior estimation model was not {bf:mixed} or {bf:meglm} with a linear model"
        exit 301
    }
    I did not check whether e(model) is the best option here. Perhaps you want e(family), which seems more closely to your verbal description. Also, return code 301 is okay but 321 might fit the situation even better.


    Edit / Added

    By the way, here is a nested version that might be easier to read (and therefore to debug and maintain):

    Code:
    if ("`e(cmd)'" != "mixed") {
        
        if ("`e(cmd2)'"!="meglm") | ("`e(model)'"!="linear") {
            
            di as err "the prior estimation model was not {bf:mixed} or {bf:meglm} with a linear model"
            exit 301
            
        }
        
    }
    Last edited by daniel klein; 27 May 2024, 02:20.

    Comment


    • #3
      Beautiful! Thank you, Daniel. I was definitely missing the the negation!

      Comment

      Working...
      X