開始使用免費開始

蜜蜂精子數的自助法(bootstrap)假設檢定

現在,你要檢定下列假設:平均而言,施用新菸鹼類殺蟲劑的雄蜂,其每毫升精液中的活精子數與未施用的雄蜂相同。你將以「平均數之差」作為檢定統計量。

供你參考,你在第 2 章撰寫的 draw_bs_reps() 函式呼叫介面為 draw_bs_reps(data, func, size=1)(連結:https://campus.datacamp.com/courses/statistical-thinking-in-python-part-2/bootstrap-confidence-intervals?ex=6)。

本練習屬於課程

Statistical Thinking in Python(第 2 部分)

檢視課程

練習說明

  • 計算 control 的活精子平均數減去 treated 的活精子平均數。
  • 計算所有活精子數的總平均。做法是先將 controltreated 串接,再對串接後的陣列取平均。
  • 分別為 controltreated 產生平移後的資料集,讓兩個資料集具有相同平均值。這部分已替你完成。
  • 使用你的 draw_bs_reps() 函式,對這兩個平移後的陣列各產生 10,000 次「平均數」的自助法重抽樣。
  • 計算「平均數之差」的自助法重抽樣結果。
  • 用來計算並列印 p 值的程式碼已寫好。按下送出即可查看結果!

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Compute the difference in mean sperm count: diff_means
diff_means = ____

# Compute mean of pooled data: mean_count
mean_count = ____

# Generate shifted data sets
control_shifted = control - np.mean(control) + mean_count
treated_shifted = treated - np.mean(treated) + mean_count

# Generate bootstrap replicates
bs_reps_control = ____(____,
                       np.mean, size=10000)
bs_reps_treated = ____(____,
                       np.mean, size=10000)

# Get replicates of difference of means: bs_replicates
bs_replicates = ____

# Compute and print p-value: p
p = np.sum(bs_replicates >= np.mean(control) - np.mean(treated)) \
            / len(bs_replicates)
print('p-value =', p)
編輯並執行程式碼