Stata:线性趋势检验-lintrend

发布时间:2021-12-07 阅读 3600

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

温馨提示: 定期 清理浏览器缓存,可以获得最佳浏览体验。

New! lianxh 命令发布了:
随时搜索推文、Stata 资源。安装:
. ssc install lianxh
详情参见帮助文件 (有惊喜):
. help lianxh
连享会新命令:cnssc, ihelp, rdbalance, gitee, installpkg

课程详情 https://gitee.com/lianxh/Course

课程主页 https://gitee.com/lianxh/Course

⛳ Stata 系列推文:

PDF下载 - 推文合集

作者:闫天琳 (中山大学)
邮箱yantlin@mail2.sysu.edu.cn

编者按:本文部分摘译自下文,特此致谢!
Source:Garrett, J. M. 1996. sg50: Graphical assessment of linear trend. Stata Technical Bulletin 30: 9–15. In Stata Technical Bulletin Reprints, vol. 5, 152–160. College Station, TX: Stata Press. -PDF-


目录


1. 简介

在经济学研究中,我们经常要研究变量之间的相关关系。其中,线性相关是非常基础且重要的一种相关关系。通过线性回归,我们可以进行因果分析和数据预测。当然上述研究的前提是,变量之间需要满足线性关系,这就需要进行线性趋势的检验。

本篇推文介绍 Stata 中用于线性趋势检验的命令 lintrend。该命令通过报告不同组别中 (根据自变量分组,例如五等分) 因变量的取值 (若为连续变量,则为组内均值;若为二元变量,则为比例和对数几率) 和绘制相关图形,来检验变量间是否满足线性关系。

2. 命令介绍

