層別に平均値と標準偏差(SD)と人数だけを算出
年代別など,層別に平均値と標準偏差を出したい場面は多いです。
サンプルデータを使いながら説明します。
mpgの平均値,標準偏差,人数をforeign別に出したい
とします。Stata(バージョン13.1)では3つのやり方があります。
【算出したい変数が1つのとき】
以下の3つでどれでも同じ結果が得られます。
それぞれ特徴などを説明していきます。
最もシンプルなものはtabulate(略してtabでOK)です。
より柔軟に出すにはtableを使います。オプションのc( )の所
に,「出したい統計量 変数名」のセットをいくつも書いていく形式です。
同じく柔軟なのはtabstatです
それぞれヘルプをみるともっと細かい設定について解説されています。
【算出したい変数が複数あるとき】
複数の変数について出したいときは,tabstatが便利です。
4つの変数mpg price weight lengthそれぞれの平均,SD,人数を
foreign別に出したいときは
表の上の所に,算出した統計量が表示されています。
繰り返しの命令を使って,tabulateやtableでもできます。
人数やtotalの部分も不要なら,tableを使ってもっと
シンプルな出力ができます
(20150327 追記)
こちらのやり方も参照
層別に複数変数の平均値を縦一列に算出
サンプルデータを使いながら説明します。
sysuse auto
mpgの平均値,標準偏差,人数をforeign別に出したい
とします。Stata(バージョン13.1)では3つのやり方があります。
【算出したい変数が1つのとき】
以下の3つでどれでも同じ結果が得られます。
tabulate foreign, sum(mpg)
table foreign, c(mean mpg sd mpg n mpg)
tabstat mpg, stat(mean sd n) by(foreign)
table foreign, c(mean mpg sd mpg n mpg)
tabstat mpg, stat(mean sd n) by(foreign)
それぞれ特徴などを説明していきます。
最もシンプルなものはtabulate(略してtabでOK)です。
tab foreign, sum(mpg)
| Summary of Mileage (mpg)
Car type | Mean Std. Dev. Freq.
-----------------+-------------------------------------------------
Domestic | 19.826923 4.7432972 52
Foreign | 24.772727 6.6111869 22
-----------------+-------------------------------------------------
Total | 21.297297 5.7855032 74
より柔軟に出すにはtableを使います。オプションのc( )の所
に,「出したい統計量 変数名」のセットをいくつも書いていく形式です。
table foreign, c(mean mpg sd mpg n mpg)
-------------------------------------------------------------------
Car type | mean(mpg) sd(mpg) N(mpg)
---------------+--------------------------------------------------
Domestic | 19.8269 4.743297 52
Foreign | 24.7727 6.611187 22
---------------------------------------------------------------------
同じく柔軟なのはtabstatです
tabstat mpg, stat(mean sd n) by(foreign)
Summary for variables: mpg
by categories of: foreign (Car type)
foreign | mean sd N
--------------+-------------------------------------------
Domestic | 19.82692 4.743297 52
Foreign | 24.77273 6.611187 22
--------------+------------------------------------------
Total | 21.2973 5.785503 74
----------------------------------------
それぞれヘルプをみるともっと細かい設定について解説されています。
【算出したい変数が複数あるとき】
複数の変数について出したいときは,tabstatが便利です。
4つの変数mpg price weight lengthそれぞれの平均,SD,人数を
foreign別に出したいときは
tabstat mpg price weight length, stat( mean sd n ) by(foreign)
Summary statistics: mean, sd, N
by categories of: foreign (Car type)
foreign | mpg price weight length
--------------+-----------------------------------------------------------
Domestic | 19.82692 6072.423 3317.115 196.1346
| 4.743297 3097.104 695.3637 20.04605
| 52 52 52 52
--------------+--------------------------------------------------------------
Foreign | 24.77273 6384.682 2315.909 168.5455
| 6.611187 2621.915 433.0035 13.68255
| 22 22 22 22
--------------+----------------------------------------
Total | 21.2973 6165.257 3019.459 187.9324
| 5.785503 2949.496 777.1936 22.26634
| 74 74 74 74
--------------------------------------------------------------------------------
表の上の所に,算出した統計量が表示されています。
繰り返しの命令を使って,tabulateやtableでもできます。
foreach x of varlist mpg price weight length {
tab foreign, sum(`x')
}
tab foreign, sum(`x')
}
| Summary of Mileage (mpg)
Car type | Mean Std. Dev. Freq.
-----------------+------------------------------------------------
Domestic | 19.826923 4.7432972 52
Foreign | 24.772727 6.6111869 22
-----------------+------------------------------------------------
Total | 21.297297 5.7855032 74
| Summary of Price
Car type | Mean Std. Dev. Freq.
-----------------+------------------------------------
Domestic | 6,072.423 3,097.104 52
Foreign | 6,384.682 2,621.915 22
-----------------+------------------------------------
Total | 6,165.257 2,949.496 74
| Summary of Weight (lbs.)
Car type | Mean Std. Dev. Freq.
-----------------+------------------------------------------------
Domestic | 3,317.115 695.36374 52
Foreign | 2,315.909 433.00345 22
-----------------+------------------------------------------------
Total | 3,019.459 777.19357 74
| Summary of Length (in.)
Car type | Mean Std. Dev. Freq.
-----------------+-------------------------------------------------
Domestic | 196.13462 20.046054 52
Foreign | 168.54545 13.682548 22
-----------------+-------------------------------------------------
Total | 187.93243 22.26634 74
人数やtotalの部分も不要なら,tableを使ってもっと
シンプルな出力ができます
foreach x of varlist mpg price weight length {
table foreign, c(mean `x' sd `x' )
}
table foreign, c(mean `x' sd `x' )
}
--------------------------------------------------
Car type | mean(mpg) sd(mpg)
---------------+-----------------------------------
Domestic | 19.8269 4.743297
Foreign | 24.7727 6.611187
----------------------------------------------------
-------------------------------------------------
Car type | mean(price) sd(price)
---------------+----------------------------------
Domestic | 6,072.4 3097.104
Foreign | 6,384.7 2621.915
---------------------------------------------------
-------------------------------------------------------
Car type | mean(weight) sd(weight)
---------------+--------------------------------------
Domestic | 3,317.1 695.3638
Foreign | 2,315.9 433.0034
-----------------------------------------------------
---------------------------------------------------
Car type | mean(length) sd(length)
---------------+------------------------------------
Domestic | 196.135 20.04605
Foreign | 168.545 13.68255
-----------------------------------------------------
(20150327 追記)
こちらのやり方も参照
層別に複数変数の平均値を縦一列に算出