1 Introduction

Greetings!. This is the Chapter 2 coursenotes of the DATACAMP course “Supervised Learning - Classification”. The guy handling this stuff is a lad named Brett Lantz

All the commentary i have here are my words on how i try to interpret what Mr Lantz is saying in the lecture videos + how i am trying to juxtapose what he’s said into a more relatable example.

1.1 Loading the required tables and data

library(tidyverse)
## -- Attaching packages -------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.0.0     v purrr   0.2.5
## v tibble  1.4.2     v dplyr   0.7.6
## v tidyr   0.8.1     v stringr 1.3.1
## v readr   1.1.1     v forcats 0.3.0
## -- Conflicts ----------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(class)
library(data.table)
## 
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
## The following object is masked from 'package:purrr':
## 
##     transpose

Folks, since the csv file for where9am is not available, i will try to reproduce the data the best i can

location <- read_csv("https://assets.datacamp.com/production/course_2906/datasets/locations.csv")
## Parsed with column specification:
## cols(
##   month = col_integer(),
##   day = col_integer(),
##   weekday = col_character(),
##   daytype = col_character(),
##   hour = col_integer(),
##   hourtype = col_character(),
##   location = col_character()
## )
where9am <- location %>%
  filter(hour == 9, hourtype == "morning")
head(location)
## # A tibble: 6 x 7
##   month   day weekday   daytype  hour hourtype location
##   <int> <int> <chr>     <chr>   <int> <chr>    <chr>   
## 1     1     4 wednesday weekday     0 night    home    
## 2     1     4 wednesday weekday     1 night    home    
## 3     1     4 wednesday weekday     2 night    home    
## 4     1     4 wednesday weekday     3 night    home    
## 5     1     4 wednesday weekday     4 night    home    
## 6     1     4 wednesday weekday     5 night    home
head(where9am)
## # A tibble: 6 x 7
##   month   day weekday   daytype  hour hourtype location
##   <int> <int> <chr>     <chr>   <int> <chr>    <chr>   
## 1     1     4 wednesday weekday     9 morning  office  
## 2     1     5 thursday  weekday     9 morning  office  
## 3     1     6 friday    weekday     9 morning  office  
## 4     1     7 saturday  weekend     9 morning  home    
## 5     1     8 sunday    weekend     9 morning  home    
## 6     1     9 monday    weekday     9 morning  campus

2 Computing Probabilities

# Compute P(A) 
p_A <- nrow(subset(where9am, location == "office")) / 91

# Compute P(B)
p_B <- nrow(subset(where9am, daytype == "weekday")) / 91

# Compute the observed P(A and B)
p_AB <- nrow(subset(where9am, where9am$location == "office" & where9am$daytype == "weekday")) / 91

# Compute P(A | B) and print its value
p_A_given_B <- p_AB / p_B
p_A_given_B
## [1] 0.6

3 Understanding Dependent Events

I believe the answeer is “ALL OF THE ABOVE” given apparently the events do not overlop and knowing that one occured tells you about the status of the other

4 A simple Naive Bayes location model

Let us load the library

library(naivebayes)
## 
## Attaching package: 'naivebayes'
## The following object is masked from 'package:data.table':
## 
##     tables

Given thursday9am and saturday9am do not seem to be available in the Datacamp database repository, i will try to filter out the data and ensure it seems like something similar

thursday9am <- where9am %>%
  filter(weekday == "thursday") 

saturday9am <- where9am %>%
  filter(weekday == "saturday")
# Load the naivebayes package
library(naivebayes)

# Build the location prediction model
locmodel <- naive_bayes(location ~ daytype, data = where9am)

# Predict Thursday's 9am location
predict(locmodel, thursday9am)
##  [1] office office office office office office office office office office
## [11] office office office
## Levels: appointment campus home office
# Predict Saturdays's 9am location
predict(locmodel, saturday9am)
##  [1] home home home home home home home home home home home home home
## Levels: appointment campus home office

5 Examining “raw” probabilities

