Announcement

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

  • simulate

    Hi stata users.
    I am having problem with simulate command. I have generated a dataset that has four different size ( n=200,300,400 & 500) after manipulating the data then I run the simulate command but I get this error
    option n() incorrectly specified”. My code is below, essentially I would like to simulate m1 and m2 for the different sample sizes. Any advice greatly appreciated.
    Many thanks
    * Run program
    clear *
    set seed 1
    capture program drop simanal
    for val i=200(100)500{
    set obs =`i'
    simanal , n(`obs')
    return list
    * Run full simulation study
    simulate n=r(n) mean_1=r(mean_1) mse_1=r(mse_1) mdf_1=r(mdf_1) ///
    mean_2=r(mean_2) mse_2=r(mse_2) mdf_2=r(mdf_2)
    reps(1000) seed(93334) saving(output, replace):simanal, n(`obs')
    }

  • #2
    Welcome to Statalist.

    You have accidentally posted your topic in Statalist's Mata Forum, which is used for discussions of Stata's Mata language. Your question will see a much larger audience if you post it in Statalist's General Forum.

    Also, if you have not already done so, take a look at the Statalist FAQ linked to at the top of this page for posting guidelines and suggestions.

    Comment


    • #3
      Welcome to Statalist. As William mentions, it will be better to post this kind of question in the main forum. While you're doing that, here are some pointers to help you with your problem:

      First

      The code you show isn't what you were actually trying to use.

      Hints:
      • for val
      • set obs =
      • mdf_2=r(mdf_2) reps(1000)

      You'll get better answers if you "Say exactly what you typed . . . N.B. exactly!.

      Second

      There's no macro obs created when you set obs.

      Substitute `i' or even `=_N' for your `obs'.

      Third

      The first pass through your simulation will leave you with a thousand observations, and at the beginning of the second pass, set obs will be wanting to set it to three hundred.

      You'll need a drop _all somewhere in your loop, preferably at the top just before the set obs line.

      Last

      Set the seed once, and set it at the top. Do not reset the seed to the same value for each simulation pass through the loop, unless there is some special reason to do so.

      This runs:
      Code:
      version 15.1
      
      clear *
      
      program define simanal, rclass
          version 15.1
          syntax , n(integer)
      
          foreach junk in mean mse mdf {
              forvalues i = 1/2 {
                  return scalar `junk'_`i' = 0
              }
          }
          return scalar n = `n'
      end
      
      set seed `=strreverse("1463750")'
      
      forvalues i = 200(100)500{
      
          drop _all
          quietly set obs `i'
      
          simanal , n(`i')
          return list
          
          * Run full simulation study
          
          simulate n=r(n) mean_1=r(mean_1) mse_1=r(mse_1) mdf_1=r(mdf_1) ///
              mean_2=r(mean_2) mse_2=r(mse_2) mdf_2=r(mdf_2), ///
              reps(1000) nodots /* seed(93334) saving(output, replace) */: simanal, n(`i')
      }
      
      exit

      Comment


      • #4
        Thank you William and Joseph.
        Sorry I didn’t realise it was under mata!. I will repost in the general discussion and show my steps as Joseph suggested, very much appreciated.
        While reading the FAQ, I come across this command multishell , which I will read about.
        Thank you

        Comment

        Working...
        X