Announcement

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

  • r(198) invalid name error message

    I'm getting an r(198) error when trying to run the code below I'm new to STATA so I'm not exactly sure if I'm missing something that might be causing it. Thanks in advance for your help

    Code:
    // initializing the regression model and variables to run the model on
    local model "logistic"
    local variables placeholdera placeholderb placeholderc placeholderd placeholdere
    local num_variables: word count `variables'
    local subcategories i.placeholder1 i.placeholder2 i.placeholder3 i.placeholder4 i.placeholder5 i.placeholder6 i.placeholder7 i.placeholder8
    local num_subcategories: word count `subcategories'
    
    // initializing matrix to store regression results in
    matrix var_Tracker = J(`num_variables',`num_subcategories', 0)
    
    // for loop to run regression models on the variables and subcategories
    forval i=1/`num_variables'{
        local this_Variable' `: word `i' of `variables''
        global cumulative_Subcategories = ""
        di "`this_Variable'"
        forval j=1/`num_subcategories'{
            local item : word `j' of `subcategories'
            di "`item'"
            global cumulative_Subcategories "`item'"
            di "`cumulative_Subcategories'"
            logit `this_Variable' `cumulative_Subcategories', or    
        }
    }

  • #2
    There are a couple of problems in your code.

    The command
    Code:
        local this_Variable' `: word `i' of `variables''
    contains a stray ' character that I have highlighted by enlarging it and displaying it in red. That quote mark has to go.

    Then, your code does not properly manage cumulative_subcategories. On the one hand, you initially define it as a global macro, but then when you try to refer to it, you do so as if it were a local macro--with the result that it is interpreted as an empty string when you get to
    Code:
    di "`cumulative_Subcategories'"
    logit `this_Variable' `cumulative_Subcategories', or
    Now, from the perspective of legal syntax, you could make it either a global or a local, so long as you do so consistently throughout the code. But since global macros are a dangerous programming practice that should be avoided except when there is no alternative, here you should do it as a local macro. So fixing all of these things, we get:

    Code:
    // initializing the regression model and variables to run the model on
    local model "logistic"
    local variables placeholdera placeholderb placeholderc placeholderd placeholdere
    local num_variables: word count `variables'
    local subcategories i.placeholder1 i.placeholder2 i.placeholder3 i.placeholder4 i.placeholder5 i.placeholder6 i.placeholder7 i.placeholder8
    local num_subcategories: word count `subcategories'
    
    // initializing matrix to store regression results in
    matrix var_Tracker = J(`num_variables',`num_subcategories', 0)
    
    // for loop to run regression models on the variables and subcategories
    forval i=1/`num_variables'{
        local this_Variable `: word `i' of `variables'' // STRAY ' REMOVED
        local cumulative_Subcategories = ""
        di "`this_Variable'"
        forval j=1/`num_subcategories' {
            local item : word `j' of `subcategories'
            di "`item'"
            local cumulative_Subcategories "`item'"
            di "`cumulative_Subcategories'"
            logit `this_Variable' `cumulative_Subcategories', or
        }
    }
    That, I believe, removes all the syntax errors Now, there is the additional question of whether there is also a logic error. From your choosing the name cumulative_Subcategories, I anticipate that you want this local macro to start out empty, and then add one more subcategory on each iteration of the -forval j- loop. The code as it stands does not do that: it simply overwrites cumulative_Subcategories with the current value of `item' at each iteration, so that it only contains one subcategory at any time. If I have correctly guessed that you want it to add on an additional subcategory each time, then
    Code:
            local cumulative_Subcategories "`item'"
    should be replaced with
    Code:
            local cumulative_Subcategories `cumulative_Subcategories' `item'
    Note that in addition to the bold-faced change, that the double-quotes have been removed from around `item'.

    Comment


    • #3
      Thanks that helped a lot!

      In addition to this, I was planning on storing the regression model outputs in the matrix var_Tracker to then graph into a forest plot. But, I noticed in another thread you mentioned that using a matrix as a data structure is not ideal if I'm not planning on performing any linear algebra operations on it so I was curious if you could recommend a better approach to this.

      Comment

      Working...
      X