# Examine the location prediction model
print(locmodel)
## ===================== Naive Bayes ===================== 
## Call: 
## naive_bayes.formula(formula = location ~ daytype, data = where9am)
## 
## A priori probabilities: 
## 
## appointment      campus        home      office 
##  0.01098901  0.10989011  0.45054945  0.42857143 
## 
## Tables: 
##          
## daytype   appointment    campus      home    office
##   weekday   1.0000000 1.0000000 0.3658537 1.0000000
##   weekend   0.0000000 0.0000000 0.6341463 0.0000000
# Obtain the predicted probabilities for Thursday at 9am
predict(locmodel, thursday9am , type = "prob")
##       appointment    campus      home office
##  [1,]  0.01538462 0.1538462 0.2307692    0.6
##  [2,]  0.01538462 0.1538462 0.2307692    0.6
##  [3,]  0.01538462 0.1538462 0.2307692    0.6
##  [4,]  0.01538462 0.1538462 0.2307692    0.6
##  [5,]  0.01538462 0.1538462 0.2307692    0.6
##  [6,]  0.01538462 0.1538462 0.2307692    0.6
##  [7,]  0.01538462 0.1538462 0.2307692    0.6
##  [8,]  0.01538462 0.1538462 0.2307692    0.6
##  [9,]  0.01538462 0.1538462 0.2307692    0.6
## [10,]  0.01538462 0.1538462 0.2307692    0.6
## [11,]  0.01538462 0.1538462 0.2307692    0.6
## [12,]  0.01538462 0.1538462 0.2307692    0.6
## [13,]  0.01538462 0.1538462 0.2307692    0.6
# Obtain the predicted probabilities for Saturday at 9am
predict(locmodel, saturday9am, type = "prob")
##       appointment campus home office
##  [1,]           0      0    1      0
##  [2,]           0      0    1      0
##  [3,]           0      0    1      0
##  [4,]           0      0    1      0
##  [5,]           0      0    1      0
##  [6,]           0      0    1      0
##  [7,]           0      0    1      0
##  [8,]           0      0    1      0
##  [9,]           0      0    1      0
## [10,]           0      0    1      0
## [11,]           0      0    1      0
## [12,]           0      0    1      0
## [13,]           0      0    1      0

6 Understanding independence

Here is my explanation

The answer “Knowing the outcome of one does not help predict the other” is because, for example, if Manchester United’s Paul Pogba scores twice today against Chelsea does not help predict if Fulham’s Aleksandar Mitrovic will score twice against Everton due to the independence of both variables and circumstances

7 Understanding NB’s “naivety”

Ok, folks, so what has been done here in these course notes and stuff is that we’ve been building a simple Naive Bayes model that used historic location data to predict future data

Additional data points are going to be needed in order to build a better prediction of probability of location

Conditional probability so far has been used if one event predicts another. Adding more predictors complicates stuff hence this is why it’s called “Naive”

Conditional probability with regards to overlapping of more than one predictor factors makes it more inefficient for a computer to process the overlap

A “naive” simplification, rather than treating everything as an overlap, uses a shortcut. Rather than treating the problem as a hodgepodge of overlapping events, it makes “naive” assumptions about the data and assumes that the events are independent. The joint probability can be calculated by multiplying the individual probabilities from much simpler intersections

8 Who are you calling naive?

The Naive Bayes algorithm gets its name because it makes a “naive” assumption about event independence. Its purpose is that joint probability calculations are simpler for independent events

9 A more sophisticated location model.

Let me try to replicate the weekday_afternoon and the weekday_evening data

locations <- location

weekday_afternoon <- locations %>%
  filter(daytype == "weekday", hourtype == "afternoon")

weekday_evening <- locations %>%
  filter(daytype == "weekday", hourtype == "evening")
# Build a NB model of location
locmodel <- naive_bayes(location ~ daytype + hourtype, data = locations)

