Announcement

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

  • Regression loop and store specific coefficients

    I am going to do two things in STATA: (1) loop a regression over a certain criteria for many times; (2) store a certain coefficient from each regression results. I am giving an example of what I am doing below:

    Code:
    clear
    sysuse auto.dta
    local x = 2000
    while `x' < 5000{
    xi: regress price mpg length gear_ratio i.foreign if weight < `x'
    est sto model_`x'
    local x = `x' + 100
    }
    est dir
    As to all stored regression coefficients results, I actually just care about one of them, for example, mpg here. Ideally, I want STATA to extract coefficients of mpg from each result and compile them into one independent file(any file is OK, .dta would be great). By doing this, I want to see the trend of coefficient of `mpg` as `weight` increases. What I am doing right now is to use`estout` to export the results, something like:

    Code:
    esttab * using test.rtf, replace se stats(r2_a N, labels(R-squared)) starl(* 0.10 ** 0.05 *** 0.01) nogap onecell title(regression tables)
    `estout` will export everything and I need to edit them by myself. Technically, It works for regressions with smaller amount of independent variables, but my real working dataset has more than 30 variables and the regression will loop for at least 100 times ( I have a variable `Distance` which is in a range of (0 ~ 30,000) and I use it as `weight` as in the above example). Therefore, it is really difficult for me to edit the results by myself without making mistakes.

    I am wondering are there any other efficient ways to solve my problem? especially the second one. I definitely would like to know a silver bullet method (if there is one) to solve them both at one time. Since my case is not looping over a group variable, but over a certain criteria. The `statsby` function seems not working well here. Any comments or suggestions are greatly appreciated!
    Last edited by Chuan Tang; 23 Nov 2015, 18:32.

  • #2
    The code below adds two variables to the data: the variable x and the variable mpg_b with the regression coefficients for each value of x.
    Code:
    sysuse auto.dta, clear
    local x = 2000
    local i = 0
    while `x' < 5000 {
      xi: regress price mpg length gear_ratio i.foreign if weight < `x'
      matrix A = r(table)
      local i = `i' + 1
      local mpg_b`i' = A[1,1]
      local x`i' = `x'
      local x = `x' + 100
    }
    capture set obs `i'
    gen x = .
    gen mpg_b = .
    forval j = 1/`i' {
      replace x = `x`j'' in `j'
      replace mpg_b = `mpg_b`j'' in `j'
    }

    Comment


    • #3
      Cross-posted (and answered differently) at http://stackoverflow.com/questions/3...e-coefficients

      In this case the answers are perhaps usefully complementary, but we do have a cross-posting policy, which is that you should tell us about it.

      Comment


      • #4
        Originally posted by Friedrich Huebler View Post
        The code below adds two variables to the data: the variable x and the variable mpg_b with the regression coefficients for each value of x.
        Code:
        sysuse auto.dta, clear
        local x = 2000
        local i = 0
        while `x' < 5000 {
        xi: regress price mpg length gear_ratio i.foreign if weight < `x'
        matrix A = r(table)
        local i = `i' + 1
        local mpg_b`i' = A[1,1]
        local x`i' = `x'
        local x = `x' + 100
        }
        capture set obs `i'
        gen x = .
        gen mpg_b = .
        forval j = 1/`i' {
        replace x = `x`j'' in `j'
        replace mpg_b = `mpg_b`j'' in `j'
        }
        Thank you very much for this answer.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          Cross-posted (and answered differently) at http://stackoverflow.com/questions/3...e-coefficients

          In this case the answers are perhaps usefully complementary, but we do have a cross-posting policy, which is that you should tell us about it.
          I did not notice the cross-posting policy and I am sorry for that. I will remember to mention it if it happens again. Thank you, Nick

          Comment


          • #6
            Thanks for wrapping this up.

            Comment


            • #7

              In the below code, how did j = 1/`i' become a loop instead of just one number? forval j = 1/`i' { replace x = `x`j'' in `j' replace mpg_b = `mpg_b`j'' in `j' }

              Comment

              Working...
              X