Announcement

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

  • Regressing on a set of independent variables with and without logs in all possible combinations

    Dear Stata folks,

    I have a question: How can i create a loop which regresses my dependent variable on a set of independent variables which are either in the ordinary or log form in all its possible combinations?

    To be more precise: I want to do:
    1. reg y x1 x2 x3 x4 x5 x6 x7 -> give me usual regression statistics, as for instance the adj. R²
    2. reg y log(x1) x2 x3 x4 x5 x6 x7 -> ""
    3. reg y log(x1) log(x2) x3 x4 x5 x6 x7 -> ""
    4. ....
    5. reg y x1 x2 log(x3) x4 x5 x6 log(x7) (that's just one example drawn out of all possible combinations of log/ordinary x.
    I hope it is clear what I'm up to. So far I just created one local with all the xs in ordinary, and one local with all the xs in log form. Now I'm stuck. It's of course necessary to just include either x1 or log(x1).

    P.S: I know this might sound a bit like hacking, but its more a way to find out if this might justify the usage of some variables as logs (based on adj. R²).

    Many thanks in advance

  • #2
    There may be a better way to do this, but this will work:

    Code:
    clear*
    
    local nvars 7
    
    //    SET UP DEMONSTRATION DATA
    set obs 100
    gen y = runiform()
    forvalues i = 1/`nvars' {
        gen x`i' = runiform()
        gen logx`i' = log(x`i')
    }
    
    //    CREATE REGRESSION COMMANDS
    forvalues n = 0/`=2^`nvars' - 1' {
        local nn = `n'
        forvalues i = 1/`nvars' {
            local b`i' = mod(`nn', 2)
            local nn = floor(`nn'/2)
        }
        local command reg y
        forvalues i = 1/`nvars' {
            if `b`i'' {
                local command `command' logx`i'
            }
            else {
                local command `command' x`i'
            }
        }
        `command'
    }
    Note: If you really have only 7 x variables this is viable. But remember that the number of regressions you are asking for is 2n if you have n variables. So if your real n is much larger, this is going to explode.

    Comment


    • #3
      Hey,

      many thanks! After some adjustments (mainly due to the naming), I was able to apply this script. Now I'm trying to figure out how to post the adj. R² after each single regression with the composition of the model into a post-file. I'll give it a shot and post my code here afterwards.

      Again many many thanks!

      Comment

      Working...
      X