# Predict Brett's location on a weekday afternoon
predict(locmodel, weekday_afternoon)
##   [1] office office office office office office office office office office
##  [11] office office office office office office office office office office
##  [21] office office office office office office office office office office
##  [31] office office office office office office office office office office
##  [41] office office office office office office office office office office
##  [51] office office office office office office office office office office
##  [61] office office office office office office office office office office
##  [71] office office office office office office office office office office
##  [81] office office office office office office office office office office
##  [91] office office office office office office office office office office
## [101] office office office office office office office office office office
## [111] office office office office office office office office office office
## [121] office office office office office office office office office office
## [131] office office office office office office office office office office
## [141] office office office office office office office office office office
## [151] office office office office office office office office office office
## [161] office office office office office office office office office office
## [171] office office office office office office office office office office
## [181] office office office office office office office office office office
## [191] office office office office office office office office office office
## [201] office office office office office office office office office office
## [211] office office office office office office office office office office
## [221] office office office office office office office office office office
## [231] office office office office office office office office office office
## [241] office office office office office office office office office office
## [251] office office office office office office office office office office
## [261] office office office office office office office office office office
## [271] office office office office office office office office office office
## [281] office office office office office office office office office office
## [291] office office office office office office office office office office
## [301] office office office office office office office office office office
## [311] office office office office office office office office office office
## [321] office office office office office office office office office office
## [331] office office office office office office office office office office
## [341] office office office office office office office office office office
## [351] office office office office office office office office office office
## [361] office office office office office office office office office office
## [371] office office office office office office office office office office
## [381] office office office office office office office office office office
## Levels: appointment campus home office restaurant store theater
# Predict Brett's location on a weekday evening
predict(locmodel, weekday_evening)
##   [1] home home home home home home home home home home home home home home
##  [15] home home home home home home home home home home home home home home
##  [29] home home home home home home home home home home home home home home
##  [43] home home home home home home home home home home home home home home
##  [57] home home home home home home home home home home home home home home
##  [71] home home home home home home home home home home home home home home
##  [85] home home home home home home home home home home home home home home
##  [99] home home home home home home home home home home home home home home
## [113] home home home home home home home home home home home home home home
## [127] home home home home home home home home home home home home home home
## [141] home home home home home home home home home home home home home home
## [155] home home home home home home home home home home home home home home
## [169] home home home home home home home home home home home home home home
## [183] home home home home home home home home home home home home home home
## [197] home home home home home home home home home home home home home home
## [211] home home home home home home home home home home home home home home
## [225] home home home home home home home home home home home home home home
## [239] home home home home home home home home home home home home home home
## [253] home home home home home home home home
## Levels: appointment campus home office restaurant store theater

10 Preparing for unforeseen circumstances

Let me try to reproduce weekend_afternoon

weekend_afternoon <- locations %>%
  filter(daytype == "weekend", hourtype == "afternoon")
# Observe the predicted probabilities for a weekend afternoon
predict(locmodel, weekend_afternoon, type = "prob")
##        appointment campus      home office restaurant      store theater
##   [1,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##   [2,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##   [3,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##   [4,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##   [5,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##   [6,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##   [7,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##   [8,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##   [9,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [10,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [11,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [12,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [13,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [14,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [15,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [16,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [17,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [18,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [19,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [20,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [21,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [22,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [23,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [24,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [25,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [26,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [27,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [28,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [29,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [30,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [31,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [32,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [33,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [34,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [35,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [36,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [37,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [38,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [39,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [40,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [41,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [42,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [43,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [44,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [45,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [46,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [47,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [48,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [49,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [50,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [51,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [52,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [53,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [54,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [55,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [56,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [57,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [58,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [59,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [60,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [61,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [62,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [63,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [64,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [65,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [66,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [67,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [68,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [69,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [70,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [71,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [72,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [73,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [74,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [75,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [76,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [77,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [78,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [79,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [80,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [81,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [82,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [83,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [84,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [85,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [86,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [87,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [88,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [89,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [90,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [91,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [92,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [93,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [94,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [95,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [96,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [97,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [98,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
##  [99,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [100,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [101,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [102,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [103,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [104,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [105,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [106,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [107,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [108,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [109,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [110,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [111,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [112,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [113,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [114,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [115,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [116,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [117,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [118,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [119,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [120,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [121,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [122,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [123,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [124,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [125,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [126,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [127,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [128,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [129,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [130,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [131,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [132,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [133,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [134,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [135,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [136,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [137,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [138,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [139,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [140,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [141,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [142,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [143,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [144,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [145,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [146,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [147,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [148,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [149,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [150,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [151,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [152,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [153,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [154,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [155,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
## [156,]  0.02472535      0 0.8472217      0  0.1115693 0.01648357       0
# Build a new model using the Laplace correction
locmodel2 <- naive_bayes(location ~ daytype + hourtype, data = locations, laplace = 1)

