# Stata：如何同时对多个数据框操作-frame

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

New！ `lianxh` 命令发布了：

`. ssc install lianxh`

`. help lianxh`

⛳ Stata 系列推文：

• 同时使用多个数据集；
• frames 与后续估计；
• frames 与 `frval` 的连接；
• frames 与 `frget` 的连接；
• 多个 frames。

## 1. 同时使用多个数据集

``````. frame create patients1
. frame patients1: use patients1
``````

``````.  frame create patients2
.  frame patients2: use patients2
``````

``````. frame dir
default    0 x 0
patients1  1772 x 14; patients1.dta
patients2  1024 x 14; patients2.dta
``````

``````. frame patients1: regress sbp c.age##c.bmi
. estimates store patients1
. frame patients2: regress sbp c.age##c.bmi
. estimates store patients2
``````

``````. estimates table patients1 patients2

----------------------------------------
Variable | patients1    patients2
-------------+--------------------------
age |   .9977217    .97277459
bmi |  1.8253391    2.1873918
|
c.age#c.bmi | -.01331291   -.01813435
|
_cons |  51.727175    48.077782
----------------------------------------
``````

## 2. frames 与后续估计

``````. frame change patients1
. regress sbp c.age##c.bmi

Source |       SS           df       MS      Number of obs   =     1,764
-------------+----------------------------------   F(3, 1760)      =     10.91
Model |  66463.5776         3  22154.5259   Prob > F        =    0.0000
Residual |  3574716.18     1,760  2031.08874   R-squared       =    0.0183
Total |  3641179.76     1,763  2065.33169   Root MSE        =    45.068
------------------------------------------------------------------------------
sbp |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
age |   .9977217   .9175058     1.09   0.277    -.8017941    2.797237
bmi |   1.825339   2.094022     0.87   0.383    -2.281694    5.932372
c.age#c.bmi |  -.0133129   .0383641    -0.35   0.729    -.0885568     .061931
_cons |   51.72718   50.30771     1.03   0.304    -46.94198    150.3963
------------------------------------------------------------------------------

. margins, at(age=(30(10)90) bmi=(10(5)40)) saving(predictions, replace)
``````

``````. frame create contour
. frame change contour
. use predictions
``````

``````. describe _at1 _at2 _margin

storage   display    value
variable name   type    format     label      variable label
-------------------------------------------------------------------------------
_at1            byte    %9.0g                 age
_at2            byte    %9.0g                 bmi
_margin         float   %9.0g                 Linear prediction, predict()

. list _at1 _at2 _margin in 1/5

+------------------------+
| _at1   _at2    _margin |
|------------------------|
1. |   30     10   95.91834 |
2. |   30     15   103.0481 |
3. |   30     20   110.1779 |
4. |   30     25   117.3076 |
5. |   30     30   124.4374 |
+------------------------+
``````

``````. rename _at1 age
. rename _at2 bmi
. rename _margin pr_sbp

. twoway (contour pr_sbp bmi age, ccuts(90(10)170)),  ///
>        xlabel(30(10)90)  ylabel(10(5)40, angle(horizontal)) ///
>        xtitle("Age (years)") ///
>        ytitle("BMI")         ///
>        ztitle("Predicted Systolic Blood Pressure") ///
>        title("Predicted SBP by Age and BMI")
``````

## 3. frames 与 frval 的连接

``````. frame create community
. frame change community
. use community
. list cid cominc in 1/5, abbrev(10)

+---------------+
| cid    cominc |
|---------------|
1. |   1   1.0e+04 |
2. |   2   5.0e+04 |
3. |   3   5.0e+04 |
4. |   4   1.2e+04 |
5. |   5   9.0e+03 |
+---------------+
``````

``````. frame change patients1
. list pid cid perinc in 1/5, abbrev(10)

+---------------------+
| pid   cid    perinc |
|---------------------|
1. |   1   326   2.0e+04 |
2. |   2   326   1.3e+04 |
3. |   3   325       276 |
4. |   4   325      7200 |
5. |   5   325   2.4e+04 |
+---------------------+
``````

``````. frlink m:1 cid, frame(community)
``````

``````. generate rel_inc = perinc / frval(community, cominc)
. list pid cid cominc perinc rel_inc in 1/5, abbrev(16)

+------------------------------------------+
| pid   cid    cominc    perinc    rel_inc |
|------------------------------------------|
1. |   1   326   2.8e+03   2.0e+04   7.272727 |
2. |   2   326   2.8e+03   1.3e+04   4.727273 |
3. |   3   325   1.1e+03       276   .2509091 |
4. |   4   325   1.1e+03      7200   6.545455 |
5. |   5   325   1.1e+03   2.4e+04   21.81818 |
+------------------------------------------+
``````

## 4. frames 与 frget 的连接

``````. frame create long
. frame change long
. use long
. list in 1/10, abbrev(10)

+--------------------------------+
| pid   j        bmi   age   sbp |
|--------------------------------|
1. |   1   1    19.5325    45   108 |
2. |   1   2    19.5325    45   108 |
3. |   1   3    19.5325    45   110 |
4. |   2   1   22.53906    49    96 |
5. |   2   2   22.53906    49   104 |
|--------------------------------|
6. |   2   3   22.53906    49   108 |
7. |   3   1   25.29938    82   163 |
8. |   3   2   25.29938    82   174 |
9. |   3   3   25.29938    82   182 |
10. |   4   1   24.53125    57   147 |
+--------------------------------+
``````

``````. frlink m:1 pid, frame(patients1)
``````

``````. frget male, from(patients1)
. list in 1/10, abbrev(10)

+---------------------------------------------------+
| pid   j        bmi   age   sbp   patients1   male |
|---------------------------------------------------|
1. |   1   1    19.5325    45   108           1      1 |
2. |   1   2    19.5325    45   108           1      1 |
3. |   1   3    19.5325    45   110           1      1 |
4. |   2   1   22.53906    49    96           2      1 |
5. |   2   2   22.53906    49   104           2      1 |
|---------------------------------------------------|
6. |   2   3   22.53906    49   108           2      1 |
7. |   3   1   25.29938    82   163           3      1 |
8. |   3   2   25.29938    82   174           3      1 |
9. |   3   3   25.29938    82   182           3      1 |
10. |   4   1   24.53125    57   147           4      0 |
+---------------------------------------------------+
``````

``````. mixed sbp i.male c.age##c.bmi || pid:, nolog noheader

------------------------------------------------------------------------------
sbp |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
1.male |   2.039036   2.435889     0.84   0.403    -2.735219    6.813291
age |   .6067942   .9697704     0.63   0.532    -1.293921    2.507509
bmi |    1.03139   2.212426     0.47   0.641    -3.304886    5.367665
|
c.age#c.bmi |  -.0003429   .0405329    -0.01   0.993    -.0797859    .0791002
|
_cons |   72.64936   53.15391     1.37   0.172     -31.5304    176.8291
------------------------------------------------------------------------------

------------------------------------------------------------------------------
Random-effects Parameters  |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
pid: Identity                |
var(_cons) |   2085.381   76.46401      1940.773    2240.764
-----------------------------+------------------------------------------------
var(Residual) |   545.5202   12.98857      520.6479    571.5807
------------------------------------------------------------------------------
LR test vs. linear model: chibar2(01) = 3875.23       Prob >= chibar2 = 0.0000
``````

## 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`