Announcement

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

  • How to fit a set of models with all possible variable combinations

    Hi all,

    I have the following code

    Code:
    webuse nhefs, clear
    stset age_lung_cancer, fail(lung_cancer)
    stcox former_smoker smoker male urban1 rural
    estat ic
    I want to find the model with the smallest IC.

    I want to fit all possible models that can be fitted with 1 variable at a time, 2 variables at a time, 3 variables at a time and so on.

    How do I do this? This seems a simple thing to do but I'm struggling to figure this out.

    Thanks

    Andre

  • #2
    Code:
    ssc install tuples
    will allow you to go

    Code:
    tuples smoker male urban1 rural
    and given those 4 variable names, you get 15 tuples (= 2^4 - 1) as accessible local macros, allowing you to go
    Code:
    forval j = 1/15  {
    Code:
       
     stcox former_smoker `tuple`j''  estat ic  } 
    where you may want to go further. If you loop

    Code:
    forval j = 0/15 { 
    
    } 
    then you invoke tuple0 which does not exist (= is empty), but that's fine, and gives you a way to include the benchmark case of no covariates.

    You may want the IC of concern to be put somewhere convenient each time around the loop.

    Comment


    • #3
      Thank you a lot for your help!

      This is the final code

      Code:
      webuse nhefs, clear
      stset age_lung_cancer, fail(lung_cancer)
      stcox i.former_smoker i.smoker i.male urban1 rural interview_age
      estat ic
      
      tuples i.former_smoker i.smoker i.male urban1 rural interview_age, nopython display 
      
      forval j = 1/63 { 
          stcox former_smoker `tuple`j''  
          estat ic
      }

      Comment

      Working...
      X