Announcement

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

  • #16
    I'd settle for a penny for every minute I spent on minor syntax errors!

    Comment


    • #17
      I'm hoping to learn from this thread as it's the closest I've come to the question I have.

      I'm running a looped mixed effects regression analysis on brain connectivity data (connections between one area of the brain and ~300 others). My predictor variable is a categorical variable with 3 groups - one exposure and 2 controls. I run the following loop without difficulty:

      foreach var of varlist l_default_1-diencephalon_ventral_right {
      xtmixed `var' i.Groups asr_scr_totprob_r devhx_18_p devhx_12a_p i.highest_ed_category i.race_ethnicity, || rel_family_id: || siteID:
      }

      Because Stata is great and recognizes categorical variables with the i. precursor I have used that. My goal is to extract the p values for the Group variable only for all of the 300 regressions. - Having run the loop and logged it there are only a handful of significant p values but to appropriately correct for multiple analyses I want to extract them all

      I have attempted to use code similar to that described in this thread but haven't figured out the syntax using my categorical predictor:

      capture postutil clear
      tempfile results
      postfile handle Groups se_Groups using `results'

      foreach var of varlist l_default_1-l_smmouth_3 {

      // RUN MIXED EFFECTS REGRESSION
      xtmixed `var' i.Groups asr_scr_totprob_r devhx_18_p devhx_12a_p i.highest_ed_category i.race_ethnicity, || rel_family_id: || siteID:

      // LOOP OVER REGRESSORS TO CREATE THE MATERIAL TO POST
      local topost ("`var'")
      foreach x in Groups {
      local topost `topost' (_b[`var':`x']) (_se[`var':`x'])
      }

      // POST IT
      post handle `topost'
      }

      postclose handle

      use `results', clear

      foreach v of varlist Groups{
      gen t_`v' = `v'/se_`v'
      gen p_`v' = 2*normal(-abs(t_`v'))
      order t_`v' p_`v', after(se_`v')
      }

      You may get the sense that I'm new to temp files etc, which is accurate! Any help would be appreciated.

      Comment


      • #18
        Since Group is a categorical variable, it gets expanded to a series of indicator ("dummy") variables, one for each level of Group except for one, which is omitted as the base category. I will assume that your Group variable is coded as 0, 1, and 2, which Stata will handle by using 0 as the base category (unless you code to tell it to do otherwise). So the "variables" whose coefficients and standard errors you need are going to be called 1.Group and 2.Group. Since there are only two of those, there is really no point in writing a loop to cover them: you can just spell them both out in your -post- command. The -postfile- command must also be modified to reflect this change. In addition, the -postfile- command you showed was incorrect by virtue of creating no variable to hold "`var'". Putting this all together the code should look something like this:
        Code:
        capture postutil clear
        tempfile results
        postfile handle str32 dep_var Groups_1 se_Groups_1 Groups_2 se_Groups_2 using `results'
        
        foreach var of varlist l_default_1-l_smmouth_3 {
        
            // RUN MIXED EFFECTS REGRESSION
            xtmixed `var' i.Groups asr_scr_totprob_r devhx_18_p devhx_12a_p i.highest_ed_category i.race_ethnicity, || rel_family_id: || siteID:
        
            // LOOP OVER REGRESSORS TO CREATE THE MATERIAL TO POST
            post handle ("`var'") (_b[`var':1.Groups]) (_se[`var':1.Groups]) ///
                (_b[`var':2.Groups]) (_se[`var':2.Groups])
        }
        
        postclose handle
         
        // ETC.
        Bear in mind that this code creates set of a results that contrast the coefficient of category 1 of Group with category 0, and category 2 of Group with category 0. Those may not be the most meaningful contrasts to look at for your purposes, depending on what is coded 0, what is 1, and what is 2. So you might want to change the -post handle- code, changing the 1. and 2. to represent the two groups you actually want to compare with the other group. (You can use 0.Groups in the _b[] and _se[] constructions even though there is no explicit 0.Groups in the regression output. Internally, Stata will know that it refers to the omitted category.)
        Last edited by Clyde Schechter; 20 Dec 2022, 14:39.

        Comment


        • #19
          This worked! I can't thank you enough Clyde!

          Comment

          Working...
          X