第6回 統計的仮説検定
関西大学総合情報学部
2023-10-26
すぐに実習できるように準備しておきましょう。
Gacha.csv
)をダウンロードしておいてください。Data
フォルダーを作成し、そこにアップロードしましょう。.R
)、またはQuartoファイル(.qmd
)の保存先はData
フォルダーでなく、プロジェクトフォルダーです。あるソシャゲにおいて★5のキャラクター(SSR)をガチャで引ける確率は5%であると知られている。そこでプレイヤー100人が行った4772回分のガチャ記録(\(n\) = 4772)を入手し、調べた結果、SSRは227回(= 4.757%)出現した。SSRが引けた場合1、引けなかった場合を0とした場合、標本不偏分散の平方根(\(u\))は0.213であった。約0.243%ポイントの差(= 5 - 4.757)ではあるものの、怪しい。このガチャからSSRが引ける確率は本当に5%だろうか。
レア度 | 獲得確率 |
---|---|
N (★★) | 55% |
R (★★★) | 25% |
SR (★★★★) | 15% |
SSR (★★★★★) | 5% |
レア度 | 出現回数 (枚) | 割合 (%) | 累積割合 (%) |
---|---|---|---|
N | 2657 | 55.7 | 55.7 |
R | 1188 | 24.9 | 80.6 |
SR | 700 | 14.7 | 95.2 |
SSR | 227 | 4.8 | 100.0 |
計 | 4772 | 100.0 |
prop.test()
)についてはサポートページの講義資料を参照すること。変数 | 説明 |
---|---|
player | プレイヤーのID |
trial | 当該プレイヤーの何番目のガチャか |
stone_type | 使用した石のタイプ (Free: 無料石、Paid: 有償石) |
result | ガチャの結果 (N, R, SR, SSR) |
if_else()
使用)gacha_df <- gacha_df |>
mutate(result_N = if_else(result == "N", 1, 0),
result_R = if_else(result == "R", 1, 0),
result_SR = if_else(result == "SR", 1, 0),
result_SSR = if_else(result == "SSR", 1, 0))
gacha_df
# A tibble: 4,772 × 8
player trial stone_type result result_N result_R result_SR result_SSR
<dbl> <dbl> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 1 1 Paid R 0 1 0 0
2 1 2 Paid R 0 1 0 0
3 1 3 Paid SR 0 0 1 0
4 1 4 Free R 0 1 0 0
5 1 5 Paid N 1 0 0 0
6 1 6 Free N 1 0 0 0
7 1 7 Free SSR 0 0 0 1
8 1 8 Free N 1 0 0 0
9 1 9 Free N 1 0 0 0
10 1 10 Free N 1 0 0 0
# ℹ 4,762 more rows
library(fastDummies)
gacha_df <- gacha_df |>
dummy_cols(select_columns = "result", ignore_na = TRUE)
gacha_df
# A tibble: 4,772 × 8
player trial stone_type result result_N result_R result_SR result_SSR
<dbl> <dbl> <chr> <chr> <int> <int> <int> <int>
1 1 1 Paid R 0 1 0 0
2 1 2 Paid R 0 1 0 0
3 1 3 Paid SR 0 0 1 0
4 1 4 Free R 0 1 0 0
5 1 5 Paid N 1 0 0 0
6 1 6 Free N 1 0 0 0
7 1 7 Free SSR 0 0 0 1
8 1 8 Free N 1 0 0 0
9 1 9 Free N 1 0 0 0
10 1 10 Free N 1 0 0 0
# ℹ 4,762 more rows
仮説: SSRキャラクターの本当の出現確率(=母平均\(\mu\))は5%(=0.05)ではない。\(\Rightarrow \mu \neq 0.05\)
「どんな時に帰無仮説を棄却するか」について考える段階
標本平均を変形した\(T = \frac{\bar{x} - \mu}{\text{SE}}\)は自由度\(n-1\)の\(t\)分布に従う。
平均値に関する統計的仮説検定の場合、検定統計量として標本平均(\(\bar{x}\))よりも\(T\)統計量を使用。
\[ t = \frac{\bar{x} - \mu}{\text{SE}} = \frac{\bar{x} - \mu_0}{\text{SE}} = \frac{\bar{x} - \mu_0}{\frac{s}{\sqrt{n}}} \]
x_bar <- mean(gacha_df$result_SSR, na.rm = TRUE) # 標本平均
n <- sum(!is.na(gacha_df$result_SSR)) # サンプルサイズ
se <- sd(gacha_df$result_SSR, na.rm = TRUE) / sqrt(n) # 標準誤差
(x_bar - 0.05) / se # T統計量
[1] -0.7888289
参考)今回の\(p\)値は0.431
「検定統計量がこの領域に入ると帰無仮説は棄却される」における「領域」を決める段階
[1] -1.960461
[1] 1.960461
帰無仮説の棄却有無を判断する2つの方法
いずれかの方法で判定する。
自分の主張が支持される = 対立仮説が支持される = \(|t|\)が大きい = \(p\)が小さい
刑事裁判における無罪推定の原則
t.test()
関数を使用
mu
引数には帰無仮説上の母数(\(\mu_0\))を指定する。
One Sample t-test
data: gacha_df$result_SSR
t = -0.78883, df = 4771, p-value = 0.4303
alternative hypothesis: true mean is not equal to 0.05
95 percent confidence interval:
0.04152782 0.05361049
sample estimates:
mean of x
0.04756915
prop.test()
関数を使用: 詳細は講義資料を参照すること。
# x: SSRが得られた回数
# n: ガチャの回数
# p: 帰無仮説におけるSSRの出現確率 (0 ~ 1)
prop.test(x = 227, n = 4772, p = 0.05, correct = FALSE)
1-sample proportions test without continuity correction
data: 227 out of 4772, null probability 0.05
X-squared = 0.59364, df = 1, p-value = 0.441
alternative hypothesis: true p is not equal to 0.05
95 percent confidence interval:
0.04188537 0.05398077
sample estimates:
p
0.04756915