近似捷徑

R 中的類別資料推論

Andrew Bray

Assistant Professor of Statistics at Reed College

信賴區間

SE
0.009998905
SE_small_n
0.03809731
SE_low_p
0.00547912

當 {{7}} 時,標準誤會變大

  • n 很小
  • p 接近 0.5
R 中的類別資料推論

自助法示意一

R 中的類別資料推論

自助法示意二

R 中的類別資料推論

常態分配

又稱「鐘形曲線」。

  • 觀測值相互獨立
  • n 很大

  • $\hat{p}$ 近似常態分配

常態曲線示意

R 中的類別資料推論

標準差

$$\sqrt{\frac{ \hat{p} \times (1 - \hat{p})}{n}}$$

R 中的類別資料推論

檢視模型假設

如何檢查「觀測值相互獨立」?

  • 這取決於資料蒐集方式。

「n 很大」是多大?

  • $n \times \hat{p} \gt 10$
  • $n \times(1 - \hat{p}) \gt 10$
R 中的類別資料推論

計算標準誤:近似法

p_hat <- gss2016 %>%
  summarize(mean(happy == "HAPPY")) %>%
  pull()
n <- nrow(gss2016)
c(n * p_hat, n * (1 - p_hat))
116  35
SE_approx <- sqrt(p_hat * (1 - p_hat) / n)
SE_approx
0.03418468
R 中的類別資料推論

計算標準誤:電腦模擬

boot <- gss2016 %>%
  specify(response = happy, success = "HAPPY") %>%
  generate(reps = 500, type = "bootstrap") %>%
  calculate(stat = "prop")
SE_boot <- boot %>%
  summarize(sd(stat)) %>%
  pull()
SE_boot
0.03176741
R 中的類別資料推論

抽樣分配

ggplot(boot, aes(x = stat)) +
  geom_density()

密度曲線 1

R 中的類別資料推論

抽樣分配

ggplot(boot, aes(x = stat)) +
  geom_density() +
  stat_function(fun = dnorm, 
                color = "purple",
                args = 
                  list(mean = p_hat,
                       sd = SE_approx))

密度曲線 1

R 中的類別資料推論

抽樣分配

ggplot(boot, aes(x = stat)) +
  geom_density() +
  stat_function(fun = dnorm, 
                color = "purple",
                args = 
                  list(mean = p_hat,
                       sd = SE_approx))

密度曲線 2

R 中的類別資料推論

一起來練習吧!

R 中的類別資料推論

Preparing Video For Download...