Announcement

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

  • Store p-values of looped polychoric correlation matrix in table/matrix

    I'd like to verify that the matrix code below does what it's supposed to be doing.

    Its purpose is to store p values from the polychoric correlation command run for every combination of two vars in a varlist.

    I created it based on the code here:
    https://www.statalist.org/forums/for...n-table-matrix

    But it looks like some lines there use syntax that doesn't work for my dataset. For instance, this throws an error in my code:
    Code:
    matrix p = J(`l',1,.)
    Not sure why.

    My modified code below appears to work, judging from the final result. However, I thought that the matrix would be empty on one side of the diagonal but it isn't, which puzzles me. Also thank you to Maarten Buis for the earlier code!

    Chris



    Code:
    // store the unabbreviated varlist in the local macro y
    // you can use that to name the rows in your matrix
    unab y : ausa aeurop crisk cpossi dsever dmeasl eclass eairpl kvacget kvacpay 
    
    
    // prepare an empty matrix in which to store the
    // p-values
    matrix p = J(10, 10, .)
    matrix rownames p = `y'
    matrix colnames p = "p"
    
    // look at the empty matrix
    matlist p
    
    // loop over the variables and store the p-values
    local a = 1
    local b = 1
    foreach x1 of local y {
        foreach x2 of local y {
        polychoric `x1' `x2'
        matrix p[`a',`b'] = r(pLR0)
        local ++a
        }
        local a = 1
        local ++b
    }
    // admire the result
    matlist p

  • #2
    You left this out of your code, which is why the command you cite does not work.
    Code:
    // count the number of variables in `y'
    local l : word count `y'
    The matrix is not lower triangular because
    Code:
    foreach x1 of local y {
        foreach x2 of local y {
        polychoric `x1' `x2'
    ...
    computes both of the following
    Code:
    polychoric ausa aeurope
    polychoric aeurope ausa
    and stores the results in p[2,1] and p[1,2]

    Comment


    • #3
      I'm sorry for my oversight. I should have mentioned that when I tried the original code, I did include that line (local l : word count `y') but I still got an error.

      Chris

      Comment


      • #4
        In that case, I believe your problem was that you wrote your code in the do-file editor window, and then rather than running everything at once, you ran it it by selecting a few lines and running them, then selecting the next few lines and running them, and so on.

        Consider the following example. In the do-file editor window, I have a two-line program that I run in its entirety.
        Code:
        . do "/Users/lisowskiw/Downloads/example.do"
        
        . local message Hello, world.
        
        . display "The message is `message'"
        The message is Hello, world.
        
        . 
        end of do-file
        Now I run the same two lines by selecting the first line and running it, then selecting the second line and running it.
        Code:
        . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD17616.000000"
        
        . local message Hello, world.
        
        . 
        end of do-file
        
        . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD17616.000000"
        
        . display "The message is `message'"
        The message is 
        
        . 
        end of do-file
        The important thing to keep in mind is that local macros vanish when the do-file within which they were created ends. If you look carefully at the results above, you'll see that when I selected a single line to run, it was copied into a temporary do-file and run, so even though both lines are in the same window in the do-file editor, they are run as separate do-files, and local macro defined in the first line vanishes at the end of that do-file, and is undefined when the second line is run.

        So I hypothesize that you ran the line that defined the local macro l, which then vanished immediately, and you then ran the rest of your code, which gave you the syntax error when it interpreted
        Code:
        matrix p = J(`l',1,.)
        as
        Code:
        matrix p = J(,1,.)

        Comment


        • #5
          That was indeed the problem. This code works now:
          Code:
          // store the unabbreviated varlist in the local macro y
          // you can use that to name the rows in your matrix
          unab y : ausa aeurop crisk cpossi dsever dmeasl eclass eairpl kvacget kvacpay 
          // count the number of variables in `y'
          local l : word count `y'
          // prepare an empty matrix in which to store the
          // p-values
          matrix p = J(`l',`l',.)
          matrix rownames p = `y'
          matrix colnames p = `y'
          // look at the empty matrix
          matlist p
          // loop over the variables and store the p-values
          local a = 1
          local b = 1
          foreach x1 of local y {
              foreach x2 of local y {
              polychoric `x1' `x2'
              matrix p[`a',`b'] = r(pLR0)
              local ++a
              }
              local a = 1
              local ++b
          }
          // admire the result
          matlist p

          Comment


          • #6
            In case anyone wants the code for one set of variable in the rows and a different set of variables in the columns here it is

            Code:
            // store the unabbreviated varlists in the local macros y and x
            // you can use that to name the rows in your matrix
            unab y : rwv1 rwv2 rwv4
            unab x : clv1 clv2 clv3 clv4 clv5
            // count the number of variables in `each 
            local l : word count `y'
            local m : word count `x'
            // prepare an empty matrix in which to store the
            // p-values
            matrix p = J(`m',`l',.)
            matrix rownames p = `x'
            matrix colnames p = `y'
            // look at the empty matrix
            matlist p
            // loop over the variables and store the p-values
            local a = 1
            local b = 1
            foreach x1 of local y {
                foreach x2 of local x {
                polychoric `x1' `x2'
                matrix p[`a',`b'] = r(pLR0)
                local ++a
                }
                local a = 1
                local ++b
            }
            // admire the result
            matlist p
            And these are the two lines you should focus on changing if you want something other than polychoric p values. You can change the first to regress, corr, whatever... and the second can pull whatever returned value you want. Just change the part after the equals sign

            Code:
                polychoric `x1' `x2'
                matrix p[`a',`b'] = r(pLR0)

            Comment

            Working...
            X