-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule7Project.rmd
200 lines (172 loc) · 7.31 KB
/
Module7Project.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
---
title: "Module 7 Project"
author: "Zachary Petrea"
date: "2024-04-17"
output:
word_document: default
pdf_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
require(tidyverse)
require(tidytext)
require(gutenbergr)
library(topicmodels)
library(ldatuning)
Movies <- read_csv("MoviesOnStreamingPlatforms.csv")
Movies <- Movies[, -1]
decades <- paste(1:9 * 10, "s", sep = "")
Movies$decades <- paste0(10*round((as.numeric(Movies$Year)-1900)/10),"s")
Movies$`Rotten Tomatoes` <- gsub("\\/.*", "", Movies$`Rotten Tomatoes`)
Movies$`Rotten Tomatoes` <- as.numeric(Movies$`Rotten Tomatoes`)
keys = list(list(1910,1919, "10's"),
list(1920,1929, "20's"),
list(1930,1939, "30's"),
list(1940,1949, "40's"),
list(1950,1959, "50's"),
list(1960,1969, "60's"),
list(1970,1979, "70's"),
list(1980,1989, "80's"),
list(1990,1999, "90's"),
list(2000,2009, "2000's"),
list(2010,2019, "2010's"),
list(2020,2029, "2020's"))
Movies$Label = NA
for(k in keys){
Movies$Label[Movies$Year >= k[[1]] & Movies$Year <= k[[2]]]=k[[3]]
}
Movies <- Movies[, -11]
level_order <- c("10's", "20's", "30's", "40's", "50's", "60's", "70's", "80's", "90's", "2000's", "2010's", "2020's")
Movies %>%
group_by(Label) %>%
summarize(Best = max(`Rotten Tomatoes`, na.rm = TRUE)) %>%
ggplot(aes(factor(Label, level = level_order), Best, color = Best)) +
geom_point() +
xlab("Decade") +
ylab("Rating") +
ggtitle("The Highest Rating Movie per Decade")
#What movies are available in all streaming Platforms?
Stream <- Movies %>%
filter(Netflix == 1 & Hulu == 1 & `Prime Video` == 1 & `Disney+` == 1)
#See which platform has provided the most movies
Platforms <- Movies[, 6:9]
platforms_Long <- pivot_longer(Platforms, cols = Netflix:`Disney+`, names_to = "Apps", values_to = "Streaming")
ggplot(platforms_Long, aes(`Apps`, `Streaming`, fill = `Apps`)) +
geom_col(show.legend = FALSE) +
labs(title = "Movies Available per Streaming Platform", x = "Apps", y = "Number of Titles")
#Platforms Most Popular per age group
unique(Movies$Age)
Movies$Age <- factor(Movies$Age)
Age_Groups <- Movies[, c(4, 6, 7, 8, 9)]
Age_Long <- pivot_longer(Age_Groups, cols = !Age, names_to = "Platform", values_to = "Streaming")
ggplot(Age_Long, aes(`Platform`, `Streaming`, fill = `Age`)) +
geom_col()
#Topic Modeling
Movies1 <- Movies %>% rename("text" = "Title")
tidy_Movies <- Movies1 %>%
#create a new column to track which line every word is coming from
mutate(line = row_number()) %>%
#unnest the text column into the word column
unnest_tokens(word, text) %>%
#filter out the stop words
anti_join(stop_words)
#count the words and identify the most common words
tidy_Movies%>%
count(word,sort = TRUE)
# Exploring tf-idf
Movies_tf_idf <- tidy_Movies %>%
count(Year, word, sort = TRUE) %>%
bind_tf_idf(word, Year, n) %>%
arrange(-tf_idf) %>%
group_by(Year) %>%
top_n(1) %>%
ungroup
Movies_tf_idf
#The importance of a word in the Movie Titles and Tv Show Titles
head(Movies_tf_idf,10) %>%
mutate(word = reorder_within(word, tf_idf,Year)) %>%
ggplot(aes(word, tf_idf, fill = Year)) +
geom_col(alpha = 0.5, show.legend = TRUE) +
coord_flip() +
theme(strip.text=element_text(size=7)) +
labs(x = NULL, y = "tf-idf",
title = "Highest tf-idf words in Movies & Tv Shows per Year")+
scale_x_reordered()
# Topic Modeling
#Cast the tidied dataset to a dtm:
Movies_dtm<-tidy_Movies %>%
count(Year, word) %>%
cast_dtm(Year, word, n)
#Traint he Topic Model
topic_model<-LDA(Movies_dtm, k=6, control = list(seed = 1234))
topic_model
#Posterior() determines the posterior probabilities of the topics for each document and of the terms for each topic for a fitted topic model.
posterior(topic_model)$topics
#Display top 15 words of each topic
terms(topic_model, k=15)
#Beta is the probability assignments of words to topics. In other words, Beta is the probablity that a word contributes to a topic.
topics <- tidy(topic_model, matrix = "beta")
topics
topics%>%
#let's group_by each topic
group_by(topic)%>%
#take the top 10 words in each topic
top_n(10)%>%
ungroup%>%
mutate(term = reorder_within(term, beta, topic)) %>%
ggplot(aes(term, beta, fill = topic)) +
geom_col(show.legend = FALSE) +
facet_wrap(~ topic, scales = "free") +
coord_flip() +
labs(x = NULL, y = expression(beta),
title = "Highest word probabilities for each topic",
subtitle = "Different words are associated with different topics")+
scale_x_reordered()
beta_spread <- topics %>%
mutate(topic = paste0("topic", topic)) %>%
spread(topic, beta) %>%
filter(topic1 > .001 | topic2 > .001) %>%
mutate(log_ratio = log2(topic2 / topic1))
beta_spread
#We can visualize the words with the greatest differences between the two topics:
beta_spread %>%
group_by(direction = log_ratio > 0) %>%
top_n(10, abs(log_ratio)) %>%
ungroup() %>%
mutate(term = reorder(term, log_ratio)) %>%
ggplot(aes(term, log_ratio)) +
geom_col() +
labs(y = "Log2 ratio of beta in topic 2 / topic 1") +
coord_flip ()
tidy(topic_model, matrix = "gamma") %>%
ggplot(aes(x=document, y=gamma)) +
geom_point(aes(color=factor(topic))) +
labs(x=NULL, y="Topic probability") +
coord_flip()
result <- FindTopicsNumber(
Movies_dtm,
topics = seq(from = 2, to = 40, by = 1),
metrics = c("Griffiths2004", "CaoJuan2009", "Arun2010", "Deveaud2014"),
method = "Gibbs",
control = list(seed = 77),
mc.cores = 2L,
verbose = TRUE)
FindTopicsNumber_plot(result)
# This shows that 5 or 6 topics was the ideal amount for the k value.
```
Column Descriptions
ID: A unique identifier for each movie within the dataset.
Title: The full title of the movie as it appears on the streaming platforms.
Year: The release year of the movie, indicating when the movie was first made available to the public.
Age: The recommended age group for the movie's audience, such as '7+', '13+', '16+', or '18+'.
Rotten Tomatoes: The movie's score on Rotten Tomatoes, which reflects critics' reviews and can be used as a measure of the movie's reception.
Netflix: A binary indicator (0 or 1) of whether the movie is available on Netflix, with 1 indicating availability.
Hulu: A binary indicator (0 or 1) of whether the movie is available on Hulu.
Prime Video: A binary indicator (0 or 1) of whether the movie is available on Amazon Prime Video.
Disney+: A binary indicator (0 or 1) of whether the movie is available on Disney+.
Type: A categorical indicator distinguishing the content as either a 'Movie' or a 'TV Show'.
Additional Dataset Attributes
Each row in the dataset represents a single movie, with its corresponding attributes spread across the aforementioned columns.
The dataset is comprehensive and potentially updated regularly, ensuring a current snapshot of movies across major streaming platforms.
There are columns for additional streaming platforms, which can be included in the analysis as needed.
Some entries may have missing values, especially in age ratings, which could be due to various factors such as the movie being unrated or newly released.