第11章 相関のグラフ
2変数がどのように相関しているのかを把握する場合には、連続変数と連続変数の間では
- 散布図
を、離散変数と連続変数の場合は
- ジッター
- 箱ひげ図
- バイオリンプロット
などを使います。
## Parsed with column specification:
## cols(
## statea = col_double(),
## stateb = col_double(),
## year = col_double(),
## dependa = col_double(),
## dependb = col_double(),
## demauta = col_double(),
## demautb = col_double(),
## allies = col_double(),
## dispute1 = col_double(),
## logdstab = col_double(),
## lcaprat2 = col_double(),
## smigoabi = col_double(),
## opena = col_double(),
## openb = col_double(),
## minrpwrs = col_double(),
## noncontg = col_double(),
## smldmat = col_double(),
## smldep = col_double(),
## dyadid = col_double()
## )
11.1 散布図
散布図を描くにはplot()
にx軸とy軸の変数を指定します。
相互依存度とパワーバランスの関係を見てみます。
ggplot2
ではgeom_point()
を使います。
## Warning: Transformation introduced infinite values in continuous y-axis
11.2 カーネル密度
2次元のカーネル密度を求めてプロットすることもできます。
## Warning: Transformation introduced infinite values in continuous y-axis
## Warning: Removed 7988 rows containing non-finite values (stat_density2d).
11.3 ジッター
x軸がカテゴリカル変数の場合、素直に散布図を描くと、よくわからないことになります。
このようなときは、ジッターをかけると、ましになります。
## Warning: Transformation introduced infinite values in continuous y-axis
11.4 箱ひげ図
しかし、ジッターをかけてもデータポイントが多すぎると分布が直感的にはわからないかもしれません。 そのような場合は箱ひげ図を使います。
smldep
が0の観察を除外しています。
## Warning: Transformation introduced infinite values in continuous y-axis
## Warning: Removed 7988 rows containing non-finite values (stat_boxplot).
- x軸はカテゴリカル変数でないといけないので、
as.factor()
で変換しています。
11.5 バイオリンプロット
同様の趣旨のグラフとしてバイオリンプロットというものもあります。
## Warning: Transformation introduced infinite values in continuous y-axis
## Warning: Removed 7988 rows containing non-finite values (stat_ydensity).
11.6 グループごとの相関
ggplot2
の散布図で色分けをするときはcolour
を使います。
tri %>%
ggplot() +
geom_point(aes(x = lcaprat2, y = smldep, colour = as.factor(dispute1)), alpha = 0.3) +
scale_y_log10()
## Warning: Transformation introduced infinite values in continuous y-axis
箱ひげ図の場合はfill
です。
tri %>%
ggplot() +
geom_boxplot(aes(x = as.factor(smldmat), y = smldep, fill = as.factor(dispute1))) +
scale_y_log10()
## Warning: Transformation introduced infinite values in continuous y-axis
## Warning: Removed 7988 rows containing non-finite values (stat_boxplot).
11.7 単回帰
単回帰(正確に言えば説明変数の種類が1つの)分析結果はgeom_smooth()
で表示できます。
## Warning: Transformation introduced infinite values in continuous y-axis
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
## Warning: Removed 7988 rows containing non-finite values (stat_smooth).
- デフォルトではLoessになっています。
se = FALSE
で信頼区間を表示しないことができます。
線形回帰の場合はmethod
を変えます。
## Warning: Transformation introduced infinite values in continuous y-axis
## `geom_smooth()` using formula 'y ~ x'
## Warning: Removed 7988 rows containing non-finite values (stat_smooth).
散布図と重ねることもできます。
tri %>%
ggplot() +
geom_point(aes(x = lcaprat2, y = smldep), alpha = 0.3) +
geom_smooth(aes(x = lcaprat2, y = smldep), method = "lm") +
scale_y_log10()
## Warning: Transformation introduced infinite values in continuous y-axis
## Warning: Transformation introduced infinite values in continuous y-axis
## `geom_smooth()` using formula 'y ~ x'
## Warning: Removed 7988 rows containing non-finite values (stat_smooth).