Announcement

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

  • Adding Tags to data collected from Collect's Command option

    Hello!

    I am creating a table from my data, and I am running into 2 problems. Below are my code and the results:

    Code:
    collect clear
        collect layout, clear
        
                    
    collect: table ///
        (var) ///
        (tscore), ///
            statistic(mean age) ///
                statistic(sd age) ///
                command(manual_pvalue = e(p): ologit tscore c.age) ///
            statistic(fvfrequency race) ///
                statistic(fvpercent race) ///
                command(manual_pvalue = r(p): tab race tscore, chi2) ///
            stars(_r_p 0.01 "***" 0.05 "**" 0.1 "*", attach(_r_b) shownote) ///
            noisily
    
    collect dims
    collect label list command, all        
            
    collect addtags var[age], fortags(result[manual_pvalue]#command[1]) replace    
    collect addtags var[race], fortags(result[manual_pvalue]#command[2]) replace
    
    collect dims
    
    collect recode result ///
        fvfrequency = variable_measure ///
            fvpercent = variable_spread ///
        mean = variable_measure ///
            sd = variable_spread //
    
    collect label levels result variable_measure "Count / Mean" variable_spread "% / (SD)"  
    collect label levels result manual_pvalue "P-Value"  
        
    collect layout ///
        (var) ///
        (tscore#result[variable_measure variable_spread] result[manual_pvalue])

    This code gives me the following table:

    Code:
    Collection: Table
          Rows: var
       Columns: tscore#result[variable_measure variable_spread] result[manual_pvalue]
       Table 1: 6 x 9
    
    --------------------------------------------------------------------------------------------------------------------------------------------------------------
                                          |                                        Osteoporosis (by T-Score)                                         P-Value
                                          |         Normal BMD                Osteopenia               Osteoporosis                 Total                        
                                          |  Count / Mean   % / (SD)   Count / Mean   % / (SD)   Count / Mean   % / (SD)   Count / Mean   % / (SD)                
    --------------------------------------+-----------------------------------------------------------------------------------------------------------------------
    age                                   |      73.67566   5.216106        75.2123    5.41293       77.31614   6.056276       74.61615   5.462809        3.22e-11
    RACE/ETHNIC CATEGORY=WHITE            |           532    86.9281            513   91.28114             81   93.10345           1126   89.29421                
    RACE/ETHNIC CATEGORY=AFRICAN AMERICAN |            61    9.96732             15   2.669039              0          0             76   6.026963                
    RACE/ETHNIC CATEGORY=ASIAN            |            12   1.960784             20   3.558719              4   4.597701             36   2.854877                
    RACE/ETHNIC CATEGORY=HISPANIC         |             2   .3267974              5   .8896797              1   1.149425              8   .6344171                
    RACE/ETHNIC CATEGORY=OTHER            |             5   .8169935              9   1.601423              1   1.149425             15   1.189532                
    --------------------------------------------------------------------------------------------------------------------------------------------------------------
    *** p<.01, ** p<.05, * p<.1
    This is excellent, however there are 2 problems:
    1. The P-Value for race variable does not appear, eventhough the P-Value for the age variable appears. I suspect this is because collected value from the <Tab> command (via r(p) ) is automatically tagged differently than the collected value from the <ologit> command (via e(p) ). So when I attempt to add Tags, I am not adding Tags to the values Collected via <Tab> command. However I am not sure what the already assigned tags are. Regardless, can you advise how to get both P-Values to be present in the right-most column in the table?
    2. Also, although I have the footnote for the significance of P-Value (i.e. *** p<.01, ** p<.05, * p<.1), how can I also make sure the associated stars are put next to the appropriate P-Values under the right-most P-Value column in the table for the P-Values?

    Thank you very much, and apologies for the multiple questions in one post!
    Last edited by Nathan Yu; 28 Mar 2024, 21:38.

  • #2
    Hello, just wanted to follow up on this. Thank you in advance!

    Comment


    • #3
      You did not provide us with a dataset to go with your code, so I'll use a publicly available one with similar variables.

      I replaced the command() options with separate calls to the commands since they contain the tscore variable in them and you only want the results from the overall sample, not for each level of tscore. This also prevents table from collecting redundant/unused results that could complicate your layout specification later.

      Note that in the call to collect get that consumes the p-value from tabulate I tag the result with the first level of race, e.g. var[1.race].

      To get the stars to show up with your custom p-value results, you need to use manual_pvalue instead of _r_p and _r_b.

      Code:
      webuse nhanes2l
      gen tscore = highbp
      
      collect clear
      collect layout, clear
          
                      
      collect: table ///
          (var) ///
          (tscore), ///
              statistic(mean age) ///
                  statistic(sd age) ///
                  statistic(fvfrequency race) ///
                  statistic(fvpercent race)
      
      * note the level value labeled "Total"
      collect label list tscore, all
      
      * get p-value for association between tscore and age
      ologit tscore c.age
      * consume p-value and attach tags to help with layout
      collect get manual_pvalue = e(p), tags(var[age] tscore[.m])
      
      * get p-value for association between tscore and race
      * note the value of the first level
      tab race tscore, chi2 nolabel
      * consume p-value and attach tags to help with layout
      collect get manual_pvalue = r(p), tags(var[1.race] tscore[.m])
      
      collect stars manual_pvalue 0.01 "***" 0.05 "**" 0.1 "*", ///
          attach(manual_pvalue) shownote
      collect style cell result[manual_pvalue], nformat(%5.2f) minimum(.01)
      
      collect recode result ///
          fvfrequency = variable_measure ///
          fvpercent = variable_spread ///
          mean = variable_measure ///
          sd = variable_spread //
      
      collect label levels result variable_measure "Count / Mean" variable_spread "% / (SD)"  
      collect label levels result manual_pvalue "P-Value"  
          
      collect layout ///
          (var) ///
          (tscore#result[variable_measure variable_spread] result[manual_pvalue])
      Here is the final table.
      Code:
      -----------------------------------------------------------------------------------------------------
                  |                                     tscore                                      P-Value
                  |             0                         1                       Total                    
                  |  Count / Mean   % / (SD)   Count / Mean   % / (SD)   Count / Mean   % / (SD)           
      ------------+----------------------------------------------------------------------------------------
      Age (years) |      42.16502   16.77157       54.97281   14.90897       47.57965   17.21483   <0.01***
      Race=White  |          5317   88.98745           3748   85.64899           9065   87.57608   <0.01***
      Race=Black  |           545   9.121339            541   12.36289           1086   10.49174           
      Race=Other  |           113   1.891213             87   1.988117            200    1.93218           
      -----------------------------------------------------------------------------------------------------

      Comment


      • #4
        Dear Mr. Pitblado,

        Firstly, my apologies for not including data. In the future I will be sure to include a example data set as an attachment.

        Also --- thank you so much!! This is excellent, and works exactly as I need it to but could not figure it out after spending so much time. Thank you again!

        A last question, what is the purpose of adding the " tscore[.m] " tag when collecting r(p) and e(p) from the commands? After tests, I noticed both of below result in the same final table I am looking for:

        Code:
            ologit tscore c.age
                collect get manual_pvalue = e(p), tags(var[age] tscore[.m])
        
            tab race tscore, chi2
                collect get manual_pvalue = r(p), tags(var[1.race] tscore[.m])
        Above code results in same table as below code:

        Code:
            ologit tscore c.age
                collect get manual_pvalue = e(p), tags(var[age] tscore[.m])
        
            tab race tscore, chi2
                collect get manual_pvalue = r(p), tags(var[1.race] tscore[.m])
        Is there a particular best practices reasons to add the tscore[.m] tag to the collected P-Values?

        Best regards.

        Comment


        • #5
          My apologies, I had a typo in the code blocks in the previous comments --- the second code block should NOT contain the tscore[.m] in the tags() option, where as the first one should contain it! I do not see an option to edit the comment at this time unfortunately.

          Comment


          • #6
            I tagged the p-values with tscore[.m] before looking closely at your layout. You are correct they are unnecessary in this example.

            In my mind (before looking more closely) your layout was
            Code:
            collect layout (var) (tscore#result[variable_measure variable_spread manual_pvalue])
            so I thought I needed tscore[.m] in the p-value tags.

            Comment


            • #7
              I see, wonderful. Thank very much Mr. Pitblado, your code was extremely helpful and gives much more control than the command option!!

              Comment

              Working...
              X