Announcement

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

  • Calculating 95%CI from svy: tab with loop

    Dear All,
    I'm trying to calculate 95%CI for svy: tabulate, storing the results in a local macro and passing them into a putexcel command. I have achieved this by writing out the command line by line for each proportional estimate, however the code is very long (see below) is there away to loop this code so it can be repeated for each proportion estimate, my current attempts have failed, kind regards and thank you for your help in advance.

    Charlie

    Code:
    *var1 is a categorical variable with 4 levels.
    
    svy, subpop(if e4==1): tab var1, ci
            matrix p = e(b)
            matrix list p    
            matrix b1= p[1,1]
            matrix list b1
    
            matrix b2= p[1,2]
            matrix list b2
    
            matrix b3= p[1,3]
            matrix list b3
    
            matrix b4= p[1,4]
            matrix list b4
    
            local b1  : display b1[1,1]
            local b2  : display b2[1,1]
            local b3  : display b3[1,1]
            local b4  : display b4[1,1]
            
            display `b1'
            display `b2'
            display `b3'
            display `b4'
    
            matrix df = e(df_r)
            matrix df1 = df[1,1]
            
            matrix v = e(V)
            matrix v1 = v[1,1]
            matrix v2 = v[2,2]
            matrix v3 = v[3,3]
            matrix v4 = v[4,4]
    
            local df  : display df[1,1]
            local v1  : display v1[1,1]
            local v2  : display v2[1,1]
            local v3  : display v3[1,1]
            local v4  : display v4[1,1]
            
            local w1= (invttail(`df',0.025))*(sqrt(`v1')/(`b1'*(1-`b1')))
            local w2= (invttail(`df',0.025))*(sqrt(`v2')/(`b2'*(1-`b2')))
            local w3= (invttail(`df',0.025))*(sqrt(`v3')/(`b3'*(1-`b3')))
            local w4= (invttail(`df',0.025))*(sqrt(`v4')/(`b4'*(1-`b4')))
            
    
            local p1=log(`b1'/(1-`b1'))
            local lb_a1=invlogit(`p1' - `w1')
            local ub_a1=invlogit(`p1' + `w1')
            
            local p2=log(`b2'/(1-`b2'))
            local lb_a2=invlogit(`p2' - `w2')
            local ub_a2=invlogit(`p2' + `w2')    
            
            local p3=log(`b3'/(1-`b3'))
            local lb_a3=invlogit(`p3' - `w3')
            local ub_a3=invlogit(`p3' + `w3')    
    
            local p4=log(`b4'/(1-`b4'))
            local lb_a4=invlogit(`p4' - `w4')
            local ub_a4=invlogit(`p4' + `w4')            
            
    
            local b1a  : display %4.1f b1[1,1]*100
            local lb_a1 : display %4.1f  (invlogit(`p1' - `w1'))*100
            local ub_a1 : display %4.1f (invlogit(`p1' + `w1'))*100
            
            local b2a  : display %4.1f b2[1,1]*100
            local lb_a2 : display %4.1f  (invlogit(`p2' - `w2'))*100
            local ub_a2 : display %4.1f (invlogit(`p2' + `w2'))*100
    
            local b3a  : display %4.1f b3[1,1]*100
            local lb_a3 : display %4.1f  (invlogit(`p3' - `w3'))*100
            local ub_a3 : display %4.1f (invlogit(`p3' + `w3'))*100
    
            local b4a  : display %4.1f b4[1,1]*100
            local lb_a4 : display %4.1f  (invlogit(`p4' - `w4'))*100
            local ub_a4 : display %4.1f (invlogit(`p4' + `w4'))*100    
    
    
            local ++row
        putexcel B`row'=("`na'/`Na'") C`row'=("`b1a'") D`row'=("(`lb_a1'-`ub_a1')")
        putexcel B`row'=("`na'/`Na'") C`row'=("`b2a'") D`row'=("(`lb_a2'-`ub_a2')")
    
        local ++row




  • #2
    This doesn't have to do with loops, but one way to shorten your code substantially would be to use the -estpost- prefix (from the -estout- package by Ben Jann available on SSC) before your initial tabulation command. This would look like:

    Code:
     
     estpost svy, subpop(if e4==1): tab var1, ci
    The nice thing about this is that it stores the calculated upper and lower bounds in seperate matrices (e(lb) and e(ub)), so you don't have to calculate these from scratch from the covariance matrix.

    Comment


    • #3
      Huge help thank you,

      regards
      CK

      Comment

      Working...
      X