title: "table"
author: "wintryheart"
date: "2022/5/6"
output: html_document
knitr::opts_chunk$set(engine="stata", engine.path="C:\\Program Files\\Stata17\\StataMP-64.exe", error=TRUE, cleanlog=TRUE, comment=NA)
library(Statamarkdown)
注:
- 當初我的電腦上的STATA版本是15幕庐,所以Statamarkdown包在安裝時搜索到的是STATA15搀愧。
- 后來我又安裝了STATA17,但是這個包似乎沒有提供函數(shù)來修改stata engine国夜。
- 所以我只好在knitr里修改engine.path米愿,但是似乎只在chuck里運行有用精居,整個markdown文件運行knitr時屯耸,會出錯腥放。暫時沒找到解決辦法终惑。
解決辦法終于找到菇篡。參見3 Stata Engine Path | Using Statamarkdown (wisc.edu)
library(Statamarkdown)
stataexe <- "C:/Program Files/Stata17/StataMP-64.exe"
knitr::opts_chunk$set(engine.path=list(stata=stataexe))
knitr::opts_chunk$set(echo = TRUE, error=TRUE, cleanlog=TRUE, comment=NA)
前言
1. 與Table有關的常用命令
tabulate灭红,最方便的做表命令脓斩,但只能用于一維和二維表证逻。多維需要借助by香椎,相當于拆成一系列二維表漱竖。
summarize, 做連續(xù)型變量分布的統(tǒng)計概要(summary statistics)最好用的命令。但只能做一維列表畜伐。
tabstat馍惹,與summarize相比,可以做分類別的連續(xù)型變量分布統(tǒng)計玛界,但不能統(tǒng)計分類變量的分布万矾。
fsum,與tabstat相比慎框,可以統(tǒng)計分類變量的百分比分布良狈。
2. 個人需求
有時,我需要做多維的百分比列聯(lián)表笨枯。以前是使用by+tabulate薪丁。輸出的結果是一系列二維表,不太好放進PPT馅精,只能手工整理成一個表格严嗜。
其他Table命令,多擅長處理連續(xù)型變量洲敢,并不適合做分類變量或定序變量的百分比列聯(lián)表漫玄。
直到,我發(fā)現(xiàn)table命令沦疾,終于解決將多維列聯(lián)表輸出在一個表中的問題称近。
table命令簡介
[R] table -- Table of frequencies, summaries, and command results
table是非常靈活的命令第队,主要做變量描述性統(tǒng)計和列聯(lián)表。
老版的table命令介紹刨秆,參見《Stata:今天你 “table” 了嗎凳谦?》
這里介紹的是STATA17的新版table命令。
語法
table (rowspec) (colspec) [(tabspec)] [if] [in] [weight] [, options]
rowspec衡未、colspec尸执、tabspec 可以為空,可以是變量名缓醋,也可以是關鍵詞如失。
常用關鍵詞:
關鍵詞 | 描述 |
---|---|
result | requested statistics |
var | variables from statistic() option |
across | index across() specifications |
colname | column names for matrix statistics |
rowname | row names for matrix statistics |
command | index option command() |
statcmd | index options statistic() and command() |
基本概念
1. 布局
一個表的布局是由行、列和表的維度構成送粱。rowspec褪贵、colspec、tabspec統(tǒng)稱為表的“布局”(layout)
- rowspec抗俄,行規(guī)范脆丁,指定行信息,
- colspec动雹,列規(guī)范槽卫,指定列信息,
- tabspec胰蝠,表的維度規(guī)范歼培,指定表的維度的信息。
例如茸塞,我們指定變量名來定義行躲庄,并將統(tǒng)計信息放在列中,反之亦然翔横。
2.關鍵詞
table可以包含這么多不同的統(tǒng)計信息读跷,我們可以指定 關鍵詞(keywords)來唯一標識從命令中收集到的結果和表計算出的統(tǒng)計信息。
如果我們在layout中忽略了一個必要的關鍵字禾唁,table將自動填充一個效览。
3. 關鍵詞使用規(guī)則
決定關鍵詞對于唯一標識表中的值是否必需的規(guī)則如下:
- 如果指定了多個統(tǒng)計信息,則在布局中使用result荡短。
- 如果在選項statistic()中指定了多個變量丐枉,而沒有指定選項command(),則在布局中使用var掘托。
- 如果比率統(tǒng)計使用了多個across()瘦锹,那么在布局中使用across。
- 如果指定了選項 command(),則布局中需要使用colname弯院。另外辱士,如果還在選項statistic()中指定了多個變量,則需要colname而不是2中要求的var听绳。
- 如果指定了多個command()選項颂碘,而未指定選項statistic(),則需要在布局中使用command椅挣。
- 如果同時指定了選項command()和statistic()头岔,那么布局中使用statcmd
如果我們沒有在rowspec、colspec或tabspec中直接指定一個必要的關鍵字鼠证,則缺少的關鍵字將被自動添加到布局中峡竣,如下所示:
- 如果行規(guī)范為空,則將缺少的關鍵字放入rowspec中量九。
- 如果行規(guī)范不為空适掰,但列規(guī)范為空,則將缺失的關鍵字放入colspec中娩鹉。
- 如果行和列的規(guī)范不為空攻谁,但表的高維規(guī)范為空,并且result是唯一缺少的關鍵字弯予,并且只有一個統(tǒng)計信息(result),那么將result放入tabspec个曙。
- 否則锈嫩,將缺少的關鍵字附加到rowvars。
下面演示關鍵字在布局中的使用規(guī)則:
sysuse auto
describe
//指定統(tǒng)計信息放在行
table (result) rep78, statistic(mean mpg) statistic(sd mpg)
//省略關鍵詞垦搬,命令與上面等價
table () rep78, statistic(mean mpg) statistic(sd mpg)
//行不空呼寸,而列空,則統(tǒng)計信息放在列猴贰。
table rep78, statistic(mean mpg) statistic(sd mpg)
running: C:\Program Files\Stata17\StataMP-64.exe /q /e do "C:\Users\wintryheart\Desktop\stata238026c06106.do"
stata output from unnamed-chunk-1
Contains data from C:\PROGRA~1\Stata17\ado\base/a/auto.dta
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
headroom float %6.1f Headroom (in.)
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
-------------------------------------------------------------------------------------
| Repair record 1978
| 1 2 3 4 5 Total
-------------------+-----------------------------------------------------------------
Mean | 21 19.125 19.43333 21.66667 27.36364 21.28986
Standard deviation | 4.242641 3.758324 4.141325 4.93487 8.732385 5.866408
-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
| Repair record 1978
| 1 2 3 4 5 Total
-------------------+-----------------------------------------------------------------
Mean | 21 19.125 19.43333 21.66667 27.36364 21.28986
Standard deviation | 4.242641 3.758324 4.141325 4.93487 8.732385 5.866408
-------------------------------------------------------------------------------------
---------------------------------------------------
| Mean Standard deviation
-------------------+-------------------------------
Repair record 1978 |
1 | 21 4.242641
2 | 19.125 3.758324
3 | 19.43333 4.141325
4 | 21.66667 4.93487
5 | 27.36364 8.732385
Total | 21.28986 5.866408
---------------------------------------------------
主要選項
totals (totals)和nototals控制哪些總計將在表中顯示对雪。默認情況下,報告所有的總計。
statistic (statspec)指定要顯示的統(tǒng)計信息米绕。包括三類:
- 頻率統(tǒng)計信息:stat(freqstat)
- 匯總統(tǒng)計信息:stat(sumstat varlist)
- 比率統(tǒng)計信息:stat(ratiostat [varlist] [瑟捣, ratio_options]),
command(cmdspec)指定從其中收集結果的Stata命令栅干÷跆祝可
重復使用,從多個STATA命令收集結果碱鳞。
commnad可以報告存儲在r()和e()中的STATA命令運行結果桑李。(通過相關命令的help文件查看存儲了哪些結果)
快速使用
sysuse auto, clear
//生成一個新的分類變量
gen mpg2=1
replace mpg2=2 if mpg>20
// 一維表
table mpg2
table mpg2, stat(freq) stat(percent)
//二維表
//交叉聯(lián)合概率
table rep78 foreign, stat(percent) nformat(%5.2f)
//行邊緣和為100%,在每個因子變量水平上的百分比。
table rep78, stat(fvpercent foreign)
table rep78, stat(fvpercent foreign mpg2)
//三維表的兩種方式
table (rep78) (foreign mpg2)
table (rep78) (foreign) (mpg2)
//因子交互
table rep78, stat(fvpercent foreign#mpg2) nototal
//帶相關分析的表贵白。命令pwcorr的相關矩陣存儲在r(C)里率拒。
table (rowname) (colname), command(r(C): pwcorr mpg weight displacement)
//帶回歸系數(shù)的表
table colname, command(regress mpg weight foreign)
running: C:\Program Files\Stata17\StataMP-64.exe /q /e do "C:\Users\wintryheart\Desktop\stata23807a6d140e.do"
stata output from auto2
(36 real changes made)
--------------------
| Frequency
--------+-----------
mpg2 |
1 | 38
2 | 36
Total | 74
--------------------
------------------------------
| Frequency Percent
--------+---------------------
mpg2 |
1 | 38 51.35
2 | 36 48.65
Total | 74 100.00
------------------------------
-------------------------------------------------
| Car origin
| Domestic Foreign Total
-------------------+-----------------------------
Repair record 1978 |
1 | 2.90 2.90
2 | 11.59 11.59
3 | 39.13 4.35 43.48
4 | 13.04 13.04 26.09
5 | 2.90 13.04 15.94
Total | 69.57 30.43 100.00
-------------------------------------------------
----------------------------------------
| Car origin
| Domestic Foreign
-------------------+--------------------
Repair record 1978 |
1 | 100.00 0.00
2 | 100.00 0.00
3 | 90.00 10.00
4 | 50.00 50.00
5 | 18.18 81.82
Total | 69.57 30.43
----------------------------------------
--------------------------------------------------------
| Car origin mpg2
| Domestic Foreign 1 2
-------------------+------------------------------------
Repair record 1978 |
1 | 100.00 0.00 50.00 50.00
2 | 100.00 0.00 62.50 37.50
3 | 90.00 10.00 66.67 33.33
4 | 50.00 50.00 33.33 66.67
5 | 18.18 81.82 36.36 63.64
Total | 69.57 30.43 52.17 47.83
--------------------------------------------------------
------------------------------------------------------------------------
| Car origin
| Domestic Foreign Total
| mpg2 mpg2 mpg2
| 1 2 Total 1 2 Total 1 2 Total
-------------------+----------------------------------------------------
Repair record 1978 |
1 | 1 1 2 1 1 2
2 | 5 3 8 5 3 8
3 | 20 7 27 3 3 20 10 30
4 | 6 3 9 9 9 6 12 18
5 | 2 2 4 5 9 4 7 11
Total | 32 16 48 4 17 21 36 33 69
------------------------------------------------------------------------
mpg2 = 1
------------------------------------------------
| Car origin
| Domestic Foreign Total
-------------------+----------------------------
Repair record 1978 |
1 | 1 1
2 | 5 5
3 | 20 20
4 | 6 6
5 | 4 4
Total | 32 4 36
------------------------------------------------
mpg2 = 2
------------------------------------------------
| Car origin
| Domestic Foreign Total
-------------------+----------------------------
Repair record 1978 |
1 | 1 1
2 | 3 3
3 | 7 3 10
4 | 3 9 12
5 | 2 5 7
Total | 16 17 33
------------------------------------------------
mpg2 = Total
------------------------------------------------
| Car origin
| Domestic Foreign Total
-------------------+----------------------------
Repair record 1978 |
1 | 2 2
2 | 8 8
3 | 27 3 30
4 | 9 9 18
5 | 2 9 11
Total | 48 21 69
------------------------------------------------
-------------------------------------------------------------
| Car origin
| Domestic Domestic Foreign Foreign
| mpg2
| 1 2 1 2
-------------------+-----------------------------------------
Repair record 1978 |
1 | 50.00 50.00 0.00 0.00
2 | 62.50 37.50 0.00 0.00
3 | 66.67 23.33 0.00 10.00
4 | 33.33 16.67 0.00 50.00
5 | 0.00 18.18 36.36 45.45
-------------------------------------------------------------
--------------------------------------------------------------------------------
| Mileage (mpg) Weight (lbs.) Displacement (cu. in.)
-----------------------+--------------------------------------------------------
Mileage (mpg) | 1 -.8071749 -.7056426
Weight (lbs.) | -.8071749 1 .8948958
Displacement (cu. in.) | -.7056426 .8948958 1
--------------------------------------------------------------------------------
----------------------------
| Coefficient
--------------+-------------
Weight (lbs.) | -.0065879
Car origin | -1.650029
Intercept | 41.6797
----------------------------
列聯(lián)表實戰(zhàn)
sysuse auto, clear
//連續(xù)型變量描述
//foreign為分類變量(因子類型),其他變量為連續(xù)型變量
table, stat(mean price-gear_ratio) stat(fvpercent foreign)
//連續(xù)變量分foreign類別描述
table (result) (foreign), stat(mean price-gear_ratio) stat(sd price-gear_ratio)
// 對于連續(xù)變量的概要統(tǒng)計比不上summary和tabstat好用禁荒。
//百分比列聯(lián)表
//生成兩個新的分類變量
egen mpg2=cut(mpg), group(2) //2等分
egen weight4=cut(weight), group(4) //4等分
//以mpg2為因變量俏橘,以weight4為自變量,以foreign為控制變量
//我們要觀察一個變量隨著另一個變量的變化而發(fā)生的變化圈浇。
//即寥掐,我們需要計算條件概率或相對概率。
//percent磷蜀,計算的是交叉聯(lián)合概率召耘。
//第一種方式:借助across()選項,計算條件概率褐隆。
//注意污它,在自變量的每一類別內,所有因變量的類別的百分比分布合計必須等于100%庶弃。
table(mpg2) (weight4), stat(percent, across(mpg2))
//隨著車重的增加衫贬,低油耗的車比例下降,高油耗的車比例上升歇攻。
//增加foreign為控制變量
table (foreign weight4) (mpg2) (result), statistic(percent, across(mpg2)) stat(freq) totals(foreign#weight4)
//第二種方式:使用fvpercent固惯,計算條件概率。
//fvpercent缴守,計算的是在因子類別下的百分比分布葬毫。
//我們可使用fvpercent計算條件概率,然后進行比較屡穗。
table weight4, stat(fvpercent mpg2) nototal
//或者贴捡,把因變量的分類放在行。
table ()(weight4), stat(fvpercent mpg2) nototal
//添加控制變量
table() (foreign weight4), stat(fvpercent mpg2) nototal
running: C:\Program Files\Stata17\StataMP-64.exe /q /e do "C:\Users\wintryheart\Desktop\stata23807bdf2781.do"
stata output from auto3
Mean |
Price | 6165.257
Mileage (mpg) | 21.2973
Repair record 1978 | 3.405797
Headroom (in.) | 2.993243
Trunk space (cu. ft.) | 13.75676
Weight (lbs.) | 3019.459
Length (in.) | 187.9324
Turn circle (ft.) | 39.64865
Displacement (cu. in.) | 197.2973
Gear ratio | 3.014865
Factor variable percent |
Car origin=Domestic | 70.27
Car origin=Foreign | 29.73
------------------------------------
----------------------------------------------------------
| Car origin
| Domestic Foreign Total
-------------------------+--------------------------------
Mean |
Price | 6072.423 6384.682 6165.257
Mileage (mpg) | 19.82692 24.77273 21.2973
Repair record 1978 | 3.020833 4.285714 3.405797
Headroom (in.) | 3.153846 2.613636 2.993243
Trunk space (cu. ft.) | 14.75 11.40909 13.75676
Weight (lbs.) | 3317.115 2315.909 3019.459
Length (in.) | 196.1346 168.5455 187.9324
Turn circle (ft.) | 41.44231 35.40909 39.64865
Displacement (cu. in.) | 233.7115 111.2273 197.2973
Gear ratio | 2.806538 3.507273 3.014865
Standard deviation |
Price | 3097.104 2621.915 2949.496
Mileage (mpg) | 4.743297 6.611187 5.785503
Repair record 1978 | .837666 .7171372 .9899323
Headroom (in.) | .9157578 .4862837 .8459948
Trunk space (cu. ft.) | 4.306288 3.216906 4.277404
Weight (lbs.) | 695.3637 433.0035 777.1936
Length (in.) | 20.04605 13.68255 22.26634
Turn circle (ft.) | 3.967582 1.501082 4.399354
Displacement (cu. in.) | 85.26299 24.88054 91.83722
Gear ratio | .3359556 .2969076 .4562871
----------------------------------------------------------
-----------------------------------------------------
| weight4
| 0 1 2 3 Total
--------+--------------------------------------------
mpg2 |
0 | 21.05 76.47 90.00 47.30
1 | 100.00 78.95 23.53 10.00 52.70
Total | 100.00 100.00 100.00 100.00 100.00
-----------------------------------------------------
Percent
---------------------------------------
| mpg2
| 0 1 Total
------------+--------------------------
Car origin |
Domestic |
weight4 |
0 | 100.00 100.00
1 | 100.00 100.00
2 | 75.00 25.00 100.00
3 | 90.00 10.00 100.00
Foreign |
weight4 |
0 | 100.00 100.00
1 | 44.44 55.56 100.00
2 | 100.00 100.00
---------------------------------------
Frequency
------------------------------
| mpg2
| 0 1 Total
------------+-----------------
Car origin |
Domestic |
weight4 |
0 | 6 6
1 | 10 10
2 | 12 4 16
3 | 18 2 20
Foreign |
weight4 |
0 | 12 12
1 | 4 5 9
2 | 1 1
------------------------------
-------------------------
| mpg2
| 0 1
--------+----------------
weight4 |
0 | 0.00 100.00
1 | 21.05 78.95
2 | 76.47 23.53
3 | 90.00 10.00
-------------------------
----------------------------------------
| weight4
| 0 1 2 3
-------+--------------------------------
mpg2=0 | 0.00 21.05 76.47 90.00
mpg2=1 | 100.00 78.95 23.53 10.00
----------------------------------------
-------------------------------------------------------------------
| Car origin
| Domestic Foreign
| weight4 weight4
| 0 1 2 3 0 1 2
-------+-----------------------------------------------------------
mpg2=0 | 0.00 0.00 75.00 90.00 0.00 44.44 100.00
mpg2=1 | 100.00 100.00 25.00 10.00 100.00 55.56 0.00
-------------------------------------------------------------------