Announcement

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

  • How to export chi2 p-values from stata to excel when using foreach loop command

    I am trying to add a chi-square test p-value column to my cross-tabulation table. I have already created tables using the collect command for the frequencies but don't know how to do that for chi2 test results.
    How to export chi2 p-values from stata to excel when using foreach loop command:

    To get my chi2 results I am using a command like this:
    foreach v of varlist v1 v2 v3 v4 {
    tab `v' group_variable if group_variable!=3, chi2
    }

    The group_variable is a 3 category variable and I am trying to do a pairwise comparison between group_variable==1 and group_variable==2.
    This code works fine and I get all the results correctly, but I want to export all the p values in a table format in excel for all the variables in the loop. Any easy way to do this or any new command line to add here?
    Last edited by Susmita Chakraborty; 23 Jun 2023, 21:42.

  • #2
    Code:
    frame create results  str30 (variable) float (N chi2 p)
    
    sysuse auto, clear
    foreach v of varlist mpg rep78{
        tab `v' foreign, chi2
        frame post results ("`v'") (`r(N)') (`r(chi2)') (`r(p)')
    }
    
    cwf results
    export excel using myfile, replace firstrow(variables) keepcellfmt
    Res.:

    Code:
       Mileage |       Car type
         (mpg) |  Domestic    Foreign |     Total
    -----------+----------------------+----------
            12 |         2          0 |         2
            14 |         5          1 |         6
            15 |         2          0 |         2
            16 |         4          0 |         4
            17 |         2          2 |         4
            18 |         7          2 |         9
            19 |         8          0 |         8
            20 |         3          0 |         3
            21 |         3          2 |         5
            22 |         5          0 |         5
            23 |         0          3 |         3
            24 |         3          1 |         4
            25 |         1          4 |         5
            26 |         2          1 |         3
            28 |         2          1 |         3
            29 |         1          0 |         1
            30 |         1          1 |         2
            31 |         0          1 |         1
            34 |         1          0 |         1
            35 |         0          2 |         2
            41 |         0          1 |         1
    -----------+----------------------+----------
         Total |        52         22 |        74
    
             Pearson chi2(20) =  35.8393   Pr = 0.016
    
        Repair |
        Record |       Car type
          1978 |  Domestic    Foreign |     Total
    -----------+----------------------+----------
             1 |         2          0 |         2
             2 |         8          0 |         8
             3 |        27          3 |        30
             4 |         9          9 |        18
             5 |         2          9 |        11
    -----------+----------------------+----------
         Total |        48         21 |        69
    
              Pearson chi2(4) =  27.2640   Pr = 0.000
    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	30.8 KB
ID:	1718291

    Last edited by Andrew Musau; 24 Jun 2023, 09:45.

    Comment


    • #3
      It works! Thanks a lot!

      Comment

      Working...
      X