# Observe the new predicted probabilities for a weekend afternoon
predict(locmodel2, weekend_afternoon, type = "prob")
##        appointment      campus      home      office restaurant      store
##   [1,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##   [2,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##   [3,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##   [4,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##   [5,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##   [6,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##   [7,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##   [8,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##   [9,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [10,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [11,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [12,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [13,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [14,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [15,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [16,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [17,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [18,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [19,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [20,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [21,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [22,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [23,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [24,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [25,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [26,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [27,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [28,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [29,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [30,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [31,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [32,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [33,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [34,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [35,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [36,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [37,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [38,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [39,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [40,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [41,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [42,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [43,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [44,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [45,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [46,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [47,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [48,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [49,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [50,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [51,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [52,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [53,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [54,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [55,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [56,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [57,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [58,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [59,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [60,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [61,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [62,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [63,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [64,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [65,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [66,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [67,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [68,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [69,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [70,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [71,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [72,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [73,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [74,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [75,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [76,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [77,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [78,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [79,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [80,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [81,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [82,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [83,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [84,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [85,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [86,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [87,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [88,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [89,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [90,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [91,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [92,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [93,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [94,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [95,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [96,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [97,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [98,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##  [99,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [100,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [101,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [102,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [103,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [104,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [105,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [106,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [107,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [108,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [109,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [110,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [111,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [112,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [113,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [114,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [115,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [116,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [117,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [118,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [119,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [120,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [121,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [122,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [123,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [124,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [125,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [126,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [127,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [128,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [129,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [130,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [131,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [132,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [133,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [134,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [135,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [136,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [137,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [138,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [139,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [140,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [141,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [142,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [143,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [144,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [145,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [146,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [147,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [148,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [149,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [150,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [151,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [152,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [153,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [154,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [155,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
## [156,]  0.01107985 0.005752078 0.8527053 0.008023444  0.1032598 0.01608175
##            theater
##   [1,] 0.003097769
##   [2,] 0.003097769
##   [3,] 0.003097769
##   [4,] 0.003097769
##   [5,] 0.003097769
##   [6,] 0.003097769
##   [7,] 0.003097769
##   [8,] 0.003097769
##   [9,] 0.003097769
##  [10,] 0.003097769
##  [11,] 0.003097769
##  [12,] 0.003097769
##  [13,] 0.003097769
##  [14,] 0.003097769
##  [15,] 0.003097769
##  [16,] 0.003097769
##  [17,] 0.003097769
##  [18,] 0.003097769
##  [19,] 0.003097769
##  [20,] 0.003097769
##  [21,] 0.003097769
##  [22,] 0.003097769
##  [23,] 0.003097769
##  [24,] 0.003097769
##  [25,] 0.003097769
##  [26,] 0.003097769
##  [27,] 0.003097769
##  [28,] 0.003097769
##  [29,] 0.003097769
##  [30,] 0.003097769
##  [31,] 0.003097769
##  [32,] 0.003097769
##  [33,] 0.003097769
##  [34,] 0.003097769
##  [35,] 0.003097769
##  [36,] 0.003097769
##  [37,] 0.003097769
##  [38,] 0.003097769
##  [39,] 0.003097769
##  [40,] 0.003097769
##  [41,] 0.003097769
##  [42,] 0.003097769
##  [43,] 0.003097769
##  [44,] 0.003097769
##  [45,] 0.003097769
##  [46,] 0.003097769
##  [47,] 0.003097769
##  [48,] 0.003097769
##  [49,] 0.003097769
##  [50,] 0.003097769
##  [51,] 0.003097769
##  [52,] 0.003097769
##  [53,] 0.003097769
##  [54,] 0.003097769
##  [55,] 0.003097769
##  [56,] 0.003097769
##  [57,] 0.003097769
##  [58,] 0.003097769
##  [59,] 0.003097769
##  [60,] 0.003097769
##  [61,] 0.003097769
##  [62,] 0.003097769
##  [63,] 0.003097769
##  [64,] 0.003097769
##  [65,] 0.003097769
##  [66,] 0.003097769
##  [67,] 0.003097769
##  [68,] 0.003097769
##  [69,] 0.003097769
##  [70,] 0.003097769
##  [71,] 0.003097769
##  [72,] 0.003097769
##  [73,] 0.003097769
##  [74,] 0.003097769
##  [75,] 0.003097769
##  [76,] 0.003097769
##  [77,] 0.003097769
##  [78,] 0.003097769
##  [79,] 0.003097769
##  [80,] 0.003097769
##  [81,] 0.003097769
##  [82,] 0.003097769
##  [83,] 0.003097769
##  [84,] 0.003097769
##  [85,] 0.003097769
##  [86,] 0.003097769
##  [87,] 0.003097769
##  [88,] 0.003097769
##  [89,] 0.003097769
##  [90,] 0.003097769
##  [91,] 0.003097769
##  [92,] 0.003097769
##  [93,] 0.003097769
##  [94,] 0.003097769
##  [95,] 0.003097769
##  [96,] 0.003097769
##  [97,] 0.003097769
##  [98,] 0.003097769
##  [99,] 0.003097769
## [100,] 0.003097769
## [101,] 0.003097769
## [102,] 0.003097769
## [103,] 0.003097769
## [104,] 0.003097769
## [105,] 0.003097769
## [106,] 0.003097769
## [107,] 0.003097769
## [108,] 0.003097769
## [109,] 0.003097769
## [110,] 0.003097769
## [111,] 0.003097769
## [112,] 0.003097769
## [113,] 0.003097769
## [114,] 0.003097769
## [115,] 0.003097769
## [116,] 0.003097769
## [117,] 0.003097769
## [118,] 0.003097769
## [119,] 0.003097769
## [120,] 0.003097769
## [121,] 0.003097769
## [122,] 0.003097769
## [123,] 0.003097769
## [124,] 0.003097769
## [125,] 0.003097769
## [126,] 0.003097769
## [127,] 0.003097769
## [128,] 0.003097769
## [129,] 0.003097769
## [130,] 0.003097769
## [131,] 0.003097769
## [132,] 0.003097769
## [133,] 0.003097769
## [134,] 0.003097769
## [135,] 0.003097769
## [136,] 0.003097769
## [137,] 0.003097769
## [138,] 0.003097769
## [139,] 0.003097769
## [140,] 0.003097769
## [141,] 0.003097769
## [142,] 0.003097769
## [143,] 0.003097769
## [144,] 0.003097769
## [145,] 0.003097769
## [146,] 0.003097769
## [147,] 0.003097769
## [148,] 0.003097769
## [149,] 0.003097769
## [150,] 0.003097769
## [151,] 0.003097769
## [152,] 0.003097769
## [153,] 0.003097769
## [154,] 0.003097769
## [155,] 0.003097769
## [156,] 0.003097769

