Announcement

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

  • Adding options to a package (.ado)

    Hi everyone,

    I have written a code to run the LFK index (Int J Evid Based Healthc. 2018 Dec;16(4):195-203). At the moment it only runs using OR as the effect size but I want to add the options to run it using OR, RR, and RD.

    Could someone please point me how to enter the options in the syntax, so I can create different loops to run
    admetan `1' `2' `3' `4' , nograph or
    admetan `1' `2' `3' `4' , nograph rr
    admetan `1' `2' `3' `4' , nograph rd


    Many thanks
    Luis


    Code:
    program define lfk, rclass
    syntax varlist(min=1 max=4 numeric) [if] [in]
     
    tempvar a
    marksample touse, novarlist
    if "`a'" == "" markout `touse'
    preserve
    quietly keep if `touse'
     
     
    ***Data case no_cases
    if "`3'" != "" & "`4'" != ""{
                            quietly {
                            admetan `1' `2' `3' `4' , nograph or
                            rename _ES ln_es
                            rename _seES se_es
                            bysort ln_es se_es: gen dup = cond(_N==1,1,_n)
                            gsort -dup ln_es
                            gen dup_max = dup[1]
                            sort ln_es dup
                            gen se_es2 = se_es^2
                            egen se_es2_max = max(se_es2)
                            gen n = round(((se_es2_max/se_es2)*100),1)
                            gen rank1 = sum(n) if dup ==1
                            replace rank1 = rank1[_n-1] if rank1==. 
                            gen pct1 = rank1[_n-dup_max]
                            replace pct1 = 0 if pct1 ==.
                            gen rank2 = (rank1 + pct1)/2
                            egen n_sum = sum(n) if dup==1                                            
                            replace n_sum = n_sum[_n-1] if n_sum==.               
                            gen i = (rank2-0.5)/n_sum
                            gen z = invnorm(i)      
                            gen z_abs = abs(invnorm(i))
                            sort z_abs
                            gen es_z_min = ln_es[1]
                            gen es_dif = ln_es - es_z_min
                            sort z
                            gen z_min = z[1]
                            gen z_max = z[_N]
                            sort es_dif
                            gen es_dif_min = es_dif[1]
                            gen es_dif_max = es_dif[_N]
                            gen r = (z_max-z_min) / (es_dif_max-es_dif_min)
                            gen r2 = z + (r*(es_dif))
                            egen r2_sum = sum(r2)
                            gen _lfk = (5/(2*_N)) * (r2_sum)
                            scalar define lfk = _lfk[1]
    }
               
                ***Report LFK
                                        di ""
                                        di ""
                                        di "LFK index"
                                        di ""
                                        di _lfk
     
                ***Create Doi plot
                                        quietly levelsof es_z_min in 1
                                        twoway (connected z_abs ln_es, xline(`r(levels)', lcolor(black) noextend) mcolor(black) msize(vlarge) msymbol(circle) mfcolor(white) lcolor(black) lpattern(shortdash)), ytitle(| Z-score |) ytitle(, size(large)) yscale(reverse) ylabel(, labsize(large) angle(horizontal) labgap(small) nogrid) xtitle(, size(large)) xlabel(, labsize(large) labgap(small)) aspectratio(1.3) graphregion(fcolor(white))
    }
     
    ***Data theta se_theta
    if "`3'" == "" & "`4'" == ""{
                            quietly{
                            bysort `1' `2': gen dup = cond(_N==1,1,_n)
                            gsort -dup `1'
                            gen dup_max= dup[1]
                            sort `1' dup
                            gen se_es2 = `2'^2
                            egen se_es2_max = max(se_es2)
                            gen n = round(((se_es2_max/se_es2)*100),1)
                            gen rank1 = sum(n) if dup ==1
                            replace rank1 = rank1[_n-1] if rank1==.
                            gen pct1 = rank1[_n-dup_max]
                            replace pct1 = 0 if pct1 ==.
                            gen rank2 = (rank1 + pct1)/2
                            egen n_sum = sum(n) if dup==1                                            
                            replace n_sum = n_sum[_n-1] if n_sum==.               
                            gen i = (rank2-0.5)/n_sum
                            gen z = invnorm(i)      
                            gen z_abs = abs(invnorm(i))
                            sort z_abs
                            gen es_z_min = `1'[1]
                            gen es_dif = `1' - es_z_min
                            sort z
                            gen z_min = z[1]
                            gen z_max = z[_N]
                            sort es_dif
                            gen es_dif_min = es_dif[1]
                            gen es_dif_max = es_dif[_N]
                            gen r = (z_max-z_min) / (es_dif_max-es_dif_min)
                            gen r2 = z + (r*(es_dif))
                            egen r2_sum = sum(r2)
                            gen _lfk = (5/(2*_N)) * (r2_sum)
                            scalar define lfk = _lfk[1]
    }
     
                ***Report LFK         
                                        di ""
                                        di ""    
                                        di "LFK index"
                                        di ""    
                                        di _lfk
                           
                ***Create Doi plot
                                        quietly levelsof es_z_min in 1
                                        twoway (connected z_abs `1', xline(`r(levels)', lcolor(black) noextend) mcolor(black) msize(vlarge) msymbol(circle) mfcolor(white) lcolor(black) lpattern(shortdash)), ytitle(| Z-score |) ytitle(, size(large)) yscale(reverse) ylabel(, labsize(large) angle(horizontal) labgap(small) nogrid) xtitle(, size(large)) xlabel(, labsize(large) labgap(small)) aspectratio(1.3) graphregion(fcolor(white))
    }
    *
    restore
    quietly gen _lfk = lfk if `touse' ==1
     
    end

  • #2
    Hi Luis,

    General information on Stata programs, and handling of options, can be found under help syntax, or [P] syntax in the Stata manuals.


    For your specific situation, the simplest approach would be simply to accept any options to lfk, and pass them wholesale onto admetan, as follows:

    Code:
    program define lfk, rclass
    syntax varlist(min=1 max=4 numeric) [if] [in] [, *]
    local lfk_options : copy local options
     
    tempvar a
    marksample touse, novarlist
    if "`a'" == "" markout `touse'
    preserve
    quietly keep if `touse'
     
     
    ***Data case no_cases
    if "`3'" != "" & "`4'" != ""{
                            quietly {
                            admetan `1' `2' `3' `4', `lfk_options'
    
                           // code continues as before
    This would allow you to pass other options to admetan if required... or, similarly, to be used in any other way within your program.


    However, if you specifically wanted to parse options or, rr or rd but no others, you could code:

    Code:
    program define lfk, rclass
    syntax varlist(min=1 max=4 numeric) [if] [in] [, OR RR RD ]
     
    tempvar a
    marksample touse, novarlist
    if "`a'" == "" markout `touse'
    preserve
    quietly keep if `touse'
     
     
    ***Data case no_cases
    if "`3'" != "" & "`4'" != ""{
                            quietly {
                            admetan `1' `2' `3' `4', `or' `rr' `rd'
    
                           // code continues as before
    Note that admetan will itself handle any errors arising from e.g. specifying both or and rr at the same time.


    Hope that helps,

    David.

    Comment


    • #3
      Thanks David for your quick response.

      I tried the second alternative as I only want the OR, RR, RD options.
      But when I try to run

      Code:
      lfk var1 var2 var3 var4, or
      I get the error message

      Code:
      invalid 'or' 
      r(198);
      Any clues why is that happening?

      Thanks,
      Luis


      Comment

      Working...
      X