Announcement

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

  • Multiple median IQR for putdocx statements

    Hello all:

    I was trying to get median and IQR of multiple continuous variable by groups of a binary variable into a wrapper I want to create for calling in a putdocx statements. I saw Andrew Musau's post using frames earlier for collecting Chi-square results which seems neat but I could not quite configure it for my needs to call the values and levels as needed. So, I tried my version with local macros but I am not getting the loop right since the values are not showing up.

    Have attached a sample with the auto dataset with mpg and length by foreign as the dichotomous variable.

    Code:
    sysuse auto, clear
    local v mpg length
    local f foreign
    levelsof `f', local(mylev)
    tabstat `v', by(`f') statistic(p25 p50 p75) save
    return list
    foreach v of local v{
        forvalue j = 1/2 {
            mat list r(Stat`j')
        local    `v'lqr`mylev'     =  r(Stat`j')[1,1]
        local    `v'med`mylev'  =  r(Stat`j')[2,1]
        local    `v'uqr`mylev'    =  r(Stat`j')[3,1]
        }
    }
    
    
    di "The median length in the domestic car group was `lengthmed0' (IQR: `lengthlqr0', `lengthuqr0')"

  • #2
    Your issue seems to arise from

    foreach v of local v{
    Here, an element of the local referenced `v' is the same as the local itself `v'. Refer to the elements of local v using a different name.


    Code:
    sysuse auto, clear
    local v mpg length
    local f foreign
    levelsof `f', local(mylev)
    tabstat `v', by(`f') statistic(p25 p50 p75) save
    return list
    foreach e of local v{
        forvalue j = 1/2 {
            mat list r(Stat`j')
        local    `e'lqr`mylev'     `=r(Stat`j')[1,1]'
        local    `e'med`mylev'   `=r(Stat`j')[2,1]'
        local    `e'uqr`mylev'    `=r(Stat`j')[3,1]'
        }
    }
    
    
    di "The median length in the domestic car group was " `lengthmed0' " (IQR: " `lengthlqr0' " -",`lengthuqr0' ")"
    Res.:

    Code:
    . 
    . di "The median length in the domestic car group was " `lengthmed0' " (IQR: " `lengthlqr0' " -",`lengthuqr0' ")"
    The median length in the domestic car group was 124.5 (IQR: 121 - 128)
    Last edited by Andrew Musau; 28 Jun 2023, 13:18.

    Comment


    • #3
      Thanks much for pointing out, Andrew Musau Sounds so logical once you explained it.

      Comment


      • #4
        Originally posted by Andrew Musau View Post
        Your issue seems to arise from



        Here, an element of the local referenced `v' is the same as the local itself `v'. Refer to the elements of local v using a different name.


        Code:
        sysuse auto, clear
        local v mpg length
        local f foreign
        levelsof `f', local(mylev)
        tabstat `v', by(`f') statistic(p25 p50 p75) save
        return list
        foreach e of local v{
        forvalue j = 1/2 {
        mat list r(Stat`j')
        local `e'lqr`mylev' `=r(Stat`j')[1,1]'
        local `e'med`mylev' `=r(Stat`j')[2,1]'
        local `e'uqr`mylev' `=r(Stat`j')[3,1]'
        }
        }
        
        
        di "The median length in the domestic car group was " `lengthmed0' " (IQR: " `lengthlqr0' " -",`lengthuqr0' ")"
        Res.:

        Code:
        .
        . di "The median length in the domestic car group was " `lengthmed0' " (IQR: " `lengthlqr0' " -",`lengthuqr0' ")"
        The median length in the domestic car group was 124.5 (IQR: 121 - 128)
        Sorry, the code seems to be picking mpg and not length when I call lengthmed0 (which is length median in foreign 0 group). Furthermore, it is prefixing everything with a 1 in median IQR values. I wanted to be able to call each macros with the varname as listed with the level of the stratification variable so it is logical. I noticed your also referenced everything to the right of the local inside the loop in another set of ` ' quotes. Why was that needed here?
        Last edited by Girish Venkataraman; 29 Jun 2023, 05:30. Reason: Had to clarify since the code was not working as expected.

        Comment


        • #5
          I did not care to examine the particulars of the code beyond it producing results. You could simplify much by referring to the elements of the locals directly. A local is not convenient if it is just holding one or two elements. Also, "mylev" is a local containing levels of the variables. You want to refer to the levels individually and to match length to the second column of the matrices and mpg to the first. Here is a way keeping your local definitions.

          Code:
          sysuse auto, clear
          local v mpg length
          local f foreign
          levelsof `f', local(mylev)
          tabstat `v', by(`f') statistic(p25 p50 p75) save
          return list
          
          local col 0
          foreach e of local v{
              local ++col
              foreach lev of local mylev{
                  local j= `lev'+1
                  mat list r(Stat`j')
                  
                  local `e'lqr`lev'   `=r(Stat`j')[1,`col']'
                  local  `e'med`lev'  `=r(Stat`j')[2,`col']'
                  local  `e'uqr`lev'  `=r(Stat`j')[3,`col']'
              }
          }
          
          di `lengthmed0'
          di `mpgmed0'
          di `lengthmed1'
          di `mpgmed1'
          Res.:

          Code:
          r(Stat1)[3,2]
                  mpg  length
          p25    16.5   179.5
          p50      19     200
          p75      22   209.5
          
          
          r(Stat2)[3,2]
                  mpg  length
          p25      21     156
          p50    24.5     170
          p75      28     175
           
          .
          . di `lengthmed0'
          200
          
          .
          . di `mpgmed0'
          19
          
          .
          . di `lengthmed1'
          170
          
          .
          . di `mpgmed1'
          24.5
          
          .
          Last edited by Andrew Musau; 29 Jun 2023, 11:24.

          Comment


          • #6
            You are right, Andrew Musau. I probably could just refer to the elements directly. I was just hoping to put in an -include- wrapper to call medians by group with a Mann-Whitney p to use in the putdocx statements. I realized I did not have a systematic way to name the locals when I would run the same test on several variables in a chunk preceding the putdocx statement. Using variable names (despite them being clunky and long) will keep things straight without the issue of misnaming when calling them. Or even worse call a p-value that was defined for an unintended test preceding the one of interest.

            Comment

            Working...
            X