jungsee的个人博客分享 http://blog.sciencenet.cn/u/jungsee

博文

postfile command

已有 2604 次阅读 2015-2-15 11:52 |个人分类:STATA|系统分类:教学心得

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

A method of simulation in Stata is touse the postfile command with a do-looping command suchas forvalues. Within each iteration of the loop, a post command is used to post (i.e., write) key results to adeclared 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 Statamemory, as well as variable names and an external filename (filename). In thepost command line, each expression needs to be enclosed inparentheses. The posting of observations ends with the postclose command.

Following is a specific example ofsimulating 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 30observations following a uniform distribution, simulates 1000 times, andcreates new variables such as mean, standard deviation, and sample size. Here, postfile also declares the memory object where the results inStata are stored (sim_mem), the variable list in the resultsdataset file (x_mean, x_sd, and x_n), and the name of that file (simresults). At each ofthe 1000 iterations in theforvalues loop, 30 samples following uniformdistribution are created, and the sample mean, standard deviation, and totalnumber 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 usingstatistical and mathematical software at Indiana University, email UITS Research Analytics. Research Analytics is locatedon the IU Bloomington campus at Woodburn Hall 200, and is open for consultationby appointment Monday-Friday 9am-5pm. For more, see Research Analytics, or call 812-855-4724 (IUB) or 317-278-4740(IUPUI).

This is document bcjn in theKnowledge Base. 
Last modified on 2014-06-25.

 




https://wap.sciencenet.cn/blog-793574-868185.html

上一篇:文献分享
下一篇:Programming Advice
收藏 IP: 111.203.16.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-5-29 08:52

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部