Announcement

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

  • Tabstat output + simulation

    Good afternoon Statlist members,

    I've been struggling for the past 3 hours with the following problem. I hope you guys have some good advice!


    I have to following code:

    gen return = rnormal(0.0050, sqrt(0.0017))
    gen sigma = return^2
    tabstat return sigma, stat(me v) save

    Now I would like to do a simple simulation process where I store each time the mean and variance of both variables return and sigma.

    program define myreg, rclass
    drop_all
    set obs 100
    gen return =rnormal(0.0050, sqrt(0.0017))
    gen sigma =return^2
    tabstat return sigma, stat(me v)
    ..............
    .............
    ............ Some code here to store the mean and variance of both return and sigma variable.

    end

    simulate mean_return = ...... var_return =...... mean_sigma=....... var_sigma=..... ,reps(100) :myreg

    I've tried tabstatmat, return scalars, estpost but nothing seems to work.

    Thank you for help!


  • #2
    A couple of suggestions.

    1. Don't use return as a variable name, since it is also a command name. You probably won't get in trouble with it here, but it could lead to confusion., and in some uses might cause syntax errors.

    2. -tabstat- returns matrices containing your statistics. And it only does so if you specify the -save- option (which you do in your first block of code but not in the second). You then would have to extract those values from the matrices and -return- those within your program. That's probably more trouble than it's worth given that you only have two variables. -summarize- makes it simpler by returning the summary statistics directly as scalars.

    So:
    Code:
    program define myreg, rclass
    drop _all
    set obs 100
    gen ret = rnormal(0.0050, sqrt(0.0017))
    gen sigma = ret^2
    summ ret, detail
    return scalar mean_return = `r(mean)'
    return scalar var_return = `r(Var)'
    summ sigma, detail
    return scalar mean_sigma = `r(mean)'
    return scalar var_sigma = `r(Var)'
    // etc.
    ...
    
    simulate mean_return =r(mean_return)  var_return = r(var_return)  mean_sigma= r(mean_sigma)  var_sigma= r(var_sigma) ,reps(100) :myreg
    should do it.

    Comment


    • #3
      Clyde gives excellent advice. A small qualification is that I would omit the local macro-like punctuation so that

      Code:
       
      `r(mean)' 
      `r(Var)'
      just become, throughout,

      Code:
       
      r(mean)
      r(Var)
      The precision difference will usually be negligible, although occasionally there might be puzzles over reproducibility.

      This is more aesthetic than anything else, although conceptually the idea of pushing a scalar into its local macro persona into a scalar is surely a little easier to think about if we omit the middle macro phase, as it were.


      Comment


      • #4
        I quite agree!

        Comment


        • #5
          Thank your very much Clyde, it worked perfectly! Wish I posted it sooner instead of frustrating those hours. And Nick thank you for your suggestion/explanation, made it easier to understand the local macro.
          Thanks again for helping me out so fast!

          Comment

          Working...
          X