この回答は、さまざまなトリックを示します。欠点は、あなたが求めるものを得るための簡単な方法がわからないことです。すべてのこれらのトリックは理解しやすく、しばしば有用であるというのは、メリットです。
あなたの例を使用してみましょう。これは目的に適しています。
. sysuse nlsw88, clear
(NLSW, 1988 extract)
ヒント#1自分でパーセント変数を計算できます。私は%singleに焦点を当てています。このデータセットでは、married
はバイナリなので、私は補完的なパーセントを表示しません。 これを計算したら、(a)それを定義するために使用したグループ内で定数であるという事実に頼ることができます。(b)直接テーブル化します。 tabdisp
がユーザーによって過小評価されています。それはプログラマーのコマンドとして課金されますが、まったく使用するのは難しくありません。 tabdisp
では、オンザフライで表示形式を設定できます。他のコマンドがformat
を使って直接割り当てることは害ではなく、有用かもしれません。
. egen pcsingle = mean(100 * (1 - married)), by(collgrad race)
. tabdisp collgrad race, c(pcsingle) format(%2.1f)
--------------------------------------
| race
college graduate | white black other
-----------------+--------------------
not college grad | 29.2 53.3 29.4
college grad | 31.4 51.5 33.3
--------------------------------------
. format pcsingle %2.1f
ヒント#2、ユーザー作成コマンドgroups
は異なる柔軟性を提供します。groups
は、SSCからインストールできます(厳密には、使用する前にをインストールする必要があります)。さまざまな種類のテーブルのラッパーですが、表示エンジンとしてlist
を使用しています。
. * do this installation just once
. ssc inst groups
. groups collgrad race pcsingle
+-------------------------------------------------------+
| collgrad race pcsingle Freq. Percent |
|-------------------------------------------------------|
| not college grad white 29.2 1217 54.19 |
| not college grad black 53.3 480 21.37 |
| not college grad other 29.4 17 0.76 |
| college grad white 31.4 420 18.70 |
| college grad black 51.5 103 4.59 |
|-------------------------------------------------------|
| college grad other 33.3 9 0.40 |
+-------------------------------------------------------+
これを改善することができます。特性を使ってより良いヘッダーテキストを設定することができます。 (実際には、これらは変数名よりも制約が少ないかもしれませんが、しばしば変数ラベルよりも短くする必要があります)。標準のlist
オプションを呼び出すことによってセパレータを使用できます。
. char pcsingle[varname] "% single"
. char collgrad[varname] "college?"
. groups collgrad race pcsingle , subvarname sepby(collgrad)
+-------------------------------------------------------+
| college? race % single Freq. Percent |
|-------------------------------------------------------|
| not college grad white 29.2 1217 54.19 |
| not college grad black 53.3 480 21.37 |
| not college grad other 29.4 17 0.76 |
|-------------------------------------------------------|
| college grad white 31.4 420 18.70 |
| college grad black 51.5 103 4.59 |
| college grad other 33.3 9 0.40 |
+-------------------------------------------------------+
ヒント#3ワイヤ表示は、文字列を等価にすることで変数にフォーマットします。私はこれを完全には説明していませんが、数字の表示と数値の表示を小数点以下の桁で組み合わせる場合はよく使用します。tabdisp
format(%2.1f)
とformat(%3.2f)
はほとんどの変数で問題ないかもしれませんが(ちなみに重要な部分は小数点以下の桁数です)、42のカウントが42.0または42.00と表示されてしまいます。 format()
オプションがtabdisp
の場合、文字列には到達せず、内容を変更します。それは文字列変数が何を含んでいるのか、どこから来たのかを知らない。だから、文字列はtabdisp
で表示されます。これはあなたが望むものです。
. gen s_pcsingle = string(pcsingle, "%2.1f")
. char s_pcsingle[varname] "% single"
groups
は、新鮮なデータセットとして集計されたものを保存するためのオプションがあります。
ヒント#4合計カテゴリを作成するには、一時的にデータを2倍にします。元のクローンは、トータルカテゴリとして再ラベル付けされています。あなたは余分な計算をする必要があるかもしれませんが、ロケット科学には何もありません。スマートな高校生がそれを理解することができます。ここでは、行単位の調査の具体的な例を説明します。
. preserve
. local Np1 = _N + 1
. expand 2
(2,246 observations created)
. replace race = 4 in `Np1'/L
(2,246 real changes made)
. label def racelbl 4 "Total", modify
. drop pcsingle
. egen pcsingle = mean(100 * (1 - married)), by(collgrad race)
. char pcsingle[varname] "% single"
. format pcsingle %2.1f
. gen istotal = race == 4
. bysort collgrad istotal: gen total = _N
. * for percents of the global total, we need to correct for doubling up
. scalar alltotal = _N/2
. * the table shows percents for college & race | collgrad and for collgrad | total
. bysort collgrad race : gen pc = 100 * cond(istotal, total/alltotal, _N/total)
. format pc %2.1f
. char pc[varname] "Percent"
. groups collgrad race pcsingle pc , show(f) subvarname sepby(collgrad istotal)
+-------------------------------------------------------+
| college? race % single Percent Freq. |
|-------------------------------------------------------|
| not college grad white 29.2 71.0 1217 |
| not college grad black 53.3 28.0 480 |
| not college grad other 29.4 1.0 17 |
|-------------------------------------------------------|
| not college grad Total 35.9 76.3 1714 |
|-------------------------------------------------------|
| college grad white 31.4 78.9 420 |
| college grad black 51.5 19.4 103 |
| college grad other 33.3 1.7 9 |
|-------------------------------------------------------|
| college grad Total 35.3 23.7 532 |
+-------------------------------------------------------+
セパレータラインを追加するために明示的に表示されていない変数を使用する余分なトリックに注意してください。
「結婚した人」の割合は?レース? 'collgrad'? 「みんな」?またはそれらの2または3または4? –
@nick与えられたパーセント 'collgrad'。白、単一、大学卒業者ではない人の%のようなものは '355 * 100 /(355 + 862)'です。 1人の非大学生グラデーションの割合は(競走に関係なく)、616 * 100 /(606 + 1098)です。 'bys collgrad:table married race、col'の結果に似ていますが、' bys'で与えられたテーブルのように2つのテーブルではありません。 – amo