Announcement

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

  • How to display/report results of a program

    Hi,

    I have a program that allows the user to obtain standardised differences (raw and weighted) after using propensity scores. I would like my program to report the standardised differences for each of the variables that I have entered into the program. I am looking for guides/articles online that show how to generically display results after writing a program but none that I can find are very helpful.


    My program is:

    Code:
    capture program drop myprogramme
        program define myprogramme
            syntax varlist(numeric fv)
            
            * Create macros from the varlist
                tokenize `varlist'
                local outcome `1'
                macro shift
                local exposure `1'
                macro shift
                local varlist `*'
            
            * Create the IPW weights
                capture drop _ipw
                qui gen _ipw = .
                qui replace _ipw = (`exposure'==1) / _ps if `exposure'==1
                qui replace _ipw = (`exposure'==0) / (1- _ps) if `exposure'==0
            
            * Calculate the covariate balance
                foreach var in `varlist' {
                        * Raw SMD
                        qui summarize `var' if `exposure'==1
                        local m1: di %9.7g r(mean)
                        local v1: di %9.7g r(Var)
                        qui summarize `var' if `exposure'==0
                        local m0: di %9.7g r(mean)
                        local v0: di %9.7g r(Var)        
                        * Calculate the Standardised "mean difference"
                        di as text "Raw SMD:      " as text "`var' " %9.7g (`m1' - `m0') / sqrt( (`v1' + `v0') /2 )
                        
                        * Weighted SMD
                        qui summarize `var' [iw=_ipw] if `exposure'==1
                        local m1: di %9.7g r(mean)
                        local v1: di %9.7g r(Var)
                        qui summarize `var' [iw=_ipw] if `exposure'==0
                        local m0: di %9.7g r(mean)
                        local v0: di %9.7g r(Var)        
                        * Calculate the Standardised "mean difference"
                        di as text "Weighted SMD: " as text "`var' " %9.7g (`m1' - `m0') / sqrt( (`v1' + `v0') /2 )
                        
                        * Raw VR
                        qui sum `var' if `exposure' ==1
                        local v1: di %9.7g r(Var)
                        qui sum `var' if `exposure' ==0
                        local v0: di %9.7g r(Var)        
                        * Calculate the variance ratio
                        di as text "Raw VR:       " as text "`var' " %9.7g `v1' / `v0'
                        
                        * Weighted VR
                        qui sum `var' [iw=_ipw] if `exposure' ==1
                        local v1: di %9.7g r(Var)
                        qui sum `var' [iw=_ipw] if `exposure' ==0
                        local v0: di %9.7g r(Var)        
                        * Calculate the variance ratio
                        di as text "Weighted VR:  " as text "`var' " %9.7g `v1' / `v0'
                }    
        end

    I would like to be able to have two main columns: one for the standardised differences and one for the variance ratios. Ideally, each of these main columns would have two subcolumns (i.e., one subcolumn for the "Raw", and one column for the "Weighted"). This is something similar to the way the command "tebalance summarize" reports results:


    Click image for larger version

Name:	Screenshot 2022-12-13 at 18.47.30.png
Views:	1
Size:	122.8 KB
ID:	1693349




    Any help is much appreciated!


  • #2
    I use the user written code covbal to do so (it permits weights), but you'd have to run it twice. It should be easy to rewrite to do both in the same table.

    Or you could use tabstat ... , save and make a table from the stored matrix.
    Last edited by George Ford; 13 Dec 2022, 13:26.

    Comment


    • #3
      Hi George,

      Thanks for the reply. Unfortunately, this program is part of a larger program and uses weights (from propensity scores) generated from the larger program. So I will need to create a table within this program. Is there a way to do it via storing results from each loop and appending it to a matrix?
      Last edited by Matthew Smith Stata; 13 Dec 2022, 15:11.

      Comment


      • #4
        As a side-issue that could bite, I note that you are typically rounding intermediate results before doing final calculations. That could lead to problems with reproducibility. Even keeping lots of decimal places may not be safeguard enough when downstream you are calculating differences and ratios. The best route is to store intermediate results in scalars and round only in the final presentation.

        Comment


        • #5
          Ah, excellent point! Many thanks, Nick! I will amend

          Comment


          • #6
            Here's a basic format. You can create tables from the matrix R.

            Code:
            use auto, clear
            g _ipw = runiform()
            
            matrix R = J(4,4,.)
            local c = 1
            foreach var in price mpg weight length {
                * Raw SMD
                qui summarize `var' if foreign==1
                local m1= r(mean)
                local v1= r(Var)
                qui summarize `var' if foreign==0
                local m0= r(mean)
                local v0= r(Var)
                * Calculate the Standardised "mean difference"
                matrix R[`c',1] = (`m1' - `m0') / sqrt( (`v1' + `v0') /2 )
            
                * Weighted SMD
                qui summarize `var' [iw=_ipw] if foreign==1
                local m1= r(mean)
                local v1= r(Var)
                qui summarize `var' [iw=_ipw] if foreign==0
                local m0= r(mean)
                local v0= r(Var)
                * Calculate the Standardised "mean difference"
                matrix R[`c',2] = (`m1' - `m0') / sqrt( (`v1' + `v0') /2 )
            
            * Raw VR
                qui sum `var' if foreign ==1
                local v1= r(Var)
                qui sum `var' if foreign ==0
                local v0= r(Var)
                * Calculate the variance ratio
                matrix R[`c',3] =  `v1' / `v0'
            
                * Weighted VR
                qui sum `var' [iw=_ipw] if foreign ==1
                local v1= r(Var)
                qui sum `var' [iw=_ipw] if foreign ==0
                local v0= r(Var)
                * Calculate the variance ratio
                matrix R[`c',4] = `v1' / `v0'
                
                local c = `c'+1
            }

            Comment


            • #7
              Many thanks, George! This is perfect

              Comment

              Working...
              X