兩種方法求 p-value
你先前已經看過,通常有兩種方式可以得到虛無分佈:以計算模擬,或以數學近似。卡方適合度檢定也不例外。近似的分佈同樣是「卡方分佈」,其自由度等於類別數減 1。
在這個練習中,你會比較這兩種方法,來計算 p-value,用以衡量伊朗第一位數分佈與 Benford 定律的一致性。請注意,你在上一題建立的觀測統計量已儲存在你的工作空間,名稱為 chi_obs_stat。
本練習屬於課程
R 中的類別資料推論
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Compute degrees of freedom
degrees_of_freedom <- ___ %>%
# Pull out first_digit vector
pull("first_digit") %>%
# Calculate n levels and subtract 1
___
# Plot both null dists
___
# Add density layer
___
# Add vertical line at obs stat
___
# Overlay chisq approx
stat_function(fun = dchisq, args = list(df = ___), color = "blue")