cor()),有意性検定を実行できる(cor.test())相関(correlation)とは,2つの変数の間にある直線的な関係の強さと方向を表す指標です。
| 相関の方向 | 意味 | 例 |
|---|---|---|
| 正の相関 | 一方が増えると他方も増える | 身長と体重 |
| 負の相関 | 一方が増えると他方は減る | 睡眠時間と疲労度 |
| 無相関 | 2変数に直線的関係がない | 誕生月と学力 |
\[r = \frac{\sum(x_i - \bar{x})(y_i - \bar{y})}{\sqrt{\sum(x_i - \bar{x})^2 \cdot \sum(y_i - \bar{y})^2}}\]
| r | |
|---|---|
| 0.0〜0.2 | ほとんど相関なし |
| 0.2〜0.4 | 弱い相関 |
| 0.4〜0.6 | 中程度の相関 |
| 0.6〜0.8 | 強い相関 |
| 0.8〜1.0 | 非常に強い相関 |
目安であり,分野や文脈によって解釈が変わることに注意。
# iris データ:がく片の長さと花びらの長さの散布図
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
geom_point(color = "steelblue", alpha = 0.6) +
labs(title = "散布図:がく片の長さと花びらの長さ",
x = "がく片の長さ (cm)", y = "花びらの長さ (cm)") +
theme_bw()# 種ごとに色を分ける
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) +
geom_point(alpha = 0.6) +
geom_smooth(method = "lm", se = FALSE) +
scale_color_brewer(palette = "Set1") +
labs(title = "種ごとの散布図と回帰直線",
x = "がく片の長さ (cm)", y = "花びらの長さ (cm)") +
theme_bw()## [1] 0.8717538
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Sepal.Length 1.0000000 -0.1175698 0.8717538 0.8179411
## Sepal.Width -0.1175698 1.0000000 -0.4284401 -0.3661259
## Petal.Length 0.8717538 -0.4284401 1.0000000 0.9628654
## Petal.Width 0.8179411 -0.3661259 0.9628654 1.0000000
# 四捨五入して見やすく
iris |>
select(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width) |>
cor() |>
round(2)## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Sepal.Length 1.00 -0.12 0.87 0.82
## Sepal.Width -0.12 1.00 -0.43 -0.37
## Petal.Length 0.87 -0.43 1.00 0.96
## Petal.Width 0.82 -0.37 0.96 1.00
相関係数が「偶然ではなく本当に存在する」かどうかを検定します。
##
## Pearson's product-moment correlation
##
## data: iris$Sepal.Length and iris$Petal.Length
## t = 21.646, df = 148, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.8270363 0.9055080
## sample estimates:
## cor
## 0.8717538
Pearson's product-moment correlation
t = 21.646, df = 148, p-value < 2.2e-16
95 percent confidence interval:
0.8270363 0.9055080
sample estimates:
cor
0.8717538
| 項目 | 意味 |
|---|---|
t |
t統計量 |
df |
自由度(n - 2) |
p-value |
p値(< 0.05 なら有意) |
95% CI |
相関係数の95%信頼区間 |
cor |
標本相関係数 r の推定値 |
論文・レポートでの記述例:
がく片の長さと花びらの長さの間には強い正の相関が見られた,r(148) = .87,p < .001。
APA形式での相関係数の書き方: - イタリック体で
r - 括弧内に自由度(= n - 2) - p 値は .001, .01, .05
の水準で報告
相関関係は因果関係を意味しない!
相関があっても,それは「一緒に変化する」ことを示すだけで, 「原因と結果」の関係があるとは限りません。
mtcars
データを使って,馬力(hp)と燃費(mpg)の散布図を作成してください。
- 回帰直線(geom_smooth(method = "lm"))を追加する -
適切なタイトル・軸ラベルをつける
mtcars の hp と mpg
の相関係数を計算してくださいcor.test()
で有意性を検定し,結果を解釈してくださいmtcars の mpg, hp,
wt,
qsec(0〜1/4マイルタイム)の相関行列を作成し,
どの変数の組み合わせが最も強い相関を持つか調べてください。
演習2の結果を,論文・レポートの記述として1〜2文でまとめてください。 (例:「〇〇と〇〇の間には,〇〇な相関が見られた,r(〇〇) = .〇〇, p < .〇〇。」)
cor() で相関係数,cor.test()
で有意性検定