# Stata：线性趋势检验-lintrend

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

New！ `lianxh` 命令发布了：

`. ssc install lianxh`

`. help lianxh`

⛳ Stata 系列推文：

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-

## 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 探究与高血压有关的因素

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

``````. 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 |
+------------------------------------------------+
``````

``````. 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 |
+------------------------------------------------+
``````

``````. 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 |
+-----------------------------------------+
``````

``````. 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 探究与背痛康复有关的特征

``````. 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 |
+----------------------------------------------------+
``````

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

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