# Stata：正确理解置信区间

Stata连享会   主页 || 视频 || 推文 || 知乎 || Bilibili 站

New！ `lianxh` 命令发布了：

`. ssc install lianxh`

`. help lianxh`

⛳ Stata 系列推文：

Source：Bevans, R. (July 9, 2022). Understanding Confidence Intervals | Easy Examples & Formulas. Scribbr. Retrieved October 5, 2022. -Link-

## 1. 何谓置信区间

• 比例
• 总体平均值
• 总体平均值和比例之间的区别
• 组间差异

## 2. 计算置信区间

$z$ 统计量 1.64 1.96 2.57

### 2.1 正态分布均值的置信区间

• 美国：$CI=35±1.96\frac{5}{\sqrt{1}00}=35±0.98$，对于美国来说，95% 置信区间的下限和上限分别为 34.02 和 35.98；
• 英国：$CI=35±1.96\frac{10}{\sqrt{1}00}=35±1.96$，对于英国来说，95% 置信区间的下限和上限分别为 33.04 和 36.96。

## 4. Stata实例

### 4.1 计算置信区间

``````. sysuse auto, clear
. des

Observations:            74            1978 automobile data
Variables:            12            13 Apr 2020 17:45
(_dta has notes)
-------------------------------------------------------------
Variable    Storage   Display    Value
name       type    format    label  Variable label
-------------------------------------------------------------
make          str18   %-18s             Make and model
price         int     %8.0gc            Price
mpg           int     %8.0g             Mileage (mpg)
rep78         int     %8.0g             Repair record 1978
trunk         int     %8.0g             Trunk space (cu. ft.)
weight        int     %8.0gc            Weight (lbs.)
length        int     %8.0g             Length (in.)
turn          int     %8.0g             Turn circle (ft.)
displacement  int     %8.0g             Displacement (cu. in.)
gear_ratio    float   %6.2f             Gear ratio
foreign       byte    %8.0g    origin   Car origin
-------------------------------------------------------------
Sorted by: foreign
``````

``````. ci means price, level(95)

Variable |   Obs      Mean   Std. err.    [95% conf. interval]
-------------+------------------------------------------------------
price |    74  6165.257   342.8719     5481.914      6848.6

. ci proportions foreign
Binomial exact
Variable |   Obs  Proportion   Std. err.   [95% conf. interval]
-------------+------------------------------------------------------
foreign |    74    .2972973   .0531331    .196584    .4148353
``````

``````. reg price weight length foreign, level(95)

Source |       SS           df       MS      Number of obs   =        74
-------------+----------------------------------   F(3, 70)        =     28.39
Model |   348565467         3   116188489   Prob > F        =    0.0000
Residual |   286499930        70  4092856.14   R-squared       =    0.5489
-------------+----------------------------------   Adj R-squared   =    0.5295
Total |   635065396        73  8699525.97   Root MSE        =    2023.1
------------------------------------------------------------------------------
price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
weight |      5.775      0.959     6.02   0.000        3.861       7.688
length |    -91.371     32.828    -2.78   0.007     -156.845     -25.897
foreign |   3573.092    639.328     5.59   0.000     2297.992    4848.191
_cons |   4838.021   3742.010     1.29   0.200    -2625.183   12301.224
------------------------------------------------------------------------------
``````

### 4.2 画置信区间

``````. reg price weight length foreign rep78 headroom trunk turn displacement gear_ratio

Source |       SS           df       MS      Number of obs   =        69
-------------+----------------------------------   F(9, 59)        =      9.76
Model |   345099215         9  38344357.2   Prob > F        =    0.0000
Residual |   231697744        59  3927080.41   R-squared       =    0.5983
-------------+----------------------------------   Adj R-squared   =    0.5370
Total |   576796959        68  8482308.22   Root MSE        =    1981.7
------------------------------------------------------------------------------
price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
weight |      4.588      1.381     3.32   0.002        1.826       7.351
length |    -74.399     39.404    -1.89   0.064     -153.246       4.449
foreign |   3427.889    891.474     3.85   0.000     1644.052    5211.725
rep78 |    159.006    316.432     0.50   0.617     -474.172     792.185
headroom |   -636.864    379.994    -1.68   0.099    -1397.229     123.502
trunk |     72.736     94.205     0.77   0.443     -115.768     261.240
turn |   -110.877    121.984    -0.91   0.367     -354.966     133.212
displacement |     11.303      8.271     1.37   0.177       -5.247      27.852
gear_ratio |   -391.968   1085.269    -0.36   0.719    -2563.586    1779.651
_cons |   8905.397   5885.535     1.51   0.136    -2871.531   20682.325
------------------------------------------------------------------------------
``````

``````. coefplot, yti("变量") xti("系数估计值 + 95% 置信区间") xline(0)
``````

## 6. 相关推文

Note：产生如下推文列表的 Stata 命令为：
`lianxh 统计 置信区间, m`

`ssc install lianxh, replace`

## 相关课程

### 最新课程-直播课

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

### 关于我们

• Stata连享会 由中山大学连玉君老师团队创办，定期分享实证分析经验。
• 连享会-主页知乎专栏，700+ 推文，实证分析不再抓狂。直播间 有很多视频课程，可以随时观看。
• 公众号关键词搜索/回复 功能已经上线。大家可以在公众号左下角点击键盘图标，输入简要关键词，以便快速呈现历史推文，获取工具软件和数据下载。常见关键词：`课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法`

✏ 连享会-常见问题解答：
https://gitee.com/lianxh/Course/wikis

New！ `lianxh``songbl` 命令发布了：

`. ssc install lianxh`

`. help lianxh`