OECDパッケージを使ってRで日本の労働生産性のグラフを描く

なんかこれ↓が話題のようなので、とりあえず元データをふわっと探してみました。

図表1には「(資料)文部科学統計要覧、鉱工業指数、国民経済計算」と書かれている。「国民経済計算」というのはこれらしい:

けどなんか探し方がよくわからないので、こういう経済指数的なやつはたぶんOECDにある?と思ってググったらありました。

たぶんRのパッケージもあるでしょ、と思ってググったらこれもありました。

OECDパッケージでデータを取得

とりあえずインストール。

install.packages("OECD")

あとの使い方はREADME通りです。まず、get_datasets()でデータセットを探します。たぶん「productivity」とかで検索すれば引っかかるでしょう。

library(OECD)

# データセットを探す
dataset_list <- get_datasets()
search_dataset("productivity", data = dataset_list)
#>           id
#> 150   PDYGTH
#> 156    LEVEL
#> 219      MFP
#> 390  ULC_EEQ
#> 443     PDBI
#> 972   PDB_LV
#> 973   PDB_GR
#> 974  PDBI_I4
#> 1137   EAMFP
#>                                                                            title
#> 150                              Labour productivity growth in the total economy
#> 156                              Labour productivity levels in the total economy
#> 219                                                    Multi-factor Productivity
#> 390  Unit labour costs and labour productivity (employment based), Total economy
#> 443                                        Productivity by industry (ISIC Rev.3)
#> 972                                     Level of GDP per capita and productivity
#> 973                               Growth in GDP per capita, productivity and ULC
#> 974                  Productivity and ULC by main economic activity (ISIC Rev.4)
#> 1137                           Environmentally Adjusted Multifactor Productivity

よくわかりませんが、Labour productivity growth in the total economyというのがそれっぽいです。このメタデータを取得して絞り込み方を考えます。

dstruc <- get_data_structure("PDYGTH")

dstruc$VAR_DESC
#>            id        description
#> 1         VAR           Variable
#> 2         COU            Country
#> 3        TIME               Time
#> 4   OBS_VALUE  Observation Value
#> 5 TIME_FORMAT        Time Format
#> 6  OBS_STATUS Observation Status

VARはデータの項目です。これもよくわかりませんが、GDP per hour worked index 2005=100というのが求めてるものっぽいです(例の記事のグラフとは基準年が違うのに注意)。 あと、日本のデータだけでいいのでCOUJPNに絞り込みましょう。

dstruc$VAR
#>      id
#> 1  GDPK
#> 2  GDPI
#> 3  HRSA
#> 4  EMPT
#> 5  HRST
#> 6  HRSI
#> 7  PDYK
#> 8  PDYI
#> 9  GDPG
#> 10 GCGR
#> 11 HRSG
#> 12 HCGR
#> 13 PDYG
#> 14 PCGR
#> 15 GPOP
#> 16 GPOG
#> 17 GOGR
#> 18 PDTE
#> 19 PDEG
#> 20 PEGR
#> 21 PDTH
#> 22 HRPO
#> 23 HROG
#> 24 HRGR
#>                                                                      label
#> 1                                  Gross Domestic Product, constant prices
#> 2                                    Gross Domestic Product index 2005=100
#> 3                                  Average hours worked per person engaged
#> 4                             Total employment (number of persons engaged)
#> 5                                        Hours worked for total employment
#> 6                                             Hours worked, index 2005=100
#> 7                  GDP per hour worked, national currency, constant prices
#> 8                                       GDP per hour worked index 2005=100
#> 9                                      Real GDP, annual growth, in percent
#> 10                                 Real GDP, annual compounded growth rate
#> 11                            Hours worked, annual growth rate, in percent
#> 12                 Hours worked, annual compounded growth rate, in percent
#> 13                Real GDP per hour worked, annual growth rate, in percent
#> 14                 Real GDP per hour worked, annual compounded growth rate
#> 15                   GDP per Capita, US dollar, constant prices, 2005 PPPs
#> 16                     Real GDP per capita, annual growth rate, in percent
#> 17          Real GDP per capita, annual compounded growth rate, in percent
#> 18          GDP per person employed, US dollar, constant prices, 2005 PPPs
#> 19            Real GDP per person employed, annual growth rate, in percent
#> 20 Real GDP per person employed, annual compounded growth rate, in percent
#> 21              GDP per hour worked, US dollar, constant prices, 2005 PPPs
#> 22                                                 Hours worked per capita
#> 23                 Hours worked per capita, annual growth rate, in percent
#> 24      Hours worked per capita, annual compounded growth rate, in percent

