Announcement

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

  • Output multiple hypothesis tests into nice looking table

    Dear Statalisters,

    I am tearing my hair about here. This seems like a very simple issue but I just can't find anything that works. Sorry if it is a bit of a stupid question.

    What I have is this:

    I wish to cross-tabulate multiple variables (coded as present or absent) according to the participant being antibody positive or negative and then do a hypothesis test. I have added in the cc command as I wish to present odd's ratios as well as p values:

    foreach var in VAR1 VAR2 VAR3 VAR4{
    tab `var' ANTIBODY, chi2
    cc `var' ANTIBODY
    }

    I have wasted a lot of time copying and pasting in to MS word and making errors along the way. Is there a way to ask Stata to make a nice looking table like this:

    Variable ANITBODY POS ANTIBODY NEG p OR
    VAR1 n (%) n (%) x.xxx x.xx-x.xx
    VAR2 n (%) n (%) x.xxx x.xx-x.xx
    VAR3 n (%) n (%) x.xxx x.xx-x.xx
    VAR4 n (%) n (%) x.xxx x.xx-x.xx
    VARX n (%) n (%) x.xxx x.xx-x.xx


    Thank you in advance...

    James

  • #2
    HI James, have your tried "tabout"?

    it can make 2x2 tables and export it to excel. Don't know if this helps.

    Comment


    • #3
      Hi,
      I have tried to use tabout, but don't seem to be able to make it perform individual tests for each variable and then combine into a single table.

      James

      Comment


      • #4
        Hi James,

        The below is not elegant, but I think it would work. Something similar worked for me...
        Please ask if anything is unclear, and obviously I apologize if the mistakes are still there...

        Best,
        Elena

        capture erase tabstat.dta
        *cleans STATA datafile to be used
        tempname hdle

        postfile `hdle' str32(var) Variable ANITBODY_POS AP% ANTIBODY_NEG AN% p OR using tabstat
        *opens new STATA datafile for writing

        foreach var of varlist VAR1 VAR2 VAR3 VAR4 {
        quietly {
        count (`var') if `var'!=""
        scalar Total=r(N)
        *calculated the number of non-missing observations for `var' and saves in into scalar called Total

        count ( `var') if ANTIBODY=="positive"&`var'=="present"
        scalar AP=r(N)
        *calculated the number of observations for `var'="present" if ANTIBODY=="positive" and saves in into scalar called AP

        count ( `var') if ANTIBODY=="negative"&`var'=="present"
        scalar AN=r(N)
        *same for AN

        scalar shareAP=AP/Total*100
        scalar shareNP=NP/Total*100
        *calculates percentages of the split

        tab `var' ANTIBODY, chi2
        g CHI2=r(chi2)
        *saves Chi2 statistics into the scalar CHI2

        cc `var' ANTIBODY
        g OR=r(or)
        *saves or statistics into the scalar OR

        post `hdle' ("`var'") (AP) (shareAP) (AN) (shareAN) (CHI2) (OR)
        *posts this to a Stata datafile

        drop AP shareAP AN shareAN CHI2 OR
        *clears the scalars for the next round (in principle, not needed)
        }
        }

        postclose `hdle'
        *closes posting to STATA datafile

        use tabstat, clear

        * Saves STATA data to spreadsheet
        xmlsave "Table 1", doctype(excel) replace

        Comment


        • #5
          Hmm... Could get that to run.

          I did some debugging and have ended up with this, but I just get a syntax error:

          tempname hdle
          tempfile tables1

          postfile `hdle' VariableName ANITBODYPOS APpercent ANTIBODYNEG ANpercent p OR using `tables1'


          foreach var of VAR1 VAR2 VAR 3 {
          count (`var') if `var'!=.
          scalar Total=r(N)


          count (`var') if antibody==1 & `var'==1
          scalar AP=r(N)

          count (`var') if antibody==0 & `var'==1
          scalar AN=r(N)


          scalar shareAP=(AP/Total)*100
          scalar shareNP=(NP/Total)*100


          tab `var' antibody, chi2
          scalar CHI2=r(chi2)


          cc `var' MUP4401
          scalar OR=r(or)


          post `hdle' ("`var'") (AP) (shareAP) (AN) (shareAN) (CHI2) (OR)

          }


          postclose `hdle'


          use `tables1', clear

          xmlsave "Table 1", doctype(excel) replace

          Comment


          • #6
            I think I see at least one mistake in my (and then your) code: all lines that use

            count (`var') if condition

            should become

            count if `var'!=. condition

            (as you count observations only).

            Sorry, I first wrote it differently, with egen function, and then forgot to change. Does it help?

            Comment


            • #7
              Correction:

              count (`var') if condition

              should become

              count if condition

              Comment


              • #8
                finally got there... had to change foreach var *of* to *in*

                thanks for you help

                James

                Comment


                • #9
                  Have you tried estpost tabulate from the SSC's estout package by Ben Jann? I think you can get all the wanted results from a single command and make a table with estout or esttab.

                  Comment

                  Working...
                  X