Announcement

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

  • Handling Omitted Variables in Mata

    Hi all,

    I am writing a Mata function that calculates a VCE for my Stata command, and I want it to be able to handle omitted variables (i.e., those flagged with the prefix "b" or "o"). How do I go about doing this? Is there a canonical method?

    Currently, I am passing the matrix r(omit) from -_ms_omit_info- (after swapping the 0s for 1s and vice versa) to Mata and using it with -st_select()- to get the data for the non-omitted variables. I can calculate the VCE for the non-omitted variables, but I want a VCE for all variables (with zeros in the rows and columns for omitted variables, which is the standard in Stata). Do I just have to insert rows and columns of zeros where the omitted variables used to be? If so, is there an efficient way to do so?

  • #2
    Since no one has responded, I've written up a subroutine that will insert columns of zeros corresponding to the zeros in the supplied vector. (To use it to insert rows of zeros, simply use the function on the transpose of the original matrix.) In case anyone in the future also has this question, here's the code for it:
    Code:
    numeric matrix insert_colzeros(real matrix M, real matrix v)
    {
        end_num_cols = cols(v)
        start_num_cols = cols(M)
        num_rows = rows(M)
        
        R = J(num_rows, end_num_cols, 0)
        zero_col = J(num_rows, 1, 0)
        M = (M, zero_col)                    // Hacky buffer to prevent the loop from breaking at the last column of M
        
        for(i=1; i<=end_num_cols; i++) {
            if (v[i] == 1) {
                R[., i] = M[., 1]
                M = M[|1, 2 \ ., .|]
            }
        }
        
        return(R)
    }

    Comment

    Working...
    X