Announcement

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

  • Performing chi squared with one variable against a group of variables individually

    I've done a ton of looking around, and I've likely just not got the right key words, but my goal is to perform a simple chi squared analysis using one dependent variable against a group of variables (independently) all at once. I'm quite new to STATA and do most of my work in R normally, but am trying to learn a multitude of platforms as some of biomedical group members use STATA exclusively. Thank you.

  • #2
    Your post's title says individually, but your post says all at once. You might want to take would you'd use in R normally, and find the equivalent in Stata, if it exists. From your sketch, I'd probably go with something like:
    Code:
    mlogit one_dependent_variable i.(<group of variables>)

    Comment


    • #3
      Joseph Coveney sorry i mean that one dependent var ("X") compared to ("A", "B","C"...) pairwise, so id get a pairwise of X:A, X:B, X:C chi squared analysis, just doing it all in one command.

      Comment


      • #4
        I don't know of any command that will do it in one line.

        Maybe
        Code:
        foreach var of varlist A B C {
            tabulate X `var', chi2
        }
        is close.

        Comment


        • #5
          this didnt work... i put
          HTML Code:
          . foreach var of varlist male10 diabetes htn {
               tabulate score 'var', chi2}
          is my syntax appropriate
          because i wanted to end up generating a list of X vs (A,B,C etc) chisq significance in stata.
          Last edited by Nicholas Reid; 30 Apr 2020, 02:53.

          Comment


          • #6
            Originally posted by Nicholas Reid View Post
            is my syntax appropriate
            No.

            Copy and paste from the code I presented, substituting only your variable names.

            Comment


            • #7
              Stata has a handy approach to solve this issue.

              If you wish the provide chi-squared test for only the first variable against a list of variables, guess what, you just need to add the firstonly option.

              Please see the example below:

              Code:
              sysuse auto
              su
              gen highprice = price > 6200
              gen highmpg = mpg > 22
              gen highweight = weight > 3020
              */ you can start from here
              tab2 foreign highprice highmpg highweight, chi2 firstonly
              
              -> tabulation of foreign by highprice  
              
                         |       highprice
                Car type |         0          1 |     Total
              -----------+----------------------+----------
                Domestic |        39         13 |        52
                 Foreign |        13          9 |        22
              -----------+----------------------+----------
                   Total |        52         22 |        74
              
                        Pearson chi2(1) =   1.8729   Pr = 0.171
              
              -> tabulation of foreign by highmpg  
              
                         |        highmpg
                Car type |         0          1 |     Total
              -----------+----------------------+----------
                Domestic |        41         11 |        52
                 Foreign |         7         15 |        22
              -----------+----------------------+----------
                   Total |        48         26 |        74
              
                        Pearson chi2(1) =  15.0022   Pr = 0.000
              
              -> tabulation of foreign by highweight  
              
                         |      highweight
                Car type |         0          1 |     Total
              -----------+----------------------+----------
                Domestic |        15         37 |        52
                 Foreign |        20          2 |        22
              -----------+----------------------+----------
                   Total |        35         39 |        74
              
                        Pearson chi2(1) =  23.8885   Pr = 0.000
              Hopefully that helps
              Best regards,

              Marcos

              Comment


              • #8
                Here is some technique that may help. The main idea is to populate variables with results and then show them all at once. The final result appears after the end of the code.


                Code:
                sysuse auto, clear 
                
                gen variable = ""
                gen chisq = .
                gen df = .
                gen double pvalue = .
                
                quietly { 
                
                local i = 0 
                
                foreach v in rep78 headroom { 
                    local ++i 
                    noisily di "{title:`v'}"
                    noisily tab foreign `v', chi2 
                    replace variable = "`v'" in `i'
                    replace pvalue = r(p) in `i'
                    replace chisq = r(chi2) in `i'
                    replace df = (r(r) - 1) * (r(c) - 1) in `i' 
                    noisily di 
                }
                
                format pvalue %8.0g 
                label var pvalue "P-value"
                format df %6.0f 
                format chisq %4.3f 
                label var chisq "chi-square"
                
                } // end quietly
                
                sort pvalue 
                gen order = _n 
                
                * install after -search labmask, sj- 
                labmask order, values(variable)
                
                label var order "variable"
                
                tabdisp order in 1/`i', c(pvalue df chisq)  
                
                * here is the result 
                
                ---------------------------------------------
                 variable |    P-value          df  chi-square
                ----------+-----------------------------------
                    rep78 |    .000018           4      27.264
                 headroom |    .002616           7      21.926
                ----------------------------------------------
                The good news is that this would not need to be more complicated with many more variables.

                In practice, this will be a small nightmare to tweak unless you run code as one from a do-file editor window. To vary it you may need to invest some effort into reading help on particular commands.

                https://www.stata-journal.com/articl...article=pr0053 offers much elementary but fundamental guidance.

                Comment

                Working...
                X