I figure that adding a Laplace correction allows for a small chance that the lad might go to the office for the weekend in the future

11 Understanding the Laplace Correction

Alright folks, by default the naive_bayes() function does not have a laplace correction. But it does leave the risk that some potential outcomes may be predicted to be impossible.

12 Applying Naive Bayes to other problems

Here, we are approached with the question, what other ways can we apply Naive Bayes?

Naive Bayes tends to work well where information from multiple attributes needs to be considered at the same time. This analogy is similar to how a doctor checks symptoms and runs test so that he/she could diagnose you if you got this certain illness or trying to assess the chances of a certain team you manage in a Football Manager save of beating another team in your league (i know, i know)

Naive Bayes can also be used to classify text data whether or not if an email is spam or the number of times i post “AAAAAAAAAAAAAAAAAAAAAAAAAA” or invoke the name of Emile Heskey and Adebayo Akinfenwa on my Facebook posts could get my FB account spanked by the mods

A consequence of this stuff is that each predictors used in a Naive Bayes model typically comprise a set of categories. The Numerical stuff like time, age, money, Enthalpy and all can’t be used as is without knowing more about the properties of the data.

12.1 Binning numeric data for Naive Bayes

I know. It took me 6 plays of the video for me to figure it out. but it’s a simple technique that divides a set of numbers into “bins” based on certain non-numerical categories.

Example, during the AFL (Australian Football League) Draft Combine, scouts, recruiters or whatever they call the guys who are part of the recruiting department of each AFL club, will want to take a look at the data of potential draftees’ performance in each of the tests in the combine (i.e. Beep Test, 3km Time Trial, Agility Test, Running and Standing Vertical Jumps). Once they get all the results they will aggregate all the numeric data based on the results of those tests and bin them into a certain category to see not just potential but also to predict their potential placing in the actual draft.

13 Handling numeric predictors

Between the ff: choices - Age values recorded as “child” or “adult cateegories”

I’ll go with the Income values part since there is no indication that the transforming these income values will create a set of categories