ARCHIVED: In Stata, how can I run a Monte Carlo (MC) simulation using postfile and forvalues do-looping commands?

This content has been archived, and is no longer maintained by Indiana University. Information here may no longer be accurate, and links may no longer be available or reliable.

A method of simulation in Stata is to use the postfile command with a do-looping command such as forvalues. Within each iteration of the loop, a post command is used to post (i.e., write) key results to a declared file in the postfile command.

The basic syntax and structure is:

        postfile postname newvarlist using filename [, replace]
        forvalues i=1/1000 {
                ...
               post postname (exp) (exp)...(exp)
                ...
        }
        postclose postname

Note that the postfile command declares the internal filename (postname) held in Stata memory, as well as variable names and an external filename (filename). In the post command line, each expression needs to be enclosed in parentheses. The posting of observations ends with the postclose command.

Following is a specific example of simulating a central limit theorem:

      set seed 10101 
      postfile sim_mem x_mean x_sd x_n using simresults, replace 
      forvalues i=1/1000 { 
           drop _all 
           set obs 30
           generate x= runiform() 
           quietly summarize x

           scalar x_mean=r(mean)
           scalar x_sd=r(sd)
           scalar x_n=r(N)

           post sim_mem (x_mean) (x_sd) (x_n)
     }
     postclose sim_mem

The above example generates 30 observations following a uniform distribution, simulates 1000 times, and creates new variables such as mean, standard deviation, and sample size. Here, postfile also declares the memory object where the results in Stata are stored (sim_mem), the variable list in the results dataset file (x_mean, x_sd, and x_n), and the name of that file (simresults). At each of the 1000 iterations in the forvalues loop, 30 samples following uniform distribution are created, and the sample mean, standard deviation, and total number are posted as new observations in the new variables (x_mean, x_sd, and x_n) in the data file (simresults.dta), which you can see in your folder.

Note: The postfile command simulation uses the quietly prefix to suppress the output within the forvalues loop. The simulate command (an alternative method) suppresses all output within the simulation automatically.

If you have questions about using statistical and mathematical software at Indiana University, contact the UITS Research Applications and Deep Learning team.

This is document bcjn in the Knowledge Base.
Last modified on 2023-05-09 14:40:42.