Announcement

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

  • How to save my y and residual errors from a regression loop


    Could you tell me why I cannot save my y or residuals here? Everything working except last and I dont know how to store y.
    In addition the looping over values is not performing, it only does the 10 obs specified in local mc = 10.

    Thanks much!
    clear local mc = 10

    set obs `mc'

    g data_store_x3 =.

    g data_store_x2 =.

    g data_store_con= .

    g data_store_y =.

    quietly{
    forvalues i = 1(1) `mc' {
    if floor((`i'-1)/100) == ((`i'-1)/100) {
    noisily display "Working on `i' out of `mc' at $S_TIME"
    }

    preserve
    clear
    set obs 10
    g x2 = rnormal()
    g x3 = rnormal()
    g e = runiform()
    g y = 1 -3*x2 + 2*x3 + e
    reg y x2 x3
    local x2coeff = _b[x2]
    local x3coeff = _b[x3]
    local const = _b[_cons]
    restore
    replace data_store_x3 = `x3coeff' in `i'
    replace data_store_x2 = `x2coeff' in `i'
    replace data_store_con = `const' in `i'
    }
    }
    summ data_store_con data_store_x2 data_store_x3 data_store_y
    display e(rmse)
    predict res, resid

  • #2
    You can ignore the second comment, this one has no loop. Just want to know how to store my y's and residuals. thanks!!!!!!

    Comment


    • #3
      My main issue is how to save my y variable on a looped regression or otherwise how to get the ANOVA to print out over the 10 iterations.

      Comment


      • #4
        My main issue is how to save my y variable on a looped regression
        This is what's happening to your y variable
        Code:
        preserve // preserves a copy of the dataset
        clear // creates a new dataset
        set obs 10
        g x2 = rnormal()
        g x3 = rnormal()
        g e = runiform()
        g y = 1 -3*x2 + 2*x3 + e // creates the variable y in the new dataset
        reg y x2 x3
        local x2coeff = _b[x2]
        local x3coeff = _b[x3]
        local const = _b[_cons]
        restore // replaces the new dataset with the preserved dataset that does not have the variable y
        or otherwise how to get the ANOVA to print out over the 10 iterations
        Code:
        noisily reg y x2 x3 // undo the effect of the "queitly" block this is surrounded by

        Comment


        • #5
          It may be that you need to assign a different name to the y in each iteration of the loop so it doesn't overwrite.

          Comment

          Working...
          X