第 15 週 13_機率概論
15.1 條件機率
請先看影片介紹
<- 1:6
S <- c(1,3,5)
A <- c(3,4,5)
B <- sum(S %in% B)/sum(S %in% S)
p_B <- sum(B %in% A)/sum(S %in% S)
p_A_and_B <- p_A_and_B/p_B p_A_bar_B
<- 1:500
S <- 1:150
A <- 121:200
C <- sum(S %in% A)/sum(S %in% S)
p_A <- sum(S %in% C)/sum(S %in% S)
p_C <- sum(A %in% C)/sum(S %in% S)
p_C_and_A <- p_C_and_A/p_A
p_C_bar_A <- sum(C %in% A)/sum(S %in% S)
p_A_and_C <- p_A_and_C/p_C p_A_bar_C
library(tidyverse)
<- tibble(
birds id = 1:200,
sex = c(rep("M",70),rep("F",100),rep("M",20),rep("F",10)),
eyes = rep(c("brown","blue"),c(170,30))
)
table(birds$sex,birds$eyes)
<- sum(birds$sex %in% "F")/sum(birds$id %in% birds$id)
p_F
<- sum(birds$sex %in% "M")/sum(birds$id %in% birds$id)
p_M
<- sum(birds$eyes %in% "brown")/sum(birds$id %in% birds$id)
p_brown
<- sum(birds$eyes %in% "blue")/sum(birds$id %in% birds$id)
p_blue
<- sum(subset(birds,sex=="M")$eyes %in% "brown")/sum(birds$id %in% birds$id)
p_M_and_brown
<- sum(subset(birds,sex=="F")$eyes %in% "brown")/sum(birds$id %in% birds$id)
p_F_and_brown <- p_F_and_brown/p_brown
p_F_bar_brown
<- sum(subset(birds,sex=="M")$eyes %in% "blue")/sum(birds$id %in% birds$id)
p_M_and_blue <- p_M_and_blue/p_blue
p_M_bar_blue
<- sum(subset(birds,eyes=="blue")$sex %in% "F")/sum(birds$id %in% birds$id)
p_blue_and_F <- p_blue_and_F/p_F p_blue_bar_F
##
## blue brown
## F 10 100
## M 20 70