Announcement

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

  • Report the sample mean and sample variance of the 5000 sample means

    Dear Sir or Madam,
    I use monte carlo with stata to generate samples of two variables y and z respectively. I need to report the sample mean and sample variance of the 500 sample means. I am sure my process of generate the two variables y and z is correct. But when I use postfile command and simulate command to repeat it 500 times to report the sample mean and sample variance of the 500 sample means, neither work.
    Here is my codes with postfile command:
    set seed 123
    postfile dm yhat zhat using result, replace
    forvalues i=1/500{
    clear
    mat sigma=[1, 0.2\0.2, 1]
    mat m = (0, 0)
    drawnorm u v, n(75) means(m) cov(sigma)
    generate y=.
    generate t=_n
    qui tsset t
    replace y=u if t==1
    replace y=0.23*l.y+u if t>1
    mean y
    post dm (_b[y])
    generate z=.
    replace z=v if t==1
    replace z=0.23*l.z+v if t>1
    mean z
    post dm (_c[z])
    }
    postclose dm
    use result, clear
    sum, detail


    Here is my code with simulate command:
    program dm, rclass
    drop all
    set seed 123
    mat sigma=[1, 0.2\0.2, 1]
    mat m = (0, 0)
    drawnorm u v, n(75) means(m) cov(sigma)
    generate y=.
    generate t=_n
    qui tsset t
    replace y=u if t==1
    replace y=0.23*l.y+u if t>1
    sum y
    return scalar yhat=r(mean)
    generate z=.
    replace z=v if t==1
    replace z=0.23*l.z+v if t>1
    sum z
    return scalar zhat=r(mean)
    end
    clear
    set seed 123
    simulate yhat=r(mean) zhat=r(mean), reps(500) nodots: dm
    describe *
    sum, detail

    Could you help me with either of the programs?
    Thank you very much for your help!
    Best,
    Cherry

  • #2
    Welcome to Statalist.

    Your problem with your first example is that your postfile command
    Code:
    postfile dm yhat zhat using result, replace
    sets up a file with two variables, yhat and zhat, but your post commands
    Code:
    post dm (_b[y])
    post dm (_c[z])
    each specify only one value to post. Also, the second post command tries to post _c[z] which does not exist, presumably you mean _b[z]. Here is code that my come closer to accomplishing what you want.
    Code:
    set seed 123
    postfile dm yhat zhat using result, replace
    forvalues i=1/500{
    clear
    mat sigma=[1, 0.2\0.2, 1]
    mat m = (0, 0)
    drawnorm u v, n(75) means(m) cov(sigma)
    generate y=.
    generate t=_n
    qui tsset t
    replace y=u if t==1
    replace y=0.23*l.y+u if t>1
    mean y
    local mean_y = _b[y]
    generate z=.
    replace z=v if t==1
    replace z=0.23*l.z+v if t>1
    mean z
    local mean_z = _b[z]
    post dm (`mean_y') (`mean_z')
    }
    postclose dm
    use result, clear
    sum, detail
    With that said, to improve your future posts, please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question.

    The more you help others understand your problem, the more likely others are to be able to help you solve your problem.
    Last edited by William Lisowski; 17 Feb 2018, 11:38.

    Comment


    • #3
      The immediate problem with your first example is that the command
      Code:
      drop all
      tried to find a variable named all to drop; the correct syntax would have been
      Code:
      drop _all
      But there were other problems as well. Here is code that may come closer to accomplishing what you want.
      Code:
      capture program drop dm
      program dm, rclass
      clear 
      // set seed 123 // NO DO NOT RESET THE SEED FOR EACH SIMULATION
      mat sigma=[1, 0.2\0.2, 1]
      mat m = (0, 0)
      drawnorm u v, n(75) means(m) cov(sigma)
      generate y=.
      generate t=_n
      qui tsset t
      replace y=u if t==1
      replace y=0.23*l.y+u if t>1
      sum y
      return scalar yhat=r(mean)
      generate z=.
      replace z=v if t==1
      replace z=0.23*l.z+v if t>1
      sum z
      return scalar zhat=r(mean)
      end
      clear
      set seed 123
      simulate yhat=r(yhat) zhat=r(zhat), reps(500) nodots: dm
      describe *
      sum, detail

      Comment


      • #4
        Thank you very much for your help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

        Comment

        Working...
        X