Announcement

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

  • e(r2_p) as a weight on a loop of regressions

    Hello everyone,

    I would like to run a loop of 10 logit regressions following more or less the example provided in 1379714-help-on-writing-a-loop-for-multiple-regressions where:


    logit y x1
    logit y x1 x2
    logit y x1 x2 x3
    logit y x1 x2 x3 x4
    ​​​​​​​logit y x1 x2 x3 x4 x5
    ​​​​​​​logit y x1 x2 x3 x4 x5 x6
    ​​​​​​​logit y x1 x2 x3 x4 x5 x6 x7
    ​​​​​​​logit y x1 x2 x3 x4 x5 x6 x7 x8
    ​​​​​​​logit y x1 x2 x3 x4 x5 x6 x7 x8 x9
    ​​​​​​​logit y x1 x2 x3 x4 x5 x6 x7 x8 x10
    [/CODE]

    And thus I followed the code suggested in the post:

    Code:
    local predictors x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
    local regressors
    
    foreach p of local predictors {
         local regressors `regressors' `p'
         logit y `regressors'
    }
    I would like to weigh each of these 10 regressions by the pseudo R^2 of each one to make a table in the end with maximum, minimum and average coefficients. I've tried running:

    Code:
    foreach p of local predictors {
         local regressors `regressors' `p'
         quietly logit y `regressors'
         local pr2_`p' = `e(r2_p)'
            foreach v of local earliest {
                 local regressors `regressors' `p'
                 logit y `regressors' [weight=pr2_`p']
            }
    }
    But it offers an obvious code of
    Code:
    pr2_ not found
    I think (honestly not very sure at this point) that what I'm doing wrong is that the pseudo R^2 is not associated with each variable v but to the whole model in general, but I don't know how to create a local for all of the 10 regressions models. Do I need to create a local for each of the 10 models? If yes, how do I do this?

    I'd appreciate it if anyone has any advice on how to do this.

    Thanks,
    Jonas
    Last edited by Jonas Bollinger; 17 Oct 2020, 10:18.

  • #2
    The error message you are getting is because to access the contents of local pr2_`p' you have to write `pr2_`p'' .

    That fix will get rid of the error message, but you still won't get what you want. What you need to do is create a frame (assuming you are running version 16, if you are on an older version use a tempfile instead) that will hold the coefficients of the regressions and the value of e(r2_p). You will have to post the coefficients and e(r2_p) after the regression into that frame (or postfile). Doing that will require some care because not every variable participates in each regression, so you have to arrange to post a missing value for each coefficient of a non-participating variable. It's a little tedious coding that, but not difficult in principle.

    The inner loop of your code, the one beginning -foreach v of local earliest- is just wrong and should be eliminated. It makes no sense to apply a constant weight to the entire regression: that changes nothing!

    Once you have completed the loop and all the coefficients are posted, you can then switch to the results frame (or -use- the closed postfile) and run -summ- specifying the weight in that command. Note, by the way, that the minimum and maximum will not be affected by the weighting, only the average.




    Comment


    • #3
      Hello Clyde, thank you for taking the time to answer.

      I tried to follow through with your advice, but I'm a bit a loss. Following your advice on: 1576600-table-after-tuples-forval #15 (I am using Stata 14.2), is something like this what you mean? Naturally this code doesn't work because I am making some sort of stupid mistake here, but I'm untrained with loops and postfiles/tempfiles/etc, but I was wondering if it's a step in the right direction:

      Code:
      tempfile logitresults
      
      capture postutil clear
      
      local logitresults_vars
      foreach p of local predictors {
            local regressors `regressors' `p'
            quietly logit y `regressors'
            local pr2_`p' = `e(r2_p)'
      
      }
      
      postfile handle `logitresults_vars' using `logitresults'
      
      foreach p of local predictors {
            local regressors `regressors' `p'
            logit y `regressors' [weight=`pr2_`p'']
            summ
      }
      
      postclose handle
      Thank you once more.

      Comment


      • #4
        You need to create the postfile before you do any of the regressions. Then within the loop you need to post the results to the postfile at each iteration. After the loop, you close the postfile and -use- it.

        Code:
        tempfile logitresults
        
        capture postutil clear
        postfile results b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 r2p using `logitresults'
        
        local predictors x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
        local regressors
        foreach p of local predictors {
              local regressors `regressors' `p'
              quietly logit y `regressors'
              local n_regressors: word count `regressors'
              local topost
              forvalues i = 1/`n_regressors' {
                  local topost `topost' (_b[x`i'])
              }
              forvalues i = `=`n_regressors'+1'/10 {
                  local topost `topost' (.)
              }
              local topost `topost' (`e(r2_p)')
              post results `topost'
        }
        postclose results
        
        use `logitresults', clear
        
        summ b* [aweight = r2p]

        Comment


        • #5
          Thank you so much, you just solved all my problems!

          Comment

          Working...
          X