# Stata：嵌套 Logit 模型 (Nested Logit)

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

E-Mail: philoyl@163.com

## 2. Nested Logit 模型的树状结构和模型设定

Nested Logit 模型假设扰动项 $\left({\epsilon }_{j1},\dots ,{\epsilon }_{jK}\right)$ 服从于 Gumbel 多元极值分布 (Gumbel's multivariate extrem-value distribution)。可证明 ($j,k$) 选择枝被选择的概率等于：

## 3. Stata 实操：nlogit 命令

### 3.1 定义树状嵌套结构

``````nlogitgen newaltvar = altvar (branchlist)
``````

``````branch, branch [, branch ...]
``````

``````[label:] alternative [| alternative [| alternative ...]]
``````

### 3.2 查看定义的树状嵌套结构

``````nlogittree altvarlist [if] [in] [weight] [,options]
``````

### 3.3 Nested Logit 模型及估计

``````nlogit depvar [indepvars] [if] [in] [weight] [|| lev1_equation [|| lev2_equation ...]] || altvar: [byaltvarlist], case(varname) [options]
``````

``````altvar: [byaltvarlist] [, base(#|lbl) estconst]
``````

## 4. Stata 实操：应用范例

### 4.1 模型设定与估计

``````use "travel2.dta", clear
des2  //查看变量的基本情况
bysort mode choice: sum time invc ttme invt gc hinc psize  //分交通方式统计变量的基本统计量
``````

``````*-生成 type 变量表示树状嵌套结构
nlogitgen type = mode(Public: Train|Bus, Private: Car)

new variable type is generated with 2 groups
label list lb_type
lb_type:
1 Public
2 Private

*-查看定义的嵌套结构
*-choice选项表示列出choice变量的频数，即每个选择枝的样本频数
*-变量顺序为：先列出最底层中表示选择枝的变量，再列出上一层中表示分组的变量
nlogittree mode type, choice(choice)

tree structure specified for the nested logit model

type     N      mode   N   k
-------------------------------
Public  304 --- Train 152  63
+- Bus   152  30
Private 152 --- Car   152  59
-------------------------------
total  456 152

k = number of times alternative is chosen
N = number of observations at each level
``````

``````.   nlogit choice time invc || type: psize, base(Private) || mode: hinc, base(Car) case(id) notree

RUM-consistent nested logit regression         Number of obs      =        456
Case variable: id                              Number of cases    =        152

Alternative variable: mode                     Alts per case: min =          3
avg =        3.0
max =          3

Wald chi2(5)    =      40.43
Log likelihood = -71.596852                       Prob > chi2     =     0.0000

------------------------------------------------------------------------------
choice |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
mode         |
time |     -0.013      0.003    -5.22   0.000       -0.018      -0.008
invc |     -0.024      0.009    -2.59   0.010       -0.042      -0.006
------------------------------------------------------------------------------
type equations
------------------------------------------------------------------------------
Public       |
psize |     -0.231      0.248    -0.93   0.350       -0.717       0.254
-------------+----------------------------------------------------------------
Private      |
psize |      0.000  (base)
------------------------------------------------------------------------------
mode equations
------------------------------------------------------------------------------
Train        |
hinc |     -0.036      0.014    -2.58   0.010       -0.063      -0.009
_cons |      3.301      0.649     5.08   0.000        2.029       4.574
-------------+----------------------------------------------------------------
Bus          |
hinc |     -0.019      0.015    -1.28   0.202       -0.049       0.010
_cons |      2.431      0.658     3.69   0.000        1.141       3.722
-------------+----------------------------------------------------------------
Car          |
hinc |      0.000  (base)
_cons |      0.000  (base)
------------------------------------------------------------------------------
dissimilarity parameters
------------------------------------------------------------------------------
type         |
/Public_tau |      0.327      0.106                         0.120       0.534
/Private_tau |      1.000   1.74e+05                     -3.40e+05    3.40e+05
------------------------------------------------------------------------------
LR test for IIA (tau=1): chi2(2) = 13.95                  Prob > chi2 = 0.0009
``````

### 4.2 计算预测概率

``````.   predict plevel1 plevel2, pr

.   list id mode type plevel1 plevel2 in 1/9, sepby(id)

+----------------------------------------------+
| id    mode      type     plevel1     plevel2 |
|----------------------------------------------|
1. |  1   Train    Public   .16579709   .15071222 |
2. |  1     Bus    Public   .16579709   .01508488 |
3. |  1     Car   Private   .83420291   .83420291 |
|----------------------------------------------|
4. |  2   Train    Public   .36135831   .34204217 |
5. |  2     Bus    Public   .36135831   .01931614 |
6. |  2     Car   Private   .63864169   .63864169 |
|----------------------------------------------|
7. |  3   Train    Public   .13511207   .00634726 |
8. |  3     Bus    Public   .13511207   .12876482 |
9. |  3     Car   Private   .86488793   .86488793 |
+----------------------------------------------+

*-对level 1 层的预测概率进行分类统计
.   bysort type: sum plevel1

*-对level 2 层的预测概率进行分类统计
.   bysort type mode: sum plevel2
``````

## 参考资料

• 陈强. 高级计量经济学及Stata应用[M]. 高等教育出版社, 2014. [-Link-]
• Cameron, A., P. Trivedi. Microeconometrics using stata[M]. Stata Press, 2009. [-Link-]

## 相关课程

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