データを取得します。絞り込み方はよくわからないんですが、filter引数に、list(1つ目の変数の絞り込み, 2つ目の変数の絞り込み, ...)という感じで条件を渡すみたいです。

df <- get_dataset(dataset = "PDYGTH", filter = list("PDYI", "JPN"))

df
#>     VAR COU TIME obsValue
#> 1  PDYI JPN 1970     32.6
#> 2  PDYI JPN 1971     34.0
#> 3  PDYI JPN 1972     36.8
#> 4  PDYI JPN 1973     39.4
#> 5  PDYI JPN 1974     40.2
#> 6  PDYI JPN 1975     42.1
#> 7  PDYI JPN 1976     43.1
#> 8  PDYI JPN 1977     44.4
#> 9  PDYI JPN 1978     46.4
#> 10 PDYI JPN 1979     48.4
#> 11 PDYI JPN 1980     49.6
#> 12 PDYI JPN 1981     51.6
#> 13 PDYI JPN 1982     53.0
#> 14 PDYI JPN 1983     54.0
#> 15 PDYI JPN 1984     55.9
#> 16 PDYI JPN 1985     59.5
#> 17 PDYI JPN 1986     60.8
#> 18 PDYI JPN 1987     63.0
#> 19 PDYI JPN 1988     66.9
#> 20 PDYI JPN 1989     70.2
#> 21 PDYI JPN 1990     74.3
#> 22 PDYI JPN 1991     76.5
#> 23 PDYI JPN 1992     77.5
#> 24 PDYI JPN 1993     79.8
#> 25 PDYI JPN 1994     80.7
#> 26 PDYI JPN 1995     82.6
#> 27 PDYI JPN 1996     84.4
#> 28 PDYI JPN 1997     86.3
#> 29 PDYI JPN 1998     86.7
#> 30 PDYI JPN 1999     89.3
#> 31 PDYI JPN 2000     91.3
#> 32 PDYI JPN 2001     92.9
#> 33 PDYI JPN 2002     94.9
#> 34 PDYI JPN 2003     96.4
#> 35 PDYI JPN 2004     98.8
#> 36 PDYI JPN 2005    100.0
#> 37 PDYI JPN 2006    100.7
#> 38 PDYI JPN 2007    102.4
#> 39 PDYI JPN 2008    102.6
#> 40 PDYI JPN 2009    101.7
#> 41 PDYI JPN 2010    105.8
#> 42 PDYI JPN 2011    105.7
#> 43 PDYI JPN 2012    107.0

TIMEは文字列型になっているので整数型に変換しておきます。

df$TIME <- as.integer(df$TIME)

グラフ

とりあえずグラフを描くとこんな感じです。

library(ggplot2)

p <- ggplot(df, aes(TIME, obsValue)) +
  geom_line() +
  theme_minimal()

p 

f:id:yutannihilation:20180212131415p:plain

例の記事とグラフの範囲を合わせるとこんな感じ。グラフの形状を見る感じこのデータであってそう?

p + 
  xlim(1995, NA) +
  ylim(75, 225)
#> Warning: Removed 25 rows containing missing values (geom_path).

f:id:yutannihilation:20180212131426p:plain

最後

あとはe-Statとか世の中に転がるデータからもっとニュース性の高い疑似相関を見つけるだけ...。楽しみですね(他力本願)。