Announcement

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

  • Creating and printing a matrix for the results of a Wald test for all variables

    Hello,

    I have about 20 variables in a regression model and would like to test each among each other if they are equal, like that:
    test _b[Var1] ==_b[Var2]
    My idea was to create a nested for loop and save r(p) for each of the 20x20 tests in a matrix which I obtain as output eventually. At best the values that will be there twice will be deleted.
    I thought about creating to local variables which contain the names of all variables and then do something like that:
    Code:
    foreach i in varlist {
      foreach j in varlist {
        quietly test _[`i']==_[`j']
        matrix a = r(p) if row ==`i' & column == `t'
      }
    }
    display a
    However, I don't know how to do that in Stata.
    I would be happy if someone could help me with that.

    Best wishes,
    Peter
    Last edited by Peter Walser; 12 Sep 2017, 16:07.

  • #2
    Something like this:

    Code:
    local predictors list_all_the_predictor_variables_here
    local n_predictors: word count `predictors'
    matrix A = J(`n_predictors', `n_predictors', 0)
    forvalues i = 1/`n_predictors' {
        forvalues j = `=`i'+1'/`n_predicors' {
            local var1: word `i' of `predictors'
            local var2: word `j' of `predictors'
            quietly test _b[`var1'] = _b[`var2']
            matrix A[`i', `j'] = r(p)
            matrix A[`j', `i'] = r(p)
        }
    }
    matrix list A
    Note: not tested. Be ware of typos, unbalanced quotes, braces, etc.

    That said, Stata matrices are really most valuable when you are going to actually perform matrix calculations with them. Since I can't think of any kind of matrix operations that would be meaningfully applied to a matrix of p-values, I think you are just using the matrix as a "container" to hold these results, and I don't know what you plan to do with them. You may find that a matrix is not a convenient structure for whatever that is. On the other hand, if you just want to list out the matrix of probabilities as a table, then a matrix is probably as good as anything. If this is the plan, you probably will want to rename the rows and colulmns of the matrix with

    Code:
    matrix rownames A = `predictors'
    matrix colnames A = `predictors'
    Finally, if you really have 20 variables and you want to test equality of all possible pairs, there are 190 such distinct pairs. I don't think you can give any meaningful interpretation to 190 p-values. It's just mining noise in the data.

    Comment


    • #3
      Many thanks for your answer!
      The code is working.
      I would like to append such a matrix in my thesis I am currently working on.
      I am considering several lags of a time-series variable and got an uncommon pattern of significance. That's why I wanted to test whether the variables differ from each other.
      Of course I don't need all of the 190 values but I think its a nice overview and I do not want to attend fractions.

      Actually is there way to format the matrix in terms of the length of the row- and colnames? I would like to restrict its lengths to 10 characters for example.
      Is there a way to rename the variables just the matrix and keep the names in the regression?
      To format the values, I use
      Code:
      matrix list A, format(%9.3f)
      which works fine.

      Comment


      • #4
        Well, yes, you can change the names of the stubs and column headers. You just have to make a list of the names you want and then use the -matrix rownames- and -matrix colnames- commands. If what you need is to reduce them all to 10 characters:

        Code:
        local predictors10
        foreach p of local predictors {
            local predictors10 `predictors10' `=substr(`"`p'"', 1, 10)'
        }
        matrix rownames A = `predictors10'
        matrix colnames A = `predictors10'
        Caution: if there are two or more original predictors whose names agree on the first 10 characters, this will either cause Stata to break and complain when you get to -matrix rownames-, or, if Stata doesn't check for that, it will create an ill-formed matrix that may do bizarre things when you try to use it.

        Note: Code above not tested. Beware of typos, unbalanced quotes, etc.

        Comment


        • #5
          Thank you very much!
          Your advices were really helpful!

          Comment

          Working...
          X