Announcement

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

  • Chi-square for subsample

    Hi all,
    A snapshot of my data is below (I have 950 data points, so its only a small section). I am running a chi-square to test the frequency of each stage vs. country using the command below:

    HTML Code:
    tab stage1 country, chi2
    tab stage2 country, chi2
    tab stage3 country, chi2
    tab stage4 country, chi2
    Now I'd like to compare only certain countries for each stage. How would I run a test, for example, to compare stage1 for country "1" and "3" only to see if there is a significant difference?
    TIA

    Click image for larger version

Name:	2019-12-27_13-51-47.png
Views:	1
Size:	21.5 KB
ID:	1529963

  • #2
    I'm not sure I understand what you want, but did you try
    Code:
    tab stage1 country if inlist(country, 1, 3), chi2
    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      I'm not sure I understand what you want, but did you try
      Code:
      tab stage1 country if inlist(country, 1, 3), chi2
      Perfect, that is just what I was looking for. And thanks for the info on dataex, I was wondering what the best way to share data was and now I know.

      Comment


      • #4
        Clyde's code is very elegant and solved the issue.

        Just to show you some alternatives, you may a) use the if condition directly (without inlist); b) use preserve, select the desired subgroup, perform the estimations, then restore.

        Both strategies are shown in the example below:

        Code:
         
        . sysuse auto
        (1978 Automobile Data)
        
        . sum mpg
        
            Variable |        Obs        Mean    Std. Dev.       Min        Max
        -------------+---------------------------------------------------------
                 mpg |         74     21.2973    5.785503         12         41
        
        . gen highmpg = mpg > 22
        
        . tab rep78
        
             Repair |
        Record 1978 |      Freq.     Percent        Cum.
        ------------+-----------------------------------
                  1 |          2        2.90        2.90
                  2 |          8       11.59       14.49
                  3 |         30       43.48       57.97
                  4 |         18       26.09       84.06
                  5 |         11       15.94      100.00
        ------------+-----------------------------------
              Total |         69      100.00
        
        
        . */ you may start from here
        
        . tab highmpg foreign if rep78 == 1 | rep78 ==3, chi2
        
                   |       Car type
           highmpg |  Domestic    Foreign |     Total
        -----------+----------------------+----------
                 0 |        25          1 |        26 
                 1 |         4          2 |         6 
        -----------+----------------------+----------
             Total |        29          3 |        32 
        
                  Pearson chi2(1) =   4.9891   Pr = 0.026
        
        . */ other strategy
        
        . preserve
        
        . keep if rep78 ==1 | rep78 ==3
        (42 observations deleted)
         
        . tab highmpg foreign, chi2
        
                   |       Car type
           highmpg |  Domestic    Foreign |     Total
        -----------+----------------------+----------
                 0 |        25          1 |        26 
                 1 |         4          2 |         6 
        -----------+----------------------+----------
             Total |        29          3 |        32 
        
                  Pearson chi2(1) =   4.9891   Pr = 0.026
        
        . restore
        Best regards,

        Marcos

        Comment

        Working...
        X