機率有多大?

R 統計學入門

Maggie Matsui

Content Developer, DataCamp

衡量機率

事件的機率是什麼?

$$ P(\text{event}) = \frac{\text{事件可發生的方式數}}{\text{所有可能結果總數}} $$

例子:擲硬幣

$$ P(\text{heads}) = \frac{\text{出現正面 1 種方式}}{\text{2 種可能結果}} = \frac{1}{2} = 50\%$$

機率數線。0% = 不可能,100% = 一定發生

R 統計學入門

指派業務人員

盒中有 Amir、Brian、Claire、Damian 的名字

R 統計學入門

指派業務人員

抽出 Brian 的名字

$$P(\text{Brian}) = \frac{1}{4} = 25\%$$

R 統計學入門

從資料框抽樣

sales_counts
   name  n_sales
 1 Amir      178
 2 Brian     126
 3 Claire     75
 4 Damian     69
sales_counts %>%
  sample_n(1)
   name  n_sales
 1 Brian     126
sales_counts %>%
  sample_n(1)
   name  n_sales
 1 Claire     75
R 統計學入門

設定隨機種子

set.seed(5)

sales_counts %>% sample_n(1)
   name  n_sales
 1 Brian     126
set.seed(5)

sales_counts %>% sample_n(1)
   name  n_sales
 1 Brian     126
R 統計學入門

第二場會議

不放回抽樣

盒中有 Amir、Claire、Damian

R 統計學入門

第二場會議

抽出 Claire 的名字

$$P(\text{Claire}) = \frac{1}{3} = 33\%$$

R 統計學入門

在 R 中抽兩次樣本

sales_counts %>%
  sample_n(2)
   name  n_sales
 1 Brian     126
 2 Claire     75
R 統計學入門

有放回抽樣

手伸進盒子抽出 Brian 的名字,再放回去的 GIF

R 統計學入門

有放回抽樣

Screen Shot 2020-04-28 at 5.21.54 PM.png

$$P(\text{Claire}) = \frac{1}{4} = 25\%$$

R 統計學入門

在 R 中進行有放回抽樣

sales_counts %>%
  sample_n(2, replace = TRUE)
   name  n_sales
 1 Brian     126
 2 Claire     75

5 場會議:

sample(sales_team, 5, replace = TRUE)
   name  n_sales
 1 Brian     126
 2 Claire     75
 3 Brian     126
 4 Brian     126
 5 Amir      178
R 統計學入門

獨立事件

若兩個事件是獨立的,則第二個事件的機率不會受第一個事件結果影響。

兩欄:第一次抽有 Amir、Brian、Claire、Damian。第二次抽的欄位空白

R 統計學入門

獨立事件

若兩個事件是獨立的,則第二個事件的機率不會受第一個事件結果影響。

 

有放回抽樣 = 每次抽取彼此獨立

第一次抽的每個名字都有箭頭指向第二次抽的 Claire,機率為 25%

R 統計學入門

相依事件

若兩個事件是相依的,則第二個事件的機率受第一個事件結果影響。

兩欄:第一次抽有 Amir、Brian、Claire、Damian。第二次抽的欄位空白

R 統計學入門

相依事件

若兩個事件是相依的,則第二個事件的機率受第一個事件結果影響。

第一欄的 Claire 指向第二欄的 Claire,機率為 0%

R 統計學入門

相依事件

若兩個事件是相依的,則第二個事件的機率受第一個事件結果影響。

 

不放回抽樣 = 每次抽取彼此相依

第一欄的 Amir、Brian、Damian 都指向第二欄的 Claire,機率為 33%

R 統計學入門

一起來練習吧!

R 統計學入門

Preparing Video For Download...