隨機化的機率密度
重複 100 次可以讓你理解置換(permuting)的運作機制。不過,100 次不足以觀察在虛無假設下比例差異的完整可能值範圍。
回顧推論的四個步驟。這四個步驟會在本課程以及未來的統計推論課程中的所有推論練習中使用。利用函式名稱來幫助你記住分析流程。
specify用來指定反應變數與解釋變數。hypothesize用來宣告虛無假設。generate用來產生重抽樣、置換或模擬。calculate用來計算摘要統計量。
在這個練習中,你會將上述流程重複 1000 次,以掌握在虛無假設下比例差異的完整分配情形。
本練習屬於課程
R 統計推論基礎
練習說明
已為你載入 dplyr、ggplot2、NHANES 與 infer 套件。
- 使用
infer語法,透過隨機打亂HomeOwn變數來產生 1000 個比例差異。回想infer的語法:specify指定你關注的關係是HomeOwn與Gender,而此情境中的成功為擁有自宅,success = "Own"。hypothesize假設虛無假設為真,設定null = "independence"(代表性別與是否擁有自宅無關)。generate進行 1000 次置換;將reps設為 1000。calculate計算統計量stat = "diff in props",並使用順序c("male", "female")。
- 執行密度圖的程式碼,產生平滑化的差異分配視覺化。這條曲線是什麼形狀?
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Perform 1000 permutations
homeown_perm <- homes %>%
# Specify HomeOwn vs. Gender, with `"Own" as success
___(___ ~ ___, success = "___") %>%
# Use a null hypothesis of independence
___(___) %>%
# Generate 1000 repetitions (by permutation)
___(reps = ___, type = "permute") %>%
# Calculate the difference in proportions (male then female)
___(___, order = ___))
# Density plot of 1000 permuted differences in proportions
ggplot(homeown_perm, aes(x = stat)) +
geom_density()