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?

PythonでT検定を実施する。

Last updated at Posted at 2025-01-04

「日本統計学会公式認定 統計検定データサイエンス基礎対応 データアナリティクス基礎」のレビューを読んでいたら「そもそもPythonでよくね?」みたいなコメントがちらほらと見受けられた。よって冬休みの宿題よろしく、「例題7 異なる授業形態による成績データ」をPythonで解いてみる。

パッケージとライブラリをインポートする

まずは必要なライブラリと拡張機能をすべてインポートする。

import numpy as np
import pandas as pd
from scipy import stats
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN

データを読み込み、中身を確認しておく

sample_data = pd.read_csv("例題7_異なる授業形態による成績データ.csv", encoding="shift-jis")
sample_data = sample_data.dropna()
print(sample_data.head(10))
print(sample_data.shape)
print(sample_data.info())

# オンラインと対面でデータを分けておく。
cf_online = sample_data[sample_data['授業形態'] == "オンライン"]
cf_face = sample_data[sample_data['授業形態'] == "対面"]

7-1:オンライン授業の受講者の成績に関して、母平均の推定値

cf_online_mean = cf_online['成績(点)'].mean()
print(Decimal(cf_online_mean).quantize(Decimal('0.1'), ROUND_HALF_UP))
出力例
58.9

7-2:母標準偏差の推定値を不偏分散に基づいて求める

estimated_standard_deviation = np.std(cf_online['成績(点)'], ddof=1)
print(Decimal(estimated_standard_deviation).quantize(Decimal('0.01'), ROUND_HALF_UP))
出力例
3.16

7-3:オンライン授業の受講者の成績に関して、標本平均の標準誤差

estimated_standard_error = estimated_standard_deviation / np.sqrt(len(cf_online))
print(Decimal(estimated_standard_error).quantize(Decimal('0.1'), ROUND_HALF_UP))
出力例
0.5

7-4:オンライン授業の受講者の成績に関して、母平均のt分布に基づく信頼度95%の信頼区間の上限

bottom, up = stats.t.interval(0.95,loc=cf_online_mean,
                 scale=estimated_standard_error,
                 df=len(cf_online))
print(Decimal(up).quantize(Decimal('0.1'), ROUND_HALF_UP))
出力例
59.9

7-5:仮説検定

P値が0.05よりはるかに小さいので帰無仮説が棄却され、対立仮説が採用される。

print(stats.ttest_ind(a=cf_online['成績(点)'],
                 b=cf_face['成績(点)'],
                 equal_var=False))
出力例
TtestResult(statistic=6.078296850332994, pvalue=4.235040588884201e-08, df=77.72447906876936)

以上。

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?