# Training dataset
dtf1 <- matrix(c(0,1,0,1,0,
0,0,1,0,0,
1,1,1,0,0,
1,0,1,0,1),
nrow=4, byrow=TRUE)
colnames(dtf1) <- c("X1", "X2", "X3", "X4", "l")
dtf1
# Label l
pl0 <- sum(dtf1[,5] == 0) / nrow(dtf1)
pl1 <- sum(dtf1[,5] == 1) / nrow(dtf1)
# Feature X1
p_x10 <- sum(dtf1[,1] == 0) / nrow(dtf1)
p_x11 <- sum(dtf1[,1] == 1) / nrow(dtf1)
p_x10_l0 <- sum(dtf1[,1] == 0 & dtf1[,5] == 0) / sum(dtf1[,5] == 0)
p_x10_l1 <- sum(dtf1[,1] == 0 & dtf1[,5] == 1) / sum(dtf1[,5] == 1)
p_x11_l0 <- sum(dtf1[,1] == 1 & dtf1[,5] == 0) / sum(dtf1[,5] == 0)
p_x11_l1 <- sum(dtf1[,1] == 1 & dtf1[,5] == 1) / sum(dtf1[,5] == 1)
p_l0_x10 <- (p_x10_l0 * pl0) / p_x10
p_l1_x10 <- (p_x10_l1 * pl1) / p_x10
p_l0_x11 <- (p_x11_l0 * pl0) / p_x11
p_l1_x11 <- (p_x11_l1 * pl1) / p_x11
# Feature X2
p_x20 <- sum(dtf1[,2] == 0) / nrow(dtf1)
p_x21 <- sum(dtf1[,2] == 1) / nrow(dtf1)
p_x20_l0 <- sum(dtf1[,2] == 0 & dtf1[,5] == 0) / sum(dtf1[,5] == 0)
p_x20_l1 <- sum(dtf1[,2] == 0 & dtf1[,5] == 1) / sum(dtf1[,5] == 1)
p_x21_l0 <- sum(dtf1[,2] == 1 & dtf1[,5] == 0) / sum(dtf1[,5] == 0)
p_x21_l1 <- sum(dtf1[,2] == 1 & dtf1[,5] == 1) / sum(dtf1[,5] == 1)
p_l0_x20 <- (p_x20_l0 * pl0) / p_x20
p_l1_x20 <- (p_x20_l1 * pl1) / p_x20
p_l0_x21 <- (p_x21_l0 * pl0) / p_x21
p_l1_x21 <- (p_x21_l1 * pl1) / p_x21
# Feature X3
p_x30 <- sum(dtf1[,3] == 0) / nrow(dtf1)
p_x31 <- sum(dtf1[,3] == 1) / nrow(dtf1)
p_x30_l0 <- sum(dtf1[,3] == 0 & dtf1[,5] == 0) / sum(dtf1[,5] == 0)
p_x30_l1 <- sum(dtf1[,3] == 0 & dtf1[,5] == 1) / sum(dtf1[,5] == 1)
p_x31_l0 <- sum(dtf1[,3] == 1 & dtf1[,5] == 0) / sum(dtf1[,5] == 0)
p_x31_l1 <- sum(dtf1[,3] == 1 & dtf1[,5] == 1) / sum(dtf1[,5] == 1)
p_l0_x30 <- (p_x30_l0 * pl0) / p_x30
p_l1_x30 <- (p_x30_l1 * pl1) / p_x30
p_l0_x31 <- (p_x31_l0 * pl0) / p_x31
p_l1_x31 <- (p_x31_l1 * pl1) / p_x31
# Feature X4
p_x40 <- sum(dtf1[,4] == 0) / nrow(dtf1)
p_x41 <- sum(dtf1[,4] == 1) / nrow(dtf1)
p_x40_l0 <- sum(dtf1[,4] == 0 & dtf1[,5] == 0) / sum(dtf1[,5] == 0)
p_x40_l1 <- sum(dtf1[,4] == 0 & dtf1[,5] == 1) / sum(dtf1[,5] == 1)
p_x41_l0 <- sum(dtf1[,4] == 1 & dtf1[,5] == 0) / sum(dtf1[,5] == 0)
p_x41_l1 <- sum(dtf1[,4] == 1 & dtf1[,5] == 1) / sum(dtf1[,5] == 1)
p_l0_x40 <- (p_x40_l0 * pl0) / p_x40
p_l1_x40 <- (p_x40_l1 * pl1) / p_x40
p_l0_x41 <- (p_x41_l0 * pl0) / p_x41
p_l1_x41 <- (p_x41_l1 * pl1) / p_x41
# Predicting probability
p0 <- sum(ifelse(dtf2[,1] == 0, p_l0_x10, p_l0_x11),
ifelse(dtf2[,2] == 0, p_l0_x20, p_l0_x21),
ifelse(dtf2[,3] == 0, p_l0_x30, p_l0_x31),
ifelse(dtf2[,4] == 0, p_l0_x40, p_l0_x41))
p1 <- sum(ifelse(dtf2[,1] == 1, p_l1_x11, p_l1_x10),
ifelse(dtf2[,2] == 1, p_l1_x21, p_l1_x20),
ifelse(dtf2[,3] == 1, p_l1_x31, p_l1_x30),
ifelse(dtf2[,4] == 1, p_l1_x41, p_l1_x40))
# Result
ifelse(p0 == p1, "Result: 0 or 1", ifelse(p0 > p1, "Result: 0", "Result: 1"))