library(ggplot2)
library(tidyverse)
library(forcats)
library(dplyr)
library(readr)
library(e1071)
library(rsample)
library(caret)Teen Games
Data Dictionary 📕
| Code | Purpose |
|---|---|
| library() | loads a function |
| ggplot() | foundation; allows to define data & aes |
| aes() | aesthetic mappings; map data variables to visual properties of plot |
| geom_bar() | create bar charts |
| scale_y_continuous() | controls y-axis scaling |
| theme() | fine-tuning customization for appearance |
| labs() | changes labels |
| facet_wrap() | wrapping layout |
| %>% (pipe operator) | passes result to next function |
| mutate() | adds or modifies columns |
| head() | shows first few rows of dataset or vector |
| count() | counts # of times a value comes up |
| prop.table() | converts counts to proportions |
| set.seed() | makes random results |
| naiveBayes() | probability model based on Bayes’ Theorem |
Downloading & Libraries 
First, I downloaded the different libraries that were needed for this project.
I made it easier for the data set to be loaded in for the case of an empty Environment.
Teen_Game <- read_csv("Fav Games of Teens Survey (Online) - Sheet1.csv")
Teen_Game <- Teen_Game %>%
mutate_at(vars(Age, Sex, Residence, PCGame, MGame), as.factor)Favorite PC Games
Then, I created a graph that portrayed the amount of people who considered what PC (Personal Computer) game they considered to be their favorite. I had 27 sureyees.
ggplot(Teen_Game)+
aes(PCGame) +
geom_bar() +
scale_y_continuous(breaks = scales::pretty_breaks(n = 10)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme(text = element_text(size = 14)) +
labs(y = "Count")Favorite Mobile Games
To follow, I did the same but with Mobile Games.
ggplot(Teen_Game)+
aes(MGame) +
geom_bar() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(y = "Count")This graphs shows the varying ages of adolescence (teenhood) and what age groups preferred which PC game.
ggplot(Teen_Game) +
aes(PCGame) +
geom_bar() +
labs(title = "Age on PC Game") +
scale_y_continuous(breaks = scales::pretty_breaks(n = 10)) +
theme(
plot.title = element_text(hjust = 0.5),
axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(y = "Count") +
facet_wrap(~ Age)ggplot(Teen_Game) +
aes(MGame) +
geom_bar() +
labs(title = "Age on Mobile Game") +
scale_y_continuous(breaks = scales::pretty_breaks(n = 3)) +
theme(
plot.title = element_text(hjust = 0.5)) +
labs(y = "Count") +
coord_flip() +
facet_wrap(~ Age)MC Fans vs. Non-MC Fans
I classified those who did not answer as No.
Teen_Game <- Teen_Game %>%
mutate(MCBinary = as.factor(case_when(
PCGame == "Minecraft" ~ "Yes",
is.na(PCGame) ~ "No",
TRUE ~ "No")))
head(Teen_Game)# A tibble: 6 × 6
Age Sex Residence PCGame MGame MCBinary
<fct> <fct> <fct> <fct> <fct> <fct>
1 18 Male USA Marvel Rivals Clash Royale No
2 15 Male USA Minecraft Block Blast Yes
3 18 Male USA Valorant Clash Royale No
4 16 Male USA ARK <NA> No
5 17 Male Canada Minecraft Block Blast Yes
6 17 Male England Minecraft Clash Royale Yes
count(Teen_Game, MCBinary)# A tibble: 2 × 2
MCBinary n
<fct> <int>
1 No 14
2 Yes 13
prop.table(table(Teen_Game$MGame
[Teen_Game$MCBinary == "Yes"]))
Block Blast Brawl Stars Chess Clash Royale CRK
0.15384615 0.15384615 0.00000000 0.30769231 0.00000000
E Football Mech Arena Retro Bowl Roblox Subway Surfers
0.00000000 0.07692308 0.00000000 0.07692308 0.00000000
TCG Pocket
0.23076923
set.seed(123)
split <- initial_split (Teen_Game, prop = 0.7, strata = MGame)
train <- training(split)
test <- testing(split)#install.packages("e1071")
model <- naiveBayes(MGame ~ MCBinary, data = train, laplace = 1)
model$tables$MCBinary
MCBinary
Y No Yes
Block Blast 0.2500000 0.7500000
Brawl Stars 0.5000000 0.5000000
Chess 0.6666667 0.3333333
Clash Royale 0.3333333 0.6666667
CRK 0.6666667 0.3333333
E Football 0.6666667 0.3333333
Mech Arena 0.3333333 0.6666667
Retro Bowl 0.6666667 0.3333333
Roblox 0.3333333 0.6666667
Subway Surfers 0.5000000 0.5000000
TCG Pocket 0.2500000 0.7500000
#install.packages(rsample)predictions <- predict(model, newdata = test)
tbl <- table(Predicted = predictions, Actual = test$MGame)
tidy_tbl <- as.data.frame(tbl)
colnames(tidy_tbl) <- c("Predicted","Actual", "Count")
print(tidy_tbl) Predicted Actual Count
1 Block Blast Block Blast 0
2 Brawl Stars Block Blast 0
3 Chess Block Blast 0
4 Clash Royale Block Blast 0
5 CRK Block Blast 0
6 E Football Block Blast 0
7 Mech Arena Block Blast 0
8 Retro Bowl Block Blast 0
9 Roblox Block Blast 0
10 Subway Surfers Block Blast 0
11 TCG Pocket Block Blast 0
12 Block Blast Brawl Stars 0
13 Brawl Stars Brawl Stars 0
14 Chess Brawl Stars 0
15 Clash Royale Brawl Stars 2
16 CRK Brawl Stars 0
17 E Football Brawl Stars 0
18 Mech Arena Brawl Stars 0
19 Retro Bowl Brawl Stars 0
20 Roblox Brawl Stars 0
21 Subway Surfers Brawl Stars 0
22 TCG Pocket Brawl Stars 0
23 Block Blast Chess 0
24 Brawl Stars Chess 0
25 Chess Chess 0
26 Clash Royale Chess 0
27 CRK Chess 0
28 E Football Chess 0
29 Mech Arena Chess 0
30 Retro Bowl Chess 0
31 Roblox Chess 0
32 Subway Surfers Chess 0
33 TCG Pocket Chess 0
34 Block Blast Clash Royale 0
35 Brawl Stars Clash Royale 0
36 Chess Clash Royale 0
37 Clash Royale Clash Royale 3
38 CRK Clash Royale 0
39 E Football Clash Royale 0
40 Mech Arena Clash Royale 0
41 Retro Bowl Clash Royale 0
42 Roblox Clash Royale 0
43 Subway Surfers Clash Royale 0
44 TCG Pocket Clash Royale 0
45 Block Blast CRK 0
46 Brawl Stars CRK 0
47 Chess CRK 0
48 Clash Royale CRK 0
49 CRK CRK 0
50 E Football CRK 0
51 Mech Arena CRK 0
52 Retro Bowl CRK 0
53 Roblox CRK 0
54 Subway Surfers CRK 0
55 TCG Pocket CRK 0
56 Block Blast E Football 0
57 Brawl Stars E Football 0
58 Chess E Football 0
59 Clash Royale E Football 0
60 CRK E Football 0
61 E Football E Football 0
62 Mech Arena E Football 0
63 Retro Bowl E Football 0
64 Roblox E Football 0
65 Subway Surfers E Football 0
66 TCG Pocket E Football 0
67 Block Blast Mech Arena 0
68 Brawl Stars Mech Arena 0
69 Chess Mech Arena 0
70 Clash Royale Mech Arena 0
71 CRK Mech Arena 0
72 E Football Mech Arena 0
73 Mech Arena Mech Arena 0
74 Retro Bowl Mech Arena 0
75 Roblox Mech Arena 0
76 Subway Surfers Mech Arena 0
77 TCG Pocket Mech Arena 0
78 Block Blast Retro Bowl 0
79 Brawl Stars Retro Bowl 0
80 Chess Retro Bowl 0
81 Clash Royale Retro Bowl 0
82 CRK Retro Bowl 0
83 E Football Retro Bowl 0
84 Mech Arena Retro Bowl 0
85 Retro Bowl Retro Bowl 0
86 Roblox Retro Bowl 0
87 Subway Surfers Retro Bowl 0
88 TCG Pocket Retro Bowl 0
89 Block Blast Roblox 0
90 Brawl Stars Roblox 0
91 Chess Roblox 0
92 Clash Royale Roblox 2
93 CRK Roblox 0
94 E Football Roblox 0
95 Mech Arena Roblox 0
96 Retro Bowl Roblox 0
97 Roblox Roblox 0
98 Subway Surfers Roblox 0
99 TCG Pocket Roblox 0
100 Block Blast Subway Surfers 0
101 Brawl Stars Subway Surfers 0
102 Chess Subway Surfers 0
103 Clash Royale Subway Surfers 1
104 CRK Subway Surfers 0
105 E Football Subway Surfers 0
106 Mech Arena Subway Surfers 0
107 Retro Bowl Subway Surfers 0
108 Roblox Subway Surfers 0
109 Subway Surfers Subway Surfers 0
110 TCG Pocket Subway Surfers 0
111 Block Blast TCG Pocket 0
112 Brawl Stars TCG Pocket 0
113 Chess TCG Pocket 0
114 Clash Royale TCG Pocket 1
115 CRK TCG Pocket 0
116 E Football TCG Pocket 0
117 Mech Arena TCG Pocket 0
118 Retro Bowl TCG Pocket 0
119 Roblox TCG Pocket 0
120 Subway Surfers TCG Pocket 0
121 TCG Pocket TCG Pocket 0
cm <- confusionMatrix(predictions, test$MGame)
cm$table Reference
Prediction Block Blast Brawl Stars Chess Clash Royale CRK E Football
Block Blast 0 0 0 0 0 0
Brawl Stars 0 0 0 0 0 0
Chess 0 0 0 0 0 0
Clash Royale 0 2 0 3 0 0
CRK 0 0 0 0 0 0
E Football 0 0 0 0 0 0
Mech Arena 0 0 0 0 0 0
Retro Bowl 0 0 0 0 0 0
Roblox 0 0 0 0 0 0
Subway Surfers 0 0 0 0 0 0
TCG Pocket 0 0 0 0 0 0
Reference
Prediction Mech Arena Retro Bowl Roblox Subway Surfers TCG Pocket
Block Blast 0 0 0 0 0
Brawl Stars 0 0 0 0 0
Chess 0 0 0 0 0
Clash Royale 0 0 2 1 1
CRK 0 0 0 0 0
E Football 0 0 0 0 0
Mech Arena 0 0 0 0 0
Retro Bowl 0 0 0 0 0
Roblox 0 0 0 0 0
Subway Surfers 0 0 0 0 0
TCG Pocket 0 0 0 0 0
cm$overall Accuracy Kappa AccuracyLower AccuracyUpper AccuracyNull
0.33333333 0.00000000 0.07485463 0.70070494 0.33333333
AccuracyPValue McnemarPValue
0.62282172 NaN
cm$byClass Sensitivity Specificity Pos Pred Value Neg Pred Value
Class: Block Blast NA 1 NA NA
Class: Brawl Stars 0 1 NaN 0.7777778
Class: Chess NA 1 NA NA
Class: Clash Royale 1 0 0.3333333 NaN
Class: CRK NA 1 NA NA
Class: E Football NA 1 NA NA
Class: Mech Arena NA 1 NA NA
Class: Retro Bowl NA 1 NA NA
Class: Roblox 0 1 NaN 0.7777778
Class: Subway Surfers 0 1 NaN 0.8888889
Class: TCG Pocket 0 1 NaN 0.8888889
Precision Recall F1 Prevalence Detection Rate
Class: Block Blast NA NA NA 0.0000000 0.0000000
Class: Brawl Stars NA 0 NA 0.2222222 0.0000000
Class: Chess NA NA NA 0.0000000 0.0000000
Class: Clash Royale 0.3333333 1 0.5 0.3333333 0.3333333
Class: CRK NA NA NA 0.0000000 0.0000000
Class: E Football NA NA NA 0.0000000 0.0000000
Class: Mech Arena NA NA NA 0.0000000 0.0000000
Class: Retro Bowl NA NA NA 0.0000000 0.0000000
Class: Roblox NA 0 NA 0.2222222 0.0000000
Class: Subway Surfers NA 0 NA 0.1111111 0.0000000
Class: TCG Pocket NA 0 NA 0.1111111 0.0000000
Detection Prevalence Balanced Accuracy
Class: Block Blast 0 NA
Class: Brawl Stars 0 0.5
Class: Chess 0 NA
Class: Clash Royale 1 0.5
Class: CRK 0 NA
Class: E Football 0 NA
Class: Mech Arena 0 NA
Class: Retro Bowl 0 NA
Class: Roblox 0 0.5
Class: Subway Surfers 0 0.5
Class: TCG Pocket 0 0.5