Announcement

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

  • Adding Variables Gradually in Regressions

    Hi,

    I want to add multiple independent variables in regressions gradually with same dependent variable and previous independent variables.

    Instead of writing regressions separately, is there any smarter way to do that, like, use a loop?

    For example, the stupidest way is

    webuse auto.dta, clear

    reg price mpg
    est store m1

    reg price mpg rep78
    est store m2

    reg price mpg rep78 headroom
    est store m3

    reg price mpg rep78 headroom trunk
    est store m4

    reg price mpg rep78 headroom trunk weight
    est store m5

  • #2
    You probably want to do stepwise regression. See the help.

    By the way, there is a way to do it cleverly rather than recompute everything at each step, using QR decomposition update, but I am not aware of any use of this approach in statistical software.

    Jean-Claude Arbaut
    Last edited by Jean-Claude Arbaut; 01 May 2019, 14:42.

    Comment


    • #3
      Welcome to Statalist.

      Something like the following might point you in a useful direction.
      Code:
      webuse auto.dta, clear
      
      local varlist mpg rep78 headroom trunk weight
      local vars 
      
      forvalues i=1/5 {
          local v : word `i' of `varlist'
          local vars `vars' `v'
          quietly regress price `vars'
          estimates store m`i'
          }
      
      estimates dir
      Code:
      . local varlist mpg rep78 headroom trunk weight
      
      . local vars 
      
      . 
      . forvalues i=1/5 {
        2.     local v : word `i' of `varlist'
        3.         local vars `vars' `v'
        4.         quietly regress price `vars'
        5.         estimates store m`i'
        6.         }
      
      . 
      . estimates dir
      
      -------------------------------------------------------
              name | command      depvar       npar  title 
      -------------+-----------------------------------------
                m1 | regress      price           2  
                m2 | regress      price           3  
                m3 | regress      price           4  
                m4 | regress      price           5  
                m5 | regress      price           6  
      -------------------------------------------------------

      Comment


      • #4
        Originally posted by William Lisowski View Post
        Welcome to Statalist.

        Something like the following might point you in a useful direction.
        Code:
        webuse auto.dta, clear
        
        local varlist mpg rep78 headroom trunk weight
        local vars
        
        forvalues i=1/5 {
        local v : word `i' of `varlist'
        local vars `vars' `v'
        quietly regress price `vars'
        estimates store m`i'
        }
        
        estimates dir
        Code:
        . local varlist mpg rep78 headroom trunk weight
        
        . local vars
        
        .
        . forvalues i=1/5 {
        2. local v : word `i' of `varlist'
        3. local vars `vars' `v'
        4. quietly regress price `vars'
        5. estimates store m`i'
        6. }
        
        .
        . estimates dir
        
        -------------------------------------------------------
        name | command depvar npar title
        -------------+-----------------------------------------
        m1 | regress price 2
        m2 | regress price 3
        m3 | regress price 4
        m4 | regress price 5
        m5 | regress price 6
        -------------------------------------------------------
        It's exactly what i am looking for. Thanks

        Comment

        Working...
        X