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?

統計検定2級 一元配置分散分析

Posted at

はじめに

今回は統計検定2級で出てくる一元配置分散分析を学習します。

✅ 一元配置分散分析とは?

グループごとの平均に差があるかどうかを、1つの要因(分類軸)に注目して調べる統計手法です。

📘 例題:一元配置分散分析(one-way ANOVA)

あなたは3つのカフェ(A、B、C)でケーキを食べ比べ、
それぞれのカフェで3人ずつの友達がケーキの満足度(10点満点)をつけました。

以下は各カフェでのスコアです:

カフェ 点数(満足度)
A 7, 8, 6
B 6, 5, 4
C 8, 9, 10

❓ 問題

カフェによってケーキの満足度の「平均」に差があるかどうか、
一元配置分散分析(one-way ANOVA)を使って検定しなさい。


🧠 解き方

① グループごとの平均を計算

  • Aの平均:$(7+8+6)/3 = 7.0$
  • Bの平均:$(6+5+4)/3 = 5.0$
  • Cの平均:$(8+9+10)/3 = 9.0$

② F値を求める考え方

F値の式は次のとおり:

$$
F = \frac{\text{グループ間のばらつき}}{\text{グループ内のばらつき}}
$$

  • グループ間で平均が大きく違えば → 分子が大きくなる
  • 各グループの中のバラつきが小さければ → 分母が小さくなる
  • → よって F値は大きくなり、「差がある」と判断されやすい

③ 仮に計算結果がこうだったとする:

  • F = 9.0
  • p値 = 0.02(※これは5%未満)

✅ 結論

  • p < 0.05 なので、帰無仮説は棄却
  • 「カフェによって満足度の平均に有意な差がある」と言える
# 📌 Google Colab専用:日本語フォントのインストール
!apt-get -y install fonts-ipafont-gothic > /dev/null

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import seaborn as sns
import pandas as pd
import scipy.stats as stats

# 日本語フォント設定
jp_font = FontProperties(fname="/usr/share/fonts/opentype/ipafont-gothic/ipag.ttf")
plt.rcParams['font.family'] = jp_font.get_name()

# データ
cafe_A = [7, 8, 6]
cafe_B = [6, 5, 4]
cafe_C = [8, 9, 10]

# ANOVA
f_statistic, p_value = stats.f_oneway(cafe_A, cafe_B, cafe_C)

# 結果表示
print(f"F値: {f_statistic:.3f}")
print(f"p値: {p_value:.3f}")
if p_value < 0.05:
    print("⇒ グループ間に有意な差があります。")
else:
    print("⇒ グループ間に有意な差は見られません。")

# 可視化データ
df = pd.DataFrame({
    '満足度スコア': cafe_A + cafe_B + cafe_C,
    'カフェ': ['A']*3 + ['B']*3 + ['C']*3
})

# 箱ひげ図(日本語ラベルを明示的にフォント指定)
plt.figure(figsize=(8, 5))
sns.boxplot(x='カフェ', y='満足度スコア', data=df)
plt.title('カフェごとのケーキ満足度(箱ひげ図)', fontproperties=jp_font)
plt.xlabel('カフェ', fontproperties=jp_font)
plt.ylabel('満足度スコア', fontproperties=jp_font)
plt.show()

image.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?