開始使用免費開始

多重比較問題

當研究者反覆檢查不同變數/樣本彼此之間是否達到顯著性時,就會產生多重比較問題。僅靠隨機機會,我們也預期偶爾會出現具有統計顯著性的結果。

在這個練習中,你將使用德州奧斯汀市公部門員工的薪資資料,並把這些薪資與隨機生成的資料相比較。你會觀察到這些隨機資料在解釋員工薪資時,有多常被判定為「顯著」。顯然,這種「顯著性」是偽相關,因為隨機數字對解釋任何事情其實都沒有幫助!

已為你載入一個警察人員薪資的 DataFrame(police_salaries_df),以及 pandas(作為 pd)、NumPy(作為 np)、Matplotlib(作為 plt),和 SciPy 的 stats。

本練習屬於課程

Python 推論基礎

檢視課程

練習說明

  • 將資料集中人數(每一列代表一個人)存到 n_rows,並將顯著結果的計數 n_significant 初始化為 0。
  • 撰寫一個執行 1000 次的 for 迴圈,每次產生 n_rows 個隨機數字。
  • 計算這些隨機生成的數字與警察人員薪資之間的 Pearson's R 與其對應的 p 值。
  • 若 p 值在 5% 顯著水準下達顯著,使用 += 運算子將 n_significant 加一。

動手互動練習

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

# Compute number of rows and initialize n_significant
n_rows = ____
n_significant = ____

# For loop which generates n_rows random numbers 1000 times
for i in ____:
  random_nums = np.random.uniform(size=____)
  # Compute correlation between random_nums and police salaries
  r, p_value = stats.____(____, random_nums)
  # If the p-value is significant at 5%, increment n_significant
  if ____ < ____:
    ____ += 1
    
print(n_significant)
編輯並執行程式碼