Announcement

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

  • Using stored results from csi command to add risk ratios with 95% CI's

    Using Stata version 18.
    Hi All, I have a straightforward question. I have a dataset with just 24 rows that I am trying to calculate risk ratios with 95% CI's.

    Here is the direct command I use:

    csi 54227 250552 27476 227947 // this gives me what I want. Risk ratios with upper and lower bound 95% CI estimates. If I type return list after this, I see that the values are stored in r(rr), r(lb_rr) and r(ub_rr)

    But then when I attempt to replace a variable I've created with a stored value from this command using this code:
    replace lowerbound[1] = r(lb_rr)

    I get this error:
    "weights not allowed
    r(101);"

    I'm actually running the code in a loop for each observation. But after the first row of data, I get this error message above. So I just tried using direct commands with the first observation and get the same message.

    If this is more helpful to see some rows of data:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long(any_mski_men any_mski_women) float(men_no_anymski women_no_anymski)
    250552 54227 227947 27476
    136817 39467 141022 28017
     74313  9434 120931  7484
     95684 28828 199172 40146
    242151 54565 226788 27570
    140070 40801 141758 29233
     76117 10053 119678  7897
     94493 29316 197814 41257
    240411 55013 229212 28960
    142652 42698 140726 29485
     79491 10725 114697  7667
     96589 30997 197036 41827
    242143 55744 223708 28728
    143613 44352 142019 30956
     82938 11639 112481  7526
    101319 33652 194852 41547
    219516 53927 248417 32912
    124985 40757 159674 35876
     75929 10832 117494  8223
     91632 32534 208484 45125
    220248 53517 246776 32534
    120433 40891 165301 37124
     79827 11120 111046  7839
     97199 34405 206582 45034
    end
    Here is my code using the loop that produces the same error message:

    clear
    set more off

    use raw_prev_any_merged

    gen risk_ratio = .
    gen lowerbound = .
    gen upperbound = .

    forvalues i = 1/`=_N' {
    local aw = any_mski_women[`i']
    local am = any_mski_men[`i']
    local nw = women_no_anymski[`i']
    local nm = men_no_anymski[`i']

    csi `aw' `am' `nw' `nm'

    * Store the results in the new variables
    replace risk_ratio[`i'] = r(rr)
    replace lowerbound[`i'] = r(lb_rr)
    replace upperbound[`i'] = r(ub_rr)
    }

    Thanks for any help!
    Ben

  • #2
    Hello Ben Hando. Your code ran for me after I changed the last 3 lines as follows:

    Code:
    * Store the results in the new variables
    replace risk_ratio = r(rr) if _n==`i'
    replace lowerbound = r(lb_rr) if _n==`i'
    replace upperbound = r(ub_rr) if _n==`i'
    Here is the slightly edited code:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long(any_mski_men any_mski_women) float(men_no_anymski women_no_anymski)
    250552 54227 227947 27476
    136817 39467 141022 28017
     74313  9434 120931  7484
     95684 28828 199172 40146
    242151 54565 226788 27570
    140070 40801 141758 29233
     76117 10053 119678  7897
     94493 29316 197814 41257
    240411 55013 229212 28960
    142652 42698 140726 29485
     79491 10725 114697  7667
     96589 30997 197036 41827
    242143 55744 223708 28728
    143613 44352 142019 30956
     82938 11639 112481  7526
    101319 33652 194852 41547
    219516 53927 248417 32912
    124985 40757 159674 35876
     75929 10832 117494  8223
     91632 32534 208484 45125
    220248 53517 246776 32534
    120433 40891 165301 37124
     79827 11120 111046  7839
     97199 34405 206582 45034
    end
    
    //clear
    //set more off
    //use raw_prev_any_merged
    
    gen risk_ratio = .
    gen lowerbound = .
    gen upperbound = .
    
    forvalues i = 1/`=_N' {
    local aw = any_mski_women[`i']
    local am = any_mski_men[`i']
    local nw = women_no_anymski[`i']
    local nm = men_no_anymski[`i']
    
    quietly csi `aw' `am' `nw' `nm'
    
    * Store the results in the new variables
    quietly replace risk_ratio = r(rr) if _n==`i'
    quietly replace lowerbound = r(lb_rr) if _n==`i'
    quietly replace upperbound = r(ub_rr) if _n==`i'
    }
    
    list, clean
    If you want to check that the stored results against the displayed results, remove the quietly before csi.

    And here is the output from that final list command:
    Code:
    . list, clean
    
           any~_men   any~omen   men_no~i   women_~i   risk_r~o   lowerb~d   upperb~d  
      1.     250552      54227     227947      27476   1.267537   1.260485   1.274629  
      2.     136817      39467     141022      28017   1.187644   1.178896   1.196458  
      3.      74313       9434     120931       7484   1.465075    1.44389    1.48657  
      4.      95684      28828     199172      40146   1.287952   1.274842   1.301197  
      5.     242151      54565     226788      27570   1.286518    1.27934   1.293737  
      6.     140070      40801     141758      29233   1.172198   1.163687    1.18077  
      7.      76117      10053     119678       7897   1.440626   1.420448    1.46109  
      8.      94493      29316     197814      41257   1.285008   1.271963   1.298187  
      9.     240411      55013     229212      28960   1.279737   1.272532   1.286983  
     10.     142652      42698     140726      29485   1.175062   1.166772   1.183411  
     11.      79491      10725     114697       7667   1.424534   1.405662   1.443659  
     12.      96589      30997     197036      41827   1.293929   1.281192   1.306793  
     13.     242143      55744     223708      28728   1.269581   1.262526   1.276676  
     14.     143613      44352     142019      30956   1.171346   1.163183   1.179566  
     15.      82938      11639     112481       7526   1.430936   1.413159   1.448936  
     16.     101319      33652     194852      41547   1.308129   1.295914   1.320459  
     17.     219516      53927     248417      32912   1.323759   1.315809   1.331757  
     18.     124985      40757     159674      35876   1.211305   1.201853   1.220831  
     19.      75929      10832     117494       8223   1.448105   1.428607    1.46787  
     20.      91632      32534     208484      45125   1.372106   1.358608   1.385738  
     21.     220248      53517     246776      32534   1.318752   1.310824   1.326727  
     22.     120433      40891     165301      37124   1.243558   1.233714    1.25348  
     23.      79827      11120     111046       7839   1.402439   1.384228    1.42089  
     24.      97199      34405     206582      45034   1.353588   1.340796   1.366503
    I hope this helps.
    --
    Bruce Weaver
    Email: [email protected]
    Version: Stata/MP 19.5 (Windows)

    Comment


    • #3
      Wow, this worked great! Thanks so muchh, Bruce. You have truly saved the day. Yes, was just reading about the quirkiness of the replace command but wasn't clear exactly how to fix it. This is great and is a huge help to me. Have a great day!
      Ben

      Comment

      Working...
      X