Announcement

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

  • Generating variables containing confidence intervals

    Hi all

    I want to create a variable containing the lower confidence interval and another with the upper confidence interval at 90% level from regression estimated in a loop.

    gen coef = .
    gen ci_lower = .
    gen ci_upper = .

    forv k = 1/10 {
    capture drop keep
    g keep = 0
    replace keep = 1 if cycle==`k'

    xi: ivreg2 dep indep i.day_of_week*i.week if keep, robust bw(auto) small
    qui replace coef = _b[indep] if cycle==`k'

    * Calculate confidence intervals (90%)
    scalar z = invnormal(1 - (0.1 / 2))

    scalar ci_lower_val = _b[indep] - z * _se[indep]
    scalar ci_upper_val = _b[indep] + z * _se[indep]

    * Store confidence intervals
    replace ci_lower = ci_lower_val if cycle == `k'
    replace ci_upper = ci_upper_val if cycle == `k'
    }

    This was a questions that came from a previous post and as it is unrelated to the main objective of the former post. I make it as a new post here. Thanks.

  • #2
    You're saying this is a new problem, but it isn't. It's an attempt to reboot a thread that appears to have stalled.

    https://www.statalist.org/forums/for...hat-went-wrong

    Rebooting in itself is sometimes a good idea, but a cross-reference to the previous thread is then essential.

    Despite good-faith attempts to help by experienced Stata users, you don't appear to be making much progress. I can make some cosmetic suggestions, but can contribute almost nothing else beyond asking questions which may be picked up by others if you can answer them clearly.

    This code appears to be equivalent to

    Code:
    gen coef = .
    gen ci_lower = .
    gen ci_upper = .
    
    forv k = 1/10 {
    
    xi: ivreg2 dep indep i.day_of_week*i.week if cycle == `k', robust bw(auto) small
    qui replace coef = _b[indep] if cycle==`k'
    
    * Calculate confidence intervals (90%)
    replace ci_lower = _b[indep] - invnormal(0.95) * _se[indep] if cycle == `k'
    replace ci_upper = _b[indep] + invnormal(0.95) * _se[indep] if cycle == `k'
    
    }
    So, what's the problem?

    You appear to be trying to run an instrumental variables regression without specifying instrumental variables.

    Using xi: with ivreg2 is, I suspect, long since unnecessary unless you're using a very old version of Stata, in which case you should tell us.

    The regression is fitting a model with how many parameters? to how much data? If there is a problem with any cycle, the code will crash.

    Your confidence intervals are themselves crude approximations to what the regression command should provide any way.

    But you're trying to run 10 regressions. Just doing that by hand and compiling a table of results would have been quicker than trying over several days to get others to write your code.

    There is no data example here.

    I am not an economist, let alone an econometrician, so some of the comments above may be misreading your needs. That said, this is my first and last contribution to the thread, as I can't get into discussing or advising on what analysis might make sense here.

    I think you need to get support at your workplace, or hire a smart graduate student or research assistant to advise you on what you should be doing and how to do it in Stata. That might entail long conversations on where you are and where you want to be. Sorry that Statalist isn't working well for you. The ideal exchange is a crisp, specific question that allows a crisp, specific answer in a thread that converges quickly That seems to have been rare in your long years on Statalist.
    Last edited by Nick Cox; 20 May 2024, 03:34.

    Comment


    • #3
      The aim is to have the Newey West automatic bandwidth adjustment which as far as I know is not available elsewhere. At least, we can not do it with "newey". So I am not aiming to using ivreg2 for instrumental variables. In this case, the results are equivalent to newey if with lag being specified as -1 of the bw.
      I have more than 10 regressions, so using 1/10 was only to make things simple here.

      I understand that the confidence intervals are crude approximations and that is exactly why I was asking if I can extract those from the regression output directly inside the loop.

      Comment

      Working...
      X