Announcement

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

  • Regression models with exposure, outcome, and GROUP of covariates in one loop

    Hi All - just struggling to get some basic regression code to work how I want it in Stata. Imagine is an easy fix when you know how, but wonder if anyone can help?

    Basically - I want to pull in and output regression results for models running on exposure (xvars), outcome (yvars) and a GROUP of covariates. I have done this with global and locals, but the problem I am having is that for the covariates Stata want's to run a regression for EACH INDIVIDUAL covariate in globals cov2 cov3 cov4 (rather than run each global as a GROUP of covariate). I thought having as a $global would help, but wondered if anyone knew how to run the regression loop so it would work on a GROUP of covariates?

    Thanks in advance - example below.


    Code:
    clear all
    use "$data\analysis.dta", clear
            
    local row = 2
    
    global cov1  //unadjusted
    global cov2 age_acc i.sex i.Eth2_v1 whitecell_count
    global cov3 $cov2 i.ed3_acc i.emp townsenddi i.par_hist_dis i.Smok i.alc_cat3 i.ofish2 i.fruitveg prmeat i.salt2
    global cov4 $cov3 i.BMI_cat
    
    global xvars enmo_accel_ad ig_ad
    global yvars telo_l telo_z
    global covars $cov1 $cov2 $cov3 $cov4
    
    foreach x of global xvars{
    foreach y of global yvars{
    foreach model of global covars{
    
    regress `y' `x' `model'
     
    matrix b = r(table)'
    matrix list b
    matselrc b c, c(1 5 6 4) r(1)  // select columns 1,5,6,4 of matrix b & store in new matrix c
    matrix list c
        putexcel A`row'="`x'"
        putexcel B`row'="`y'"
        putexcel D`row'=mat(c)
        putexcel C`row'="`model'"
        putexcel D`row'=mat(c)
            matrix drop c
    
    local row =`row' + 1        
            
    }
    }
    }
    Last edited by patrick handcock; 20 Oct 2020, 06:17.

  • #2
    I think you want:

    Code:
    clear all
    use "$data\analysis.dta", clear
            
    local row = 2
    
    global cov1  //unadjusted
    global cov2 age_acc i.sex i.Eth2_v1 whitecell_count
    global cov3 $cov2 i.ed3_acc i.emp townsenddi i.par_hist_dis i.Smok i.alc_cat3 i.ofish2 i.fruitveg prmeat i.salt2
    global cov4 $cov3 i.BMI_cat
    
    global xvars enmo_accel_ad ig_ad
    global yvars telo_l telo_z
    global covars $cov1 $cov2 $cov3 $cov4 // DELETE THIS--IT ISN'T USEFUL FOR YOUR PURPOSES
    
    foreach x of global xvars{
        foreach y of global yvars{
            forvalues i = 1/4 {
    
                regress `y' `x' ${cov`i'}
                
                // ETC.
    By the way, it is much safer to use local macros, not globals, for holding variable lists like this.
    Last edited by Clyde Schechter; 20 Oct 2020, 11:54.

    Comment


    • #3
      Thanks very much Clyde Schechter - very helpful!

      Comment

      Working...
      X