Announcement

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

  • Looping regressions

    Hello, I'm looping over a regression so that in each loop 1 observation is dropped, for example in the first loop only observation 1 is dropped, loop 2 only observation 2 is dropped and observation 1 is back. so far this is the code im working with (im trying to store the coefficients of one of the variables in each loop)

    sysuse auto,clear
    gen coef = _n
    forvalues i = 1/74{
    drop if _n=`i'
    reg price mpg trunk
    replace coef = _b[mpg] if _n==`i'
    }

    the problem with the drop command is it ommits the observations but does not return them back in the second loop so the number of observations keeps dropping rather than always being n-1 in each regression. What is the code used to only ommit it for the regression in the loop.

  • #2
    Instead of first dropping the observation and then doing the regression, you should just do the regression without the observation:

    Code:
    sysuse auto, clear
    gen coef = _n
    forval i = 1/74{
    reg price mpg trunk if _n != `i'
    replace coef = _b[mpg] if _n == `i'
    }

    Comment


    • #3
      Code:
      sysuse auto,clear
      gen coef = .
      forvalues i = 1/74{
          reg price mpg trunk if _n != `i'
          replace coef = _b[mpg] in `i'
      }
      A slightly fancier way to do this is:
      Code:
      rangestat (reg) price mpg trunk, interval(price . .) excludeself
      -rangestat- is written by Robert Picard, Nick Cox, and Roberto Ferrer. It is available from SSC.

      Comment


      • #4
        This worked thank u so much guys

        Comment

        Working...
        X