*命令安装
ssc install lintrend, replace
*命令语法
lintrend yvar xvar [if exp] [in range], [groups(#) round(#) int] [graph noline graph_options]

其中,

  • yvar: 因变量。如果是二元变量,则按分组变量计算其比例和对数几率,否则按分组变量计算其均值;
  • xvar: 自变量 (分组变量)。

option 如下:

  • group(#):将自变量 xvar 等分成若干组,并用组内均值代表组值;
  • round(#):将自变量 xvar 四舍五入到 # 倍数,以此作为组别划分依据;
  • integer:若自变量 xvar 是整数,则直接根据具体数值分组。
  • graph:绘制图形。yvar 为连续变量,则绘制其均值,yvar 为二元变量,则绘制其对数几率 (Log odds of Y for binary yvar);
  • noline:不绘制回归线。

3. Stata 实操

3.1 探究与高血压有关的因素

在本例中,我们使用来自对 1784 个 24-51 岁成年人的高血压调查数据集 (hyperten)。其中,sbpavg 为平均收缩压,age 为年龄,ses 为社会经济地位,hbp 为是否高血压。首先,我们使用 groups(#) 分组来检验高血压与年龄的关系。

. lxhuse hyperten.dta, clear
. describe

Contains data from hyperten.dta
 Observations:         1,784                  
    Variables:             4                  6 Feb 1996 16:49
----------------------------------------------------------------
Variable      Storage   Display    Value
    name         type    format    label      Variable label
----------------------------------------------------------------
sbpavg          float   %9.0g                 Average Systolic Blood Pressure
age             byte    %8.0g                 Current Age
ses             byte    %8.0g      seslbl     Socioeconomic Status
hbp             byte    %8.0g      yesno      High Blood Pressure
----------------------------------------------------------------

将样本分成 10 个大小相等的组,并计算每个年龄组中高血压 (1=高血压,0=正常) 的比例和对数几率。

. lintrend hbp age, groups(10) graph

The proportion and ln(odds) of hbp by categories of age
  (Note: 10 age categories of equal sample size;
     Uses mean age value for each category)
  +------------------------------------------------+
  |  age   min   max    d   total    hbp   ln_odds |
  |------------------------------------------------|
  | 26.0    24    27   24     246   0.10     -2.22 |
  | 28.5    28    29   27     165   0.16     -1.63 |
  | 30.6    30    31   26     169   0.15     -1.70 |
  | 32.4    32    33   34     179   0.19     -1.45 |
  | 34.5    34    35   43     151   0.28     -0.92 |
  | 36.5    36    37   44     163   0.27     -0.99 |
  | 39.0    38    40   70     220   0.32     -0.76 |
  | 41.9    41    43   66     152   0.43     -0.26 |
  | 45.5    44    47   86     200   0.43     -0.28 |
  | 49.0    48    51   65     137   0.47     -0.10 |
  +------------------------------------------------+

可以看出,年龄和高血压之间存在正线性关系。然后,我们使用 round(#) 分组来检验高血压与年龄的关系,即将年龄四舍五入到 # 的倍数。

. lintrend hbp age, round(5) graph

The proportion and ln(odds) of hbp by categories of age
  (Note: age in categories rounded to nearest 5)
  +------------------------------------------------+
  | age   min   max     d   total    hbp   ln_odds |
  |------------------------------------------------|
  |  25    24    27    24     246   0.10     -2.22 |
  |  30    28    32    72     436   0.17     -1.62 |
  |  35    33    37   102     391   0.26     -1.04 |
  |  40    38    42   117     323   0.36     -0.57 |
  |  45    43    47   105     249   0.42     -0.32 |
  |  50    48    51    65     137   0.47     -0.10 |
  +------------------------------------------------+

接着,使用 integer 分组来检验高血压与社会地位之间的关系。社会经济地位 ses 是一个序数变量,ses=1 表示低社会经济地位,ses=2 表示中等社会经济地位,ses=3 表示高社会经济地位。

. lintrend hbp ses, integer graph

The proportion and ln(odds) of hbp by categories of ses
  (Note: ses in categories using original values)
  +-----------------------------------------+
  |      ses     d   total    hbp   ln_odds |
  |-----------------------------------------|
  |    1:Low   215     670   0.32     -0.75 |
  | 2:Middle   138     537   0.26     -1.06 |
  |   3:High   117     512   0.23     -1.22 |
  +-----------------------------------------+

可以看出的是,高血压随着社会经济地位的上升而减少,两者有较强的负线性相关性。

最后,使用 groups(#) 分组来检验平均收缩压与年龄的关系。前面的例子检验的是二元变量 (是否高血压),lintrend 会计算对数几率。在本例中,我们将使用连续变量平均收缩压 sbpavg 来代表高血压,lintrend 则会计算平均值。

. lintrend sbpavg age, groups(10) graph

The mean of sbpavg by categories of age 
  (Note: 10 age categories of equal sample size;
     Uses mean age value for each category)
  +-----------------------------------+
  |  age   min   max   total   sbpavg |
  |-----------------------------------|
  | 26.0    24    27     246   120.83 |
  | 28.5    28    29     165   119.79 |
  | 30.6    30    31     169   120.27 |
  | 32.4    32    33     178   122.99 |
  | 34.5    34    35     151   124.16 |
  | 36.5    36    37     163   125.56 |
  | 39.0    38    40     219   124.53 |
  | 41.9    41    43     152   129.62 |
  | 45.5    44    47     200   131.05 |
  | 49.0    48    51     137   132.59 |
  +-----------------------------------+

3.2 探究与背痛康复有关的特征

本例数据来自于对 1552 个背痛患者进行了 6 个月的随访,以确定与康复相关的特征的调查数据。其中,better 为八周后的恢复概率 (1=全部恢复,0=没有恢复),score 为残疾状况。

. lxhuse backpain.dta, clear
. describe

Contains data from backpain.dta
 Observations:         1,552                  Recovery from back pain
    Variables:             4                  9 Feb 1996 16:32
----------------------------------------------------------------------
Variable      Storage   Display    Value
    name         type    format    label      Variable label
----------------------------------------------------------------------
better          byte    %8.0g      yesno      Better at 8 Weeks
score           byte    %8.0g                 Disability Score
score_2         int     %9.0g                 Disability Score Squared
score_3         int     %9.0g                 Disability Score Cubed
----------------------------------------------------------------------
. lintrend better score, groups(12) graph

The proportion and ln(odds) of better by categories of score
  (Note: 12 score categories of equal sample size;
     Uses mean score value for each category)
  +----------------------------------------------------+
  | score   min   max     d   total   better   ln_odds |
  |----------------------------------------------------|
  |   0.4     0     1   151     170     0.89      2.07 |
  |   2.5     2     3   100     136     0.74      1.02 |
  |   4.6     4     5    69     105     0.66      0.65 |
  |   6.5     6     7    67     119     0.56      0.25 |
  |   8.5     8     9    75     139     0.54      0.16 |
  |  10.5    10    11    60     128     0.47     -0.13 |
  |  12.5    12    13    57     118     0.48     -0.07 |
  |  15.1    14    16    96     197     0.49     -0.05 |
  |  17.0    17    17    33      79     0.42     -0.33 |
  |  18.5    18    19    49     147     0.33     -0.69 |
  |  20.5    20    21    40     121     0.33     -0.71 |
  |  22.5    22    23    24      93     0.26     -1.06 |
  +----------------------------------------------------+

可以看出,当残疾分数从 0 增加到 10 左右时,恢复速度会急剧下降,当残疾分数从 10 增加到 15 左右时,恢复速度又会趋于平稳,当残疾分数超过 15 时,恢复速度会继续急剧下降。上述关系可能表明二者间存在非线性关系。进一步,我们通过将残疾状况的二次项、三次项引入到逻辑回归模型中分析,回归结果也证实了上述猜想。

. logistic better score score_2 score_3

Logistic regression                                     Number of obs =  1,552
                                                        LR chi2(3)    = 207.17
                                                        Prob > chi2   = 0.0000
Log likelihood = -969.56991                             Pseudo R2     = 0.0965
------------------------------------------------------------------------------
      better | Odds ratio   Std. err.      z    P>|z|     [95% conf. interval]
-------------+----------------------------------------------------------------
       score |      0.643      0.050    -5.71   0.000        0.553       0.748
     score_2 |      1.032      0.008     4.05   0.000        1.016       1.048
     score_3 |      0.999      0.000    -3.63   0.000        0.999       1.000
       _cons |      7.937      1.713     9.60   0.000        5.200      12.116
------------------------------------------------------------------------------

4. 参考文献

  • Garrett, J. M. (2017). LINTREND: Stata module to graph observed proportions or means for a continuous or ordinal X variable. -Link-
  • Garrett, J. M. 1996. sg50: Graphical assessment of linear trend. Stata Technical Bulletin 30: 9–15. In Stata Technical Bulletin Reprints, vol. 5, 152–160. College Station, TX: Stata Press. -PDF-
  • Ronan M. Conroy, 2002. "Choosing an appropriate real-life measure of effect size:the case of a continuous predictor and a binary outcome," Stata Journal, StataCorp LP, vol. 2(3), pages 290-295, August. -PDF-

5. 相关推文

Note:产生如下推文列表的 Stata 命令为:
lianxh 绘图, m
安装最新版 lianxh 命令:
ssc install lianxh, replace

相关课程

免费公开课

最新课程-直播课

专题 嘉宾 直播/回看视频
最新专题 文本分析、机器学习、效率专题、生存分析等
研究设计 连玉君 我的特斯拉-实证研究设计-幻灯片-
面板模型 连玉君 动态面板模型-幻灯片-
面板模型 连玉君 直击面板数据模型 [免费公开课,2小时]
  • Note: 部分课程的资料,PPT 等可以前往 连享会-直播课 主页查看,下载。

课程主页

课程主页

关于我们

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

连享会小程序:扫一扫,看推文,看视频……

扫码加入连享会微信群,提问交流更方便

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

New! lianxhsongbl 命令发布了:
随时搜索连享会推文、Stata 资源,安装命令如下:
. ssc install lianxh
使用详情参见帮助文件 (有惊喜):
. help lianxh