0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

# 2つのデータの独立性の検定 ( χ2検定 )

Posted at

χ2検定

観測されたデータの分布は,理論値の分布とほぼ同じとみなせるかを調べるための検定.

χ2検定の検定統計量

 χ2の実現値 = \frac{(観測度数 - 期待値)^2}{期待値} の総和
  • 期待度数と観測度数が完全に一致すれば,χ2値は0になる
  • 期待度数と観測度数の差が大きくなると,χ2値も大きくなる

サイコロの目の分布

data <- read.table("./r_sample/chap07/sec01/dice/サイコロ.txt", header=T)

freq <- c(
  data[1, 2],
  data[2, 2],
  data[3, 2],
  data[4, 2],
  data[5, 2],
  data[6, 2]
 )

# 統計検定量を求める
element <- (freq -2)^2 / 2
elm_val <- sum(element)  # 5.435

# 有意水準5%の値を求める
chi_val <- qchisq(0.05, 5, lower.tail=FALSE) # 11.07

この結果,検定統計量の実現値は棄却域を超えていなかった.
サイコロ投げ12回の目の出方は偶然に起こり得る範囲内だと言える.

2店舗のメインメニュー,セカンドメニューの売り上げ数の割合


data <- read.table("./r_sample/chap07/sec01/chi_square_test/A店B店.txt", header=T)

A_sum <- data[1,3]
B_sum <- data[2,3]
AB_sum <- data[3,3]

menu1_sum <- data[3,1]
menu2_sum <- data[3,2]
menu_sales <- c(
  data[1,1],
  data[1,2],
  data[2,1],
  data[2,2]
)

exp_A_m1 <- menu1_sum * A_sum / AB_sum #A店メニュー2の期待値
exp_A_m2 <- menu2_sum * A_sum / AB_sum #A店メニュー2の期待値
exp_B_m1 <- menu1_sum * B_sum / AB_sum #B店メニュー1の期待値
exp_B_m2 <- menu2_sum * B_sum / AB_sum #B店メニュー2の期待値

exp_freq <- c(exp_A_m1, exp_A_m2, exp_B_m1, exp_B_m2)

chi_element <- (menu_sales - exp_freq) ^2 / exp_freq
chi_elm_val <- sum(chi_element)
chi_val <- qchisq(0.05, 1, lower.tail = FALSE)

curve(dchisq(x, 1), 0, 6)
abline(v=qchisq(0.05, 1, lower.tail = FALSE))

検定統計量の実現値は4.55であった.有意水準5%の値である3.84を超えているので,このデータ分布は「めったに起こらない」と言える.つまり,2店舗の注文数には差がある.

スクリーンショット 2020-03-09 10.20.54.png
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?