Announcement

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

  • Loop mixed model over a least of predictors and store the coefficients and P values

    Hi,

    I want to loop a mixed model over a list of predictors (fixed effects) and store the coefficients and P values in Stata v. 17.

    Code:
    sysuse auto, clear
    
    xtmixed price mpg || foreign:, covariance(independent) vce(robust)
    
    foreach var of varlist mpg-gear_ratio {
        xtmixed price `var' || foreign:, covariance(exchangeable) vce(robust)
    }
    Can anyone help me with how to proceed to store the coefficients and P values of the fixed effect for each model?

    Thanks in advance,

    Sergio
    Last edited by Sergio Serrano; 30 Aug 2022, 15:16.

  • #2
    Code:
    sysuse auto, clear
    
    xtmixed price mpg || foreign:, covariance(independent) vce(robust)
    
    foreach var of varlist mpg-gear_ratio {
        xtmixed price `var' || foreign:, covariance(exchangeable) vce(robust)
        matrix m_`var' = r(table)
    }
    
    foreach var of varlist mpg-gear_ratio {
        mat list m_`var'
    }

    Comment


    • #3
      Thank you for the fast and useful reply!
      To make the process fully automatic, I would still need to extract the Coefficient and P value of each variable of "varlist" to create a table like the one below.
      Variable Coef P
      rep78 0.7 0.12
      headroom 1.3 0.33
      trunk 2.2 0.44
      ... ... ...
      Any help will be appreciated.

      Comment


      • #4
        Code:
        sysuse auto, clear
        
        foreach var of varlist mpg-gear_ratio {
            xtmixed price `var' || foreign:, covariance(exchangeable) vce(robust)
            matrix m_`var' = r(table)
        }
        
        generate xvar = ""
        generate beta = .
        generate pval = .
        
        local counter = 1
        foreach var of varlist mpg-gear_ratio {
            replace xvar = "`var'" in `counter'
            replace beta = m_`var'[1,1] in `counter'
            replace pval = m_`var'[4,1] in `counter'
            local counter = `counter' + 1
        }
        
        browse xvar beta pval in 1/9
        Last edited by Ken Chui; 30 Aug 2022, 16:25.

        Comment


        • #5
          You nailed it. Thank you so much!

          Comment


          • #6
            Originally posted by Sergio Serrano View Post
            You nailed it. Thank you so much!
            You are welcome. It can probably be slimmed down to just one loop since it seems saving the matrix is no longer the focus:

            Code:
            sysuse auto, clear
            
            generate xvar = ""
            generate beta = .
            generate pval = .
            local counter = 1
            
            foreach var of varlist mpg-gear_ratio {
                xtmixed price `var' || foreign:, covariance(exchangeable) vce(robust)
                replace xvar = "`var'" in `counter'
                replace beta = r(table)[1,1] in `counter'
                replace pval = r(table)[4,1] in `counter'
                local counter = `counter' + 1
            }
            
            browse xvar beta pval in 1/9

            Comment

            Working...
            X