Announcement

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

  • Running regsave with append in loops overwrites observation numbers?

    Dear Statalist Members,

    I am trying to run a number of bivariate regressions using a loop and saving the results to a .dta dataset using regsave. I have a list of y variables and a list of x variables, and I need to regress each y var on each x var, and save the regression results into the same column. Here is the code I am running:

    Code:
    local yvar `" "yvar1" "yvar2" "yvar3" "yvar4" yvar5" "'
    
    local xvar `" "xvar1" "xvar2" "xvar3" "xvar4" "xvar5" "xvar6" "xvar7" "'
    
    foreach y of local yvar {
      local append replace
    
      foreach x of local xvar {
        qui reg `y' `x'
        regsave using `y'regs, `append' table(`y', format(%7.2f) parentheses(stderr) asterisk())
        local append append
      }
    }
    My problem is, regsave gives me a "Warning: width of using dataset has not increased. Values may have been overwritten" after each output dataset is written. When I look at the dataset generated, the constant, constant coefficient, constant se, observation number N, and r2 from each regression have been overwritten and replaced with those from the last regression that was run. All the coefficients from the xvar and their se are correct. An example output dataset yvar1regs.dta for the regressions with yvar yvar1 looks like this:

    var yvar1
    xvar1_coef 0.15***
    xvar1_stderr (0.02)
    xvar2_coef 0.20***
    xvar2_stderr (0.01)
    xvar3_coef 0.25***
    xvar3_stderr (0.05)
    xvar4_coef 0.30***
    xvar4_stderr (0.05)
    ... ...
    _cons_coef 0.33***
    _cons_stderr (0.01)
    N 1000
    r 0.21

    What I want is this:

    var yvar1
    xvar1_coef 0.15***
    xvar1_stderr (0.02)
    _cons_coef 0.22***
    _cons_stderr (0.03)
    N 1300
    r2 0.13
    ... ...
    xvar7_coef 0.20***
    xvar7_stderr (0.01)
    _cons_coef 0.33***
    _cons_stderr (0.01)
    N 1000
    r 0.21



    I have looked at the regsave help file and it does not mention how I might solve this problem, nor did I encounter any posts from Statalist that could shed light on my conundrum. I would greatly appreciate your help. Thank you very much!

  • #2
    The problem is that you are merging together results from two different regressions into a single column, and some of the variables (eg N, r2, _cons) have the same names. One solution is to manually append the saved results from the different regressions. The following code provides an example.

    Code:
    sysuse auto, clear
    
    tempfile t
    preserve
     
    local yvar `" "price" "'
    local xvar `" "trunk" "weight" "'
    
    foreach y of local yvar {
      
      local run_no = 0
      foreach x of local xvar {
          
        qui reg `y' `x'
        regsave, table(`y', format(%7.2f) parentheses(stderr) asterisk())
        
        if `run_no'==1 append using "`t'"
        save "`t'", replace
        local run_no = 1
        restore, preserve
      }
    }
    
    restore, not
    use "`t'", clear
    list
    Associate Professor of Finance and Economics
    University of Illinois
    www.julianreif.com

    Comment

    Working...
    X