參數估計

R 中的混合模型

Victor Medina

Researcher at The University of Edinburgh

head(data)
         x
1 3.294453
2 5.818586
3 2.380493
4 4.415913
5 5.048659
6 4.750195

R 中的混合模型

假設

  1. 哪種分佈?$\rightarrow$ 常態分佈 $\checkmark$
  2. 幾個叢集?$\rightarrow$ 2 個 $\checkmark$
  3. 哪些參數
    • 2 個平均
    • 2 個比例
    • 2 個 sd $\rightarrow$ 皆為 1 $\checkmark$

$\Rightarrow$ 需估計 4 個參數!(2 個平均與 2 個比例)

R 中的混合模型

1 已知機率 $\rightarrow$

估計平均與比例

2 已知平均與比例 $\rightarrow$

估計機率

R 中的混合模型

步驟 1:已知機率

head(data_with_probs)
         x prob_red prob_blue
1 3.294453     0.64      0.36
2 5.818586     0.01      0.99
3 2.380493     0.92      0.08
4 4.415913     0.16      0.84
5 5.048659     0.05      0.95
6 4.750195     0.09      0.91

R 中的混合模型

針對平均

means_estimates <- data_with_probs %>%
   summarise(mean_red = sum(x * prob_red) / sum(prob_red),
             mean_blue = sum(x * prob_blue) / sum(prob_blue))
means_estimates
  mean_red mean_blue
1  2.86925  5.062976

針對比例

proportions_estimates <- data_with_probs %>% 
   summarise(proportion_red = mean(prob_red),
             proportion_blue = 1 - proportion_red)
proportions_estimates
  proportion_red proportion_blue
1          0.305           0.695
R 中的混合模型

R 中的混合模型

步驟 2:已知平均與比例

R 中的混合模型

R 中的混合模型

R 中的混合模型

R 中的混合模型

步驟 2:縮放後的機率

$\text{Probability}_{\text{ blue}}=\frac{0.065}{0.115 + 0.065}=0.36$

data %>% 
   mutate(prob_from_red = 0.3 * dnorm(x, mean = 3),
          prob_from_blue = 0.7 * dnorm(x,mean = 5),
          prob_red = prob_from_red/(prob_from_red + prob_from_blue),
          prob_blue = prob_from_blue/(prob_from_red + prob_from_blue)) %>% 
   select(x, prob_red, prob_blue) %>% head(4)
         x   prob_red  prob_blue
1 3.294453 0.63733037 0.36266963
2 5.818586 0.01115698 0.98884302
3 2.380493 0.91619343 0.08380657
4 4.415913 0.15721146 0.84278854
R 中的混合模型

重點整理

  • 已知機率時 $\rightarrow$ 估計平均與比例
  • 已知平均與比例時 $\rightarrow$ 估計機率

R 中的混合模型

一起來練習吧!

R 中的混合模型

Preparing Video For Download...