Announcement

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

  • How to Getting Rid of The Factorial Variables after Deleting Multicolinear Variables Using "_rmcoll"

    I have a set of variables X1, X2, X3, X4, X5. Among them, X5 is colinear with X3. So I use the command "_rmcoll" to delete X5 from my variable list. And the following is the code and the result.


    Code:
    _rmcoll x1 x2 x3 x4 x5
    note: x5 omitted because of collinearity
    
    local variables `r(varlist)'
    
    display "`variables'"
    x1 x2 x3 x4 o.x5
    The problem is, the variable x5 is not really deleted. It is still there marked as a factorial variable "o.x5".

    Since I need to create a matrix for the remaining variable list using the command "mkmat", which does not allow factorial variables, I need to get rid of the "o.x5" in the variable list.


    Code:
    _rmcoll x1 x2 x3 x4 x5
    
    note: x5 omitted because of collinearity
    
    local variables `r(varlist)'
    
    display "`variables'"
    x1 x2 x3 x4 o.x5
    
    mkmat`variables', matrix(variables)
    factor-variable and time-series operators not allowed
    r(101);

    Is there any way I can exclude the annoying factorial "o.x5" from my macro "`variables'"? I cannot do it manually since part of a Stata package.

    Last edited by Yutian Yang; 12 Jul 2021, 17:08.

  • #2
    How about this:
    Code:
    _rmcoll x1 x2 x3 x4 x5
    local variables_o `r(varlist)'
    
    local variables
    foreach v of local variables_o {
        if substr(`"`v'"', 1, 2) != "o." {
            local variables `variables' `v'
        }
    }
    
    display `"`variables'"'
    There may be a simpler way to do this using regular expressions, but I'm not very adept at using those.

    Comment


    • #3
      Thank you so much, Clyde! It works very well.

      Originally posted by Clyde Schechter View Post
      How about this:
      Code:
      _rmcoll x1 x2 x3 x4 x5
      local variables_o `r(varlist)'
      
      local variables
      foreach v of local variables_o {
      if substr(`"`v'"', 1, 2) != "o." {
      local variables `variables' `v'
      }
      }
      
      display `"`variables'"'
      There may be a simpler way to do this using regular expressions, but I'm not very adept at using those.

      Comment

      Working...
      X