Announcement

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

  • Testing differences in categorical variables

    Hi everyone,

    I run a regression with the command
    Code:
    ivreghdfe
    on 24 dummy variables (named "wave_serial") that can take either value 1 or 0. I was interested in the difference between the pairwise comparisons between a specific dummy (manely 24.wave_serial) and the others, the problem is that when I use the code
    Code:
    pwcompare wave_serial, effects mcompare(bonferroni)
    , I have an enormous table with all the possible cases. How can I restrict the analysis on the aforementioned comparison?
    I'm giving you an example of the codes I run:

    Code:
    ivreghdfe y1 i.wave_serial, dkraay(2)
    
    pwcompare wave_serial, effects mcompare(bonferroni)

    Ideally, I would like to have a table with 6 columns (because I'm analysing 6 variables: y1, y2... y6) with the 24 comparisons between wave_serial==24 and wave_serial==n with n=1,2,...,23.

    thank you very much for your help

  • #2
    Here's an illustration using the auto.dta data set. In this code rep78 plays the role of wave_serial, with 5 being the value corresponding to wave_serial == 24.

    Code:
    clear*
    
    sysuse auto
    
    local depvars price mpg headroom trunk weight
    levelsof rep78, local(levels)
    local exclude 5
    local levels: list levels - exclude
    frame create results byte rep78 str32 depvar float diff
    
    foreach d of local depvars {
        regress `d' turn displacement gear_ratio i.foreign i.rep78
        foreach l of local levels {
            lincom `l'.rep78 - 5.rep78
            frame post results (`l') ("`d'") (`r(estimate)')
        }
    }
    
    frame change results
    reshape wide diff, i(rep78) j(depvar) string
    rename diff* *
    list, noobs clean
    Now, the above code gives only the difference between the coefficients of `l'.rep78 and 5.rep78. You may want other statistics as well, such as the standard error, or confidence bounds or p-value. All of those are also returned in r(), and you can add those things to the definition of the results frame and to the -frame post- command. Of course, then you will not have 6 columns, you will have 6 * S columns, where S is the number of result statistics you choose.

    By the way, there are only 23 comparisons between 24 and n = 1, 2, ..., 23. Typo?

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      Here's an illustration using the auto.dta data set. In this code rep78 plays the role of wave_serial, with 5 being the value corresponding to wave_serial == 24.

      Code:
      clear*
      
      sysuse auto
      
      local depvars price mpg headroom trunk weight
      levelsof rep78, local(levels)
      local exclude 5
      local levels: list levels - exclude
      frame create results byte rep78 str32 depvar float diff
      
      foreach d of local depvars {
      regress `d' turn displacement gear_ratio i.foreign i.rep78
      foreach l of local levels {
      lincom `l'.rep78 - 5.rep78
      frame post results (`l') ("`d'") (`r(estimate)')
      }
      }
      
      frame change results
      reshape wide diff, i(rep78) j(depvar) string
      rename diff* *
      list, noobs clean
      Now, the above code gives only the difference between the coefficients of `l'.rep78 and 5.rep78. You may want other statistics as well, such as the standard error, or confidence bounds or p-value. All of those are also returned in r(), and you can add those things to the definition of the results frame and to the -frame post- command. Of course, then you will not have 6 columns, you will have 6 * S columns, where S is the number of result statistics you choose.

      By the way, there are only 23 comparisons between 24 and n = 1, 2, ..., 23. Typo?
      Dear Clyde.

      Thank you very much for your response. The code is perfect as soon as I adapted for my intentions.

      PS: I confirm that they are actually 23 comparisons.

      Thanks
      Best
      Riccardo

      Comment


      • #4
        Here is another way to do it, modifying the code in #2 to take advantage of the collect system that is available in Stata 17 and up:
        Code:
        sysuse auto, clear
        
        local depvars price mpg headroom trunk weight
        levelsof rep78, local(levels)
        local exclude 5
        local levels: list levels - exclude
        
        collect clear
        foreach d of local depvars {
            regress `d' turn displacement gear_ratio i.foreign i.rep78
            foreach l of local levels {
                qui: collect, tags(depvar[`d'] lvl[`l']) : lincom `l'.rep78 - `exclude'.rep78
                collect label levels lvl `l' "`l' vs. `exclude'"
            }
        }
        
        collect style header result[estimate], level(hide)
        collect layout (lvl) (depvar#result[estimate])
        which produces:
        Code:
        . collect preview
        
        -----------------------------------------------------------
                |     price       mpg  headroom     trunk    weight
        --------+--------------------------------------------------
        1 vs. 5 |  -770.702 -4.145783 -1.282966 -6.075217 -24.69492
        2 vs. 5 | -837.1242 -2.834294  .1449196 -1.668882 -206.5084
        3 vs. 5 |  -358.416 -4.670429   .020161  .0065602 -44.37141
        4 vs. 5 |  -594.349 -4.151717  .0848208 -.0438418 -12.51525
        -----------------------------------------------------------
        Last edited by Hemanshu Kumar; 25 Sep 2025, 23:18.

        Comment

        Working...
        X