# Stata：蒙特卡洛模拟A-(Monte-Carlo-Simulation)没那么神秘

Stata 连享会   主页 || 视频 || 推文

## 2. 第一个 MCS

### 2.1 示例 1：模拟数据的平均值

``````. drop _all
. set obs 500
. set seed 12345
. generate y = rchi2(1)
. mean y

Mean estimation        Number of obs =    500

---------------------------------------------
|   Mean   Std. Err.  [95% Conf. Interval]
---+-----------------------------------------
y |  0.911      0.055      0.803       1.019
---------------------------------------------
``````

1. 从 DGP 中抽取
2. 计算估算值
3. 存储估算值

### 2.2 示例 2：三次抽奖的估计平均值

``````set seed 12345
postfile buffer mhat using mcs, replace

forvalues i=1/3 {
quietly drop _all
quietly set obs 500
quietly generate y = rchi2(1)
quietly mean y
post buffer (_b[y])
}

postclose buffer

use mcs, clear

. list

+----------+
|     mhat |
|----------|
1. | .9107645 |
2. |  1.03821 |
3. | 1.039254 |
+----------+
``````

``````forvalues i=1/3 {
}
``````

``````quietly drop _all
quietly set obs 500
quietly generate y = rchi2(1)
quietly mean y
``````

### 2.3 示例 3：2000 个估计平均值的均值

``````set seed 12345
postfile buffer mhat using mcs, replace

forvalues i=1/2000 {
quietly drop _all
quietly set obs 500
quietly generate y = rchi2(1)
quietly mean y
post buffer (_b[y])
}
postclose buffer
use mcs, clear
summarize
``````

``````. summarize

Variable |   Obs     Mean    Std. Dev.       Min        Max
----------+-------------------------------------------------
mhat | 2,000  1.00017    .0625367   .7792076    1.22256
``````

## 3. 包含标准误

### 3.1 示例 4：2000个标准误差的平均值

``````set seed 12345
postfile buffer mhat sehat using mcs, replace

forvalues i=1/2000 {
quietly drop _all
quietly set obs 500
quietly generate y = rchi2(1)
quietly mean y
post buffer (_b[y]) (_se[y])
}
postclose buffer
use mcs, clear
summarize
``````

``````. summarize

Variable |    Obs        Mean    Std. Dev.       Min        Max
----------+-----------------------------------------------------
mhat |  2,000     1.00017    .0625367   .7792076    1.22256
sehat |  2,000    .0629644    .0051703   .0464698   .0819693
``````

## 4. 估计拒绝率

### 示例 5：估计拒绝率

``````set seed 12345
postfile buffer mhat sehat reject using mcs, replace

forvalues i=1/2000 {
quietly drop _all
quietly set obs 500
quietly generate y = rchi2(1)
quietly mean y
quietly test _b[y]=1
local r = (r(p)<.05)
post buffer (_b[y]) (_se[y]) (`r')
}

postclose buffer
use mcs, clear
summarize
``````

``````. summarize

Variable |    Obs        Mean    Std. Dev.       Min        Max
----------+-----------------------------------------------------
mhat |  2,000     1.00017    .0625367   .7792076    1.22256
sehat |  2,000    .0629644    .0051703   .0464698   .0819693
reject |  2,000       .0475     .212759          0          1
``````

## 6. 附录

### 6.1 附录 I：同一过程的多次重复

``````左边 `    右边 '  合起来 `value'
``````
``````local value "hello"
display "`value'"

local value "2.134"
display "`value'"
``````

``````forvalues i=1/3 {
display "i is now `i'"
}
``````

``````. forvalues i=1/3 {
2.    display "i is now `i'"
3. }
i is now 1
i is now 2
i is now 3
``````

### 6.2 附录II：获取估计结果

#### 示例 6：访问估计值

``````drop _all
set obs 500
set seed 12345
generate y = rchi2(1)
mean y
display  _b[y]
display _se[y]
``````

``````. mean y

Mean estimation            Number of obs   =      500

-----------------------------------------------------
|       Mean   Std. Err.     [95% Conf. Interval]
----+------------------------------------------------
y |      0.911      0.055         0.803       1.019
-----------------------------------------------------

. display  _b[y]
.91076444

. display _se[y]
.05486467
``````

### 6.3 附录Ⅲ：通过检验计算获取 p 值

#### 示例 7：Wald 测试结果

``````drop _all
set obs 500
set seed 12345
generate y = rchi2(1)
mean y
test _b[y]=1
``````

``````. mean y

Mean estimation              Number of obs   =    500

-----------------------------------------------------
|       Mean   Std. Err.     [95% Conf. Interval]
----+------------------------------------------------
y |      0.911      0.055         0.803       1.019
-----------------------------------------------------

. test _b[y]=1

( 1)  y = 1

F(  1,   499) =    2.65
Prob > F =    0.1045
``````

#### 示例 8：通过测试存储的结果

``````. return list

scalars:
r(drop) =  0
r(df_r) =  499
r(F) =  2.645393542355073
r(df) =  1
r(p) =  .1044817316846186

``````

``````local r = (r(p)<.05)
display "`r'"
``````

## 7. 代码汇总

``````drop _all
set obs 500
set seed 12345
generate y = rchi2(1)
mean y

set seed 12345
postfile buffer mhat using mcs, replace

forvalues i=1/3 {
quietly drop _all
quietly set obs 500
quietly generate y = rchi2(1)
quietly mean y
post buffer (_b[y])
}

postclose buffer

use mcs, clear
list

set seed 12345
postfile buffer mhat using mcs, replace

forvalues i=1/2000 {
quietly drop _all
quietly set obs 500
quietly generate y = rchi2(1)
quietly mean y
post buffer (_b[y])
}
postclose buffer
use mcs, clear
summarize

set seed 12345
postfile buffer mhat sehat using mcs, replace

forvalues i=1/2000 {
quietly drop _all
quietly set obs 500
quietly generate y = rchi2(1)
quietly mean y
post buffer (_b[y]) (_se[y])
}
postclose buffer
use mcs, clear
summarize

set seed 12345
postfile buffer mhat sehat reject using mcs, replace

forvalues i=1/2000 {
quietly drop _all
quietly set obs 500
quietly generate y = rchi2(1)
quietly mean y
quietly test _b[y]=1
local r = (r(p)<.05)
post buffer (_b[y]) (_se[y]) (`r')
}

postclose buffer
use mcs, clear
summarize

qui local value "hello"
display "`value'"

qui local value "2.134"
display "`value'"

forvalues i=1/3 {
display "i is now `i'"
}

drop _all
set obs 500
set seed 12345
generate y = rchi2(1)
mean y
display  _b[y]
display _se[y]

drop _all
set obs 500
set seed 12345
generate y = rchi2(1)
mean y
test _b[y]=1

return list

local r = (r(p)<.05)
display "`r'"
``````

## 相关课程

http://lianxh.duanshu.com

### 课程一览

Stata数据清洗 游万海 直播, 2 小时，已上线

Note: 部分课程的资料，PPT 等可以前往 连享会-直播课 主页查看，下载。

#### 关于我们

• Stata连享会 由中山大学连玉君老师团队创办，定期分享实证分析经验。直播间 有很多视频课程，可以随时观看。
• 连享会-主页知乎专栏，300+ 推文，实证分析不再抓狂。
• 公众号推文分类： 计量专题 | 分类推文 | 资源工具。推文分成 内生性 | 空间计量 | 时序面板 | 结果输出 | 交乘调节 五类，主流方法介绍一目了然：DID, RDD, IV, GMM, FE, Probit 等。
• 公众号关键词搜索/回复 功能已经上线。大家可以在公众号左下角点击键盘图标，输入简要关键词，以便快速呈现历史推文，获取工具软件和数据下载。常见关键词：`课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法`

✏ 连享会学习群-常见问题解答汇总：
https://gitee.com/arlionn/WD