Announcement

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

  • Please help: Using cell from tabulation in function

    Hi all,

    I'm fairly new to Stata, and I have encountered a problem I hope you can help me solve.

    In essence, I have a two-way table: on the one axis, parental status (low and high, respectively) and on the other, offspring status (low and high).

    I produce the table, and it gives me four numbers of observations, e.g.
    1652 9820
    6520 1360

    Then I would like to use the CSI (relative odds/risk ratio) based on these numbers. If I enter it manually, it would look like this:

    csi 1652 9820 6520 1360, or

    Since I will have to do this for many different sets (geographical units), I do not have time to read the tables and manually enter these numbers into the csi function. Also, doing so involves a great amount of risk that I'll make mistakes along the way.

    Can anyone please help me automate this so that Stata knows I want the numbers from the table?

    I have tried for hours and hours, and I have used the tab option matcell, but when entering:

    csi matname[1,1] matname[1,2] matname[2,1] matname[2,2], or

    I get the following error: matcellname found where integer expected. Curiously, I can say, e.g.: di matcellname[1,2] + matcellname[2,2] and get the correct result.

    Any help would be greatly appreciated!

    Chris

  • #2
    kindly post threads on general, otherwise you wont get the answer.

    Comment


    • #3
      As this thread is in General, #2 is already addressed.

      I have a different request: As almost every thread could be prefixed "Please help:" that is not needed (and we won't consider omitting as impolite).

      Here is a positive answer: Check out

      FAQ . . . . . . . . . . . . . Accumulating results from immediate commands
      . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
      10/15 How do I accumulate the results of immediate commands?
      http://www.stata.com/support/faqs/data-management/
      accumulating-results-from-immediate-commands/

      Comment


      • #4
        Thank you very much for your response. Your request is duly noted.

        I've checked your link and I have been trying to make it work for a couple of hours, but sadly to no avail.

        I don't mean to be obtuse, but isn't there a way to simply say (here, in pseudo-code)

        Loop, for each geographical unit
        perform task: tab var1, var 2

        Insert row1/col1, r1/c2, r2/c1, r2/c2 into the csi function

        Rinse, repeat?

        Is it necessary to clear the memory and create new files for each run?

        Kind regards,
        Chris

        Comment


        • #5
          No need to feel obtuse. I feel obtuse using Stata sometimes and I both write programs and have used it for maybe 6 years now.

          My request for you now is to present a data example which illustrates the problem.

          Use dataex to show us your example data, and then run the exact code you ran to reproduce the error or problem in question.

          Comment


          • #6
            The csi command - the immediate version of the cs command - given in post #1 allows us to construct the example tabulation.

            The output of help cs tells us about all the results that the cs and csi commands return in r(). The example below uses this to set the local macro riskratio to the the value reported by the cs command. I think this approach (use the cs command rather than the csi command) will help you to follow the guidance Nick Cox gave you. No need to run the tabulate command - the cs comand will provide that for you.
            Code:
            . tabulate status1 status2
            
                       |        status2
               status1 |         0          1 |     Total
            -----------+----------------------+----------
                     0 |     1,350      6,520 |     7,870
                     1 |     9,820      1,652 |    11,472
            -----------+----------------------+----------
                 Total |    11,170      8,172 |    19,342
            
            . cs status1 status2, or
            
                             |        status2         |
                             |   Exposed   Unexposed  |      Total
            -----------------+------------------------+-----------
                       Cases |      1652        9820  |      11472
                    Noncases |      6520        1350  |       7870
            -----------------+------------------------+-----------
                       Total |      8172       11170  |      19342
                             |                        |
                        Risk |  .2021537    .8791406  |   .5931134
                             |                        |
                             |      Point estimate    |    [95% conf. interval]
                             |------------------------+------------------------
             Risk difference |        -.6769869       |   -.6875868   -.6663869
                  Risk ratio |         .2299447       |    .2201305    .2401964
             Prev. frac. ex. |         .7700553       |    .7598036    .7798695
             Prev. frac. pop |         .3253486       |
                  Odds ratio |         .0348325       |    .0322052    .0376742 (Cornfield)
                             +-------------------------------------------------
                                           chi2(1) =  8962.51  Pr>chi2 = 0.0000
            
            . return list
            
            scalars:
                            r(afp) =  .3253485708316863
                         r(ub_afe) =  .7798694810561047
                         r(lb_afe) =  .7598036059347512
                            r(afe) =  .770055317795702
                          r(ub_or) =  .0376742021956104
                          r(lb_or) =  .032205155323997
                             r(or) =  .0348325065910312
                          r(ub_rr) =  .2401963940652488
                          r(lb_rr) =  .2201305189438954
                             r(rr) =  .229944682204298
                          r(ub_rd) =  -.6663869302230486
                          r(lb_rd) =  -.6875867888018025
                             r(rd) =  -.6769868595124255
                              r(p) =  0
                           r(chi2) =  8962.507195509905
            
            . local riskratio = r(rr)
            
            . display `riskratio'
            .22994468
            For details, read the output of
            Code:
            help cs
            help return
            and section 18.3 of the Stata User's Guide PDF included in your Stata installation and accessible from Stata's Help menu.
            Last edited by William Lisowski; 18 Aug 2022, 09:58.

            Comment


            • #7
              You are absolute lifesavers! Thank you so, so much.

              With a little tweaking of the variables, I got William Lisowski's code to work. (The variables had to be coded 1's and 0's).

              Thank you all for the help - and for the encouraging words, Jared Greathouse.

              Chris

              Comment

              Working...
              X