-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.rmd
391 lines (260 loc) · 16 KB
/
README.rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
---
output:
md_document:
variant: markdown_github
---
# Purpose
This README documents Zander Prinsloo's (20065124) answers to the Data Science test on Monday 24 May 2021. It describes how I go about answering the three questions of the test, and also gives those answers in the sections below. The sections are appropriately named *Question 1*, *Question 2*, and *Question 3*.
Note, however, that there are also sub-folders for each question. These also have their own projects. Within those folders you will find, amongst other things, a) pdfs with the answers to the relevant questions, b) a code folder that contains specific code and functions used in answering that question. The latter are sourced into this README.
#### How I Got Started
Here is the code for how I created this folder, specifically the *20065124.Rproj* and corresponding folders, as well as the sub-directories for each question.
```{r}
# Create the project - This is all commented out now.
# LOCATION <- c("/Users/zanderprinsloo/Library/Mobile Documents/com~apple~CloudDocs/Desktop/Desktop – MacBookPro’s MacBook Pro/Academic/Postgraduate/Masters/Modules/Data Science/Test")
# fmxdat::make_project(FilePath = LOCATION,
# ProjNam = "20065124",
# Mac = T)
# # Create Projects for each question including Texevier template
# Texevier::create_template(directory = paste0(LOCATION, "/20065124"),
# template_name = "Question1")
# Texevier::create_template(directory = paste0(LOCATION, "/20065124"),
# template_name = "Question2")
# Texevier::create_template(directory = paste0(LOCATION, "/20065124"),
# template_name = "Question3")
```
# Question 1
```{r, echo = FALSE, message = FALSE, warning = FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 5)
options(dplyr.summarise.inform=F)
#Load Packages
if(!require(pacman))(install.packages("pacman")) # in case someone has not installed `pacman`
library(pacman)
p_load(tidyverse, lubridate, xtable, purrr, ggsci)
# Notice that as you are working in a .Rproj file (I am assuming you are) - the relative paths of your directories start at your specified root.
# This means that when working in a .Rproj file, you never need to use getwd() - it is assumed as your base root automatically.
# Source Functions
list.files('Question1/code/', full.names = T, recursive = T) %>%
as.list() %>%
walk(~source(.))
# Data
```
```{r}
list.files('code/', full.names = T, recursive = T) %>% as.list() %>% walk(~source(.))
# Source Question 1's code too ()for plot_order function) - note ..// here means go up one level from current folder
list.files('..//Question1/code/', full.names = T, recursive = T) %>% as.list() %>% walk(~source(.))
# Data loading:
# Import Data
Loc <- "/Users/zanderprinsloo/Library/Mobile Documents/com~apple~CloudDocs/Desktop/Desktop – MacBookPro’s MacBook Pro/Academic/Postgraduate/Masters/Modules/Data Science/Test/20065124/data/Movies/"
df_movies <- read_csv(paste0(Loc, "Movies.csv"),
col_names = T,
col_types = cols())
```
This section provides the answers for Question 1. Before answering the question I need to a) do garbage collection, b) load important packages, c) source relevant code from the `Question1/code`. This is done the separate chunks below.
Garbage Collection:
```{r}
# Garbage Collection
gc()
# Load Packages
if(!require(pacman))(install.packages("pacman")) # in case someone has not installed `pacman`
library(pacman)
p_load(tidyverse, lubridate, forcats)
# Source Functions
list.files('Question1/code/', full.names = T, recursive = T) %>%
as.list() %>%
walk(~source(.))
```
## Task
I will seek to prove that movie critics were not a near perfect prediction of a film'a popularity and profitability amongst audiences in the mid-2000s. In order to do that I use the **Movies.csv** data set given to me by my friend at *Mr Video*. I will try to disprove her claims using plots throughout.
## Claim 1
She claims that:
*:if a movie had a rating of more than 80% on Rotten Tomatoes, audiences would rate it above 85% every time."*
The barplot below clearly shows that my friend's claim is false. For the films that Rotten Tomatoes rated above 80, an overwhelming majority was not rated above 85 by audiences.
```{r Figure1, warning = FALSE, fig.align = 'center', fig.cap = "Caption Here \\label{Figure1}", fig.ext = 'png', fig.height = 6, fig.width = 7}
g1 <- Barplot1_Q1(df = df_movies,
title = "Rotten Tomatoes is Rotten",
subtitle = "RT is a bad predictor of what audiences love",
caption = "Thank you to Mr Video for the data",
Xlab = "Rotten Tomatoes Rating",
Ylab = "Number of Films in Each Class",
xaxis_size = 10)
g1
```
## Claim 2
The second claim is that *"Disney films may not have the highest grossing numbers, but they’ve always been the most profitable of all the leading studios."*
I will show that they have not always been the most profitable out of the leading studios.
There is one film called `r df_movies %>% filter(Profitability>60) %>% select(Film)` that has a very high profitability of `r df_movies %>% filter(Profitability>60) %>% select(Profitability)`. It is an outlier that will be excluded from the plot along with any missing values of profitability or studios.
```{r}
df_movies %>%
filter(Profitability>60)
```
In \ref{Figure2} we see that Disney is not clearly the most profitable film. While it's median is very close to Fox, Sony, and Summit, it's upper quintile is substantially lower than Summit. Note also that Disney is being pulled up by an outlier, `r df_movies %>% filter(Profitability>20 & Profitability <60) %>% select(Film)`
```{r Figure2, warning = FALSE, fig.align = 'center', fig.cap = "Boxplot Comparing Disney's Profitability \\label{Figure2}", fig.ext = 'png', fig.height = 6, fig.width = 7}
g2 <- Boxplot_Q1(df = df_movies,
Title = "Profitability of Leading Studios",
Subtitle = "Disney vs Others",
Caption = "Note: \n Thank you to Mr Video for the data",
Ylab = "Studios",
Xlab = "Profitability of Films")
g2
```
If we remove this outlier, we get \ref{Figure3}. It shows that Disney is an average performer in terms of Profitability. It's median is lwoer than Fox, and its upper quintile appears lower than Fox, Sony, Summit, and Universal Studios.
```{r Figure3, warning = FALSE, fig.align = 'center', fig.cap = "Boxplot Comparing Disney's Profitability \\label{Figure3}", fig.ext = 'png', fig.height = 6, fig.width = 7}
df_g2 <- g2$data
g2 %+%
subset(df_g2, Profitability<20)
```
Therefore, it is not clear that Disney is indeed more profitable. The plots indicate that there are a number of studios that are more profitable and more consistent.
## Claim 3
The third claim is that *"Audiences are always drawn to the highest grossing films"*. She goes further to claim that the correlation between world wwide grossing numbers and audience scores would be near 80%.
<!-- The correlation between these two variables is `r as.matrix(df_movies %>% select("Audience score %", "Worldwide Gross")) ` -->
```{r}
df_use <- bind_rows(df_movies %>%
select("Audience score %", "Worldwide Gross") %>%
mutate(Type = "Audience and Gross Relationship") %>%
summarise(AudienceScore = as.numeric(`Audience score %`),
WorldWideGross = as.numeric(`Worldwide Gross`),
Type),
tibble(AudienceScore = seq(from = 30, to = 90, length = nrow(df_movies)),
WorldWideGross = seq(from = 100, to = 600, length = nrow(df_movies)))) %>%
mutate(Type = ifelse(is.na(Type), "Linear Relationship", Type))
```
Note that \ref{Figure4} gives the relationship between world wide gross and audience rating. The linear line is shows what a strong correlation would look like. However, the actual line plot of the relationship between world wide gross and audience scores is much more jagged and sporadic. There is not a clear linear relationship and correlation. This means that the third claim also appears false.
```{r Figure4, warning = FALSE, fig.align = 'center', fig.cap = "Jagged Line Plot of Gross and Rating \\label{Figure4}", fig.ext = 'png', fig.height = 6, fig.width = 7}
g3 <- Lineplot_Q1(df = df_movies,
Title = "Line Plot - Relationship Between World Wide Gross and Audience Scores",
Subtitle = "Compared to a strong correlation",
Caption = "Note: \n Thank you to Mr Video for the data",
Ylab = "World Wide Gross",
Xlab = "Audience Ratings")
g3
```
# Question 2
```{r, echo = FALSE, message = FALSE, warning = FALSE}
options(dplyr.summarise.inform=F)
if(!require("pacman")) install.packages("pacman")
library(pacman)
p_load(tidyverse)
list.files('code/', full.names = T, recursive = T) %>% as.list() %>% walk(~source(.))
list.files('..//Question1/code/', full.names = T, recursive = T) %>% as.list() %>% walk(~source(.))
list.files('..//Question2/code/', full.names = T, recursive = T) %>% as.list() %>% walk(~source(.))
source("/Users/zanderprinsloo/Library/Mobile Documents/com~apple~CloudDocs/Desktop/Desktop – MacBookPro’s MacBook Pro/Academic/Postgraduate/Masters/Modules/Data Science/Test/20065124/Question2/code/fn_Collate_Data.R")
source("/Users/zanderprinsloo/Library/Mobile Documents/com~apple~CloudDocs/Desktop/Desktop – MacBookPro’s MacBook Pro/Academic/Postgraduate/Masters/Modules/Data Science/Test/20065124/Question2/code/plot1_Q2.R")
# Data loading:
df_forbes <- fn_Collate_Data(path = "/Users/zanderprinsloo/Library/Mobile Documents/com~apple~CloudDocs/Desktop/Desktop – MacBookPro’s MacBook Pro/Academic/Postgraduate/Masters/Modules/Data Science/Test/20065124/data/Forbes/", ext = "csv")
```
# Task
Some interesting findings using the Forbes data set
# Plot 1
```{r Figure1, , warning = FALSE, fig.align = 'center', fig.cap = "Names of Rich People \\label{Figure1}", fig.ext = 'png', fig.height = 3, fig.width = 6}
g1 <- plot1_Q2(df = df_forbes, title = "Common Names of Rich People",
Xlab = "Names",
Ylab = "Frequency of Names")
g1
```
This plot in \ref{Figure1} shows that there is a surprisingly high requency of rich people with ('Common') names John, Mike/Michael, James, or Bill. Similarly, there are quite a number of families that make up the Billionaires on Forbes' list.
# Question 3
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE, fig.width = 6, fig.height = 5, fig.pos="H", fig.pos = 'H')
# Note: Include = FALSE implies the code is executed, but not printed in your pdf.
# warning and message = FALSE implies ugly messages and warnings are removed from your pdf.
# These should be picked up when you execute the command chunks (code sections below) in your rmd, not printed in your paper!
# Load Packages
if(!require(pacman))(install.packages("pacman")) # in case someone has not installed `pacman`
library(pacman)
p_load(tidyverse, lubridate, xtable)
# Notice that as you are working in a .Rproj file (I am assuming you are) - the relative paths of your directories start at your specified root.
# This means that when working in a .Rproj file, you never need to use getwd() - it is assumed as your base root automatically.
# Source Functions
list.files('Question3/code/', full.names = T, recursive = T) %>%
as.list() %>%
walk(~source(.))
source("/Users/zanderprinsloo/Library/Mobile Documents/com~apple~CloudDocs/Desktop/Desktop – MacBookPro’s MacBook Pro/Academic/Postgraduate/Masters/Modules/Data Science/Test/20065124/Question3/code/plot1_q3.R")
# Data
df_country <- read_csv("/Users/zanderprinsloo/Library/Mobile Documents/com~apple~CloudDocs/Desktop/Desktop – MacBookPro’s MacBook Pro/Academic/Postgraduate/Masters/Modules/Data Science/Test/20065124/data/Tweets/Country_list.csv")
country_names <- colnames(df_country)
df_bbc <- readRDS("/Users/zanderprinsloo/Library/Mobile Documents/com~apple~CloudDocs/Desktop/Desktop – MacBookPro’s MacBook Pro/Academic/Postgraduate/Masters/Modules/Data Science/Test/20065124/data/Tweets/tweets_bbc.rds")
df_cnn <- readRDS("/Users/zanderprinsloo/Library/Mobile Documents/com~apple~CloudDocs/Desktop/Desktop – MacBookPro’s MacBook Pro/Academic/Postgraduate/Masters/Modules/Data Science/Test/20065124/data/Tweets/tweets_cnn.rds")
df_eco <- readRDS("/Users/zanderprinsloo/Library/Mobile Documents/com~apple~CloudDocs/Desktop/Desktop – MacBookPro’s MacBook Pro/Academic/Postgraduate/Masters/Modules/Data Science/Test/20065124/data/Tweets/tweets_eco.rds")
```
For this question I will give a barplot to show the composition of tweets that use photos and videos for the different media outlets
```{r}
df_photo <- bind_rows(
df_bbc %>%
select(photos, video) %>%
mutate(photos = ifelse(photos=="[]", 0, 1),
Media = ifelse(photos==1, "photo", ifelse(video==1, "video", "none")),
Outlet = "BBC"),
df_cnn %>%
select(photos, video) %>%
mutate(photos = ifelse(photos=="[]", 0, 1),
Media = ifelse(photos==1, "photo", ifelse(video==1, "video", "none")),
Outlet = "CNN"),
df_eco %>%
select(photos, video) %>%
mutate(photos = ifelse(photos=="[]", 0, 1),
Media = ifelse(photos==1, "photo", ifelse(video==1, "video", "none")),
Outlet = "Eco"))
```
```{r Figure5, warning = FALSE, fig.align = 'center', fig.cap = "Compare Videos and Photos of Outlets \\label{Figure1}", fig.ext = 'png', fig.height = 3, fig.width = 6}
plot1_q3 <- function(df_bbc, df_cnn, df_eco, title, subtitle, Ylab, Xlab){
df_use <- bind_rows(
df_bbc %>%
select(photos, video) %>%
mutate(photos = ifelse(photos=="[]", 0, 1),
Media = ifelse(photos==1, "photo", ifelse(video==1, "video", "none")),
Outlet = "BBC"),
df_cnn %>%
select(photos, video) %>%
mutate(photos = ifelse(photos=="[]", 0, 1),
Media = ifelse(photos==1, "photo", ifelse(video==1, "video", "none")),
Outlet = "CNN"),
df_eco %>%
select(photos, video) %>%
mutate(photos = ifelse(photos=="[]", 0, 1),
Media = ifelse(photos==1, "photo", ifelse(video==1, "video", "none")),
Outlet = "Eco"))
g <- ggplot(df_photo) +
geom_bar(aes(x = Outlet, colour = Media, fill = Media))+
labs(title = title,
subtitle = subtitle,
y = Ylab,
x = Xlab)
g
}
plot1_q3(df_bbc, df_eco, df_cnn,
title = "Compare Photo/Video Usage of Main Outlets",
subtitle = "Comparison of Photos and Videos in Tweets",
Xlab = "Media Outlet",
Ylab = "Count")
```
# Question 3b
Now I do a similar plot to compare the quantity of hashtags
```{r Figure6, warning = FALSE, fig.align = 'center', fig.cap = "Caption Here \\label{Figure2}", fig.height = 3, fig.width = 6, dev = 'png'}
pacman::p_load(lubridate)
df_hashtags <- bind_rows(
df_bbc %>%
select(hashtags, date) %>%
mutate(hashtags = ifelse(hashtags=="[]", 0, 1),
Outlet = c("BBC"),
date = as.character(date),
year = str_sub(date, start = 1, end = 4)),
df_cnn %>%
select(hashtags, date) %>%
mutate(hashtags = ifelse(hashtags=="[]", 0, 1),
Outlet = "CNN",
date = as.character(date),
year = str_sub(date, start = 1, end = 4)),
df_eco %>%
select(hashtags, date) %>%
mutate(hashtags = ifelse(hashtags=="[]", 0, 1),
Outlet = "Eco",
date = as.character(date),
year = str_sub(date, start = 1, end = 4))) %>%
group_by(Outlet, year) %>%
summarise(TotalHashtags = sum(hashtags))
pacman::p_load(ggpubr)
g2 <- ggplot(data = df_hashtags) +
geom_bar(aes(x = year, y = TotalHashtags, colour = Outlet, fill = Outlet), stat = "identity") +
labs(title = "Hashtags")
g2
```