第 15 週 13_機率概論

15.1 條件機率

請先看影片介紹

S <- 1:6
A <- c(1,3,5)
B <- c(3,4,5)
p_B <-  sum(S %in% B)/sum(S %in% S) 
p_A_and_B <- sum(B %in% A)/sum(S %in% S)
p_A_bar_B <- p_A_and_B/p_B
S <- 1:500
A <- 1:150
C <- 121:200
p_A <- sum(S %in% A)/sum(S %in% S)
p_C <- sum(S %in% C)/sum(S %in% S)
p_C_and_A <- sum(A %in% C)/sum(S %in% S)
p_C_bar_A <- p_C_and_A/p_A
p_A_and_C <- sum(C %in% A)/sum(S %in% S)
p_A_bar_C <- p_A_and_C/p_C
library(tidyverse)
birds <- tibble(
  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)

p_F <- sum(birds$sex %in% "F")/sum(birds$id %in% birds$id)

p_M <- sum(birds$sex %in% "M")/sum(birds$id %in% birds$id)

p_brown <- sum(birds$eyes %in% "brown")/sum(birds$id %in% birds$id)

p_blue <- sum(birds$eyes %in% "blue")/sum(birds$id %in% birds$id)

p_M_and_brown <- sum(subset(birds,sex=="M")$eyes %in% "brown")/sum(birds$id %in% birds$id)

p_F_and_brown <- sum(subset(birds,sex=="F")$eyes %in% "brown")/sum(birds$id %in% birds$id)
p_F_bar_brown <- p_F_and_brown/p_brown

p_M_and_blue <- sum(subset(birds,sex=="M")$eyes %in% "blue")/sum(birds$id %in% birds$id)
p_M_bar_blue <- p_M_and_blue/p_blue

p_blue_and_F <- sum(subset(birds,eyes=="blue")$sex %in% "F")/sum(birds$id %in% birds$id)
p_blue_bar_F <- p_blue_and_F/p_F
##    
##     blue brown
##   F   10   100
##   M   20    70