3.5 カテゴリ変数の要約

一方でカテゴリ変数は、代表値や分散によって含意を得るのではなく、頻度のカウント(集計)や、クロス集計を用いることが多い。これにより、各カテゴリにどれぐらいの観測数があるのかを確認することが可能になる。カテゴリ変数の内容(出現頻度)の確認には、table() 関数を用いる。また、with()関数を用いて同様の結果を得ることも可能である。ここでは、先ほど用いた firmdata から2018年度の情報を抽出し、日経業種に基づく産業の違いから、どのカテゴリの企業がどれだけデータ内にいるのかを確認する。なお、tidyverseを起動していない場合には、必要に応じて library(tidyverse) を事前に指示してほしい。

firm2018 <- firmdata %>% 
  filter(fyear == 2018)

table(firm2018$ind_en)
## 
##       Air Transportation       Amusement Services          Bakery Products 
##                        8                        4                        1 
##   Communication Services Cosmetics & Toilet Goods        Department Stores 
##                        2                        3                        7 
##               Foods, NEC          Home & Pre-Fabs                   Hotels 
##                        1                        2                        5 
##   Miscellaneous Services Miscellaneous Wholesales           Motor Vehicles 
##                       27                        2                        4 
##       Musical Instrument         Railroad (Major)         Railroad (Minor) 
##                        1                       27                        2 
##      Real Estate - Sales       Retail Stores, NEC       Supermarket Chains 
##                        1                       35                       14 
##                 Trucking 
##                        1
with(firm2018, table(ind_en))

また、table関数にて2つのカテゴリ変数を指定することで、両変数に対応するカテゴリの出現頻度を返してくれる。このような表のことをクロス集計表とよぶ。例えば、同データにおける広告集中的な企業を把握するため、広告集中度が中央値よりも高ければ1、それ以外であれば0を取るダミー変数(6.2節参照)を作成し、各産業カテゴリとの関係を確認する。

firm2018 <- firm2018 %>% 
  mutate(ad_dummy = ifelse(adint > median(adint),1, 0))
with(firm2018, table(ind_en,ad_dummy))
##                           ad_dummy
## ind_en                      0  1
##   Air Transportation        4  4
##   Amusement Services        4  0
##   Bakery Products           0  1
##   Communication Services    1  1
##   Cosmetics & Toilet Goods  0  3
##   Department Stores         0  7
##   Foods, NEC                0  1
##   Home & Pre-Fabs           0  2
##   Hotels                    5  0
##   Miscellaneous Services   17 10
##   Miscellaneous Wholesales  1  1
##   Motor Vehicles            0  4
##   Musical Instrument        0  1
##   Railroad (Major)         27  0
##   Railroad (Minor)          2  0
##   Real Estate - Sales       0  1
##   Retail Stores, NEC       11 24
##   Supermarket Chains        2 12
##   Trucking                  1  0

上の表では、各行に産業名が記載されており、その右隣に、広告集中度が低い(ad_dummy=0)企業数が、さらにその右隣には広告集中度が高い(ad_dummy=1)企業数がそれぞれ記載されている。これらのデータを確認すると、鉄道会社やアミューズメント、ホテル、トラック運送業において広告集中度が高い企業が少ないことがわかる。それ以外では産業内でも広告集中度の高い企業と低い企業とが比較的バラけている。

特定のカテゴリに着目して、カテゴリ変数についての集計を行うことも可能である。例えば、広告集中度が高い企業における産業のばらつきを調べたいときには、filter() 関数を用いれば良い。

firm2018 %>% 
  filter(ad_dummy == 1) %>% 
  with(table(ind_en))
## ind_en
##       Air Transportation          Bakery Products   Communication Services 
##                        4                        1                        1 
## Cosmetics & Toilet Goods        Department Stores               Foods, NEC 
##                        3                        7                        1 
##          Home & Pre-Fabs   Miscellaneous Services Miscellaneous Wholesales 
##                        2                       10                        1 
##           Motor Vehicles       Musical Instrument      Real Estate - Sales 
##                        4                        1                        1 
##       Retail Stores, NEC       Supermarket Chains 
##                       24                       12

カテゴリ変数と量的変数の関係を調べることも、グループ別に量的変数の要約を行う形で可能である。具体的には、 group_by() 関数を用いる。group_by() は関数内で指定した変数を用いてデータをグループ化し、グループごとの集計や処理を可能にする関数である。例えば、売上高と広告集中度の平均と標準偏差を産業ごとに確認することは、以下のような指示で可能になる。

firm2018 %>% 
  group_by(ind_en) %>% 
  summarize(obs = n(),
            sales_m = mean(sales),
            sales_sd = sd(sales),
            adint_m = mean(adint),
            adint_sd = sd(adint))
## # A tibble: 19 × 6
##    ind_en                     obs  sales_m sales_sd adint_m adint_sd
##    <chr>                    <int>    <dbl>    <dbl>   <dbl>    <dbl>
##  1 Air Transportation           8 1772786.  305240. 0.00311  0.00333
##  2 Amusement Services           4  298138.  263017. 0        0      
##  3 Bakery Products              1 1059442       NA  0.0122  NA      
##  4 Communication Services       2  547088.  172736. 0.0231   0.0327 
##  5 Cosmetics & Toilet Goods     3  140669.  100063. 0.108    0.0498 
##  6 Department Stores            7  843248.  348819. 0.0195   0.00535
##  7 Foods, NEC                   1  504153       NA  0.0229  NA      
##  8 Home & Pre-Fabs              2 4143505        0  0.00857  0      
##  9 Hotels                       5   62135.   58060. 0        0      
## 10 Miscellaneous Services      27  311867.  456037. 0.0114   0.0204 
## 11 Miscellaneous Wholesales     2  176520    52778. 0.0203   0.0287 
## 12 Motor Vehicles               4 5279122. 4233188. 0.0254   0.00404
## 13 Musical Instrument           1  434373       NA  0.0443  NA      
## 14 Railroad (Major)            27 1302921. 1037834. 0        0      
## 15 Railroad (Minor)             2  260502        0  0        0      
## 16 Real Estate - Sales          1 1861195       NA  0.0114  NA      
## 17 Retail Stores, NEC          35  571019.  547247. 0.0243   0.0272 
## 18 Supermarket Chains          14 4335164. 3511347. 0.0147   0.00782
## 19 Trucking                     1 1118094       NA  0       NA

このように、カテゴリごとの量的変数の要約も実行可能である。なお、標準偏差が NA となっている箇所は、観測数が 1 であり、標準偏差を計算できない状況を表している。