-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata preprocessing.R
319 lines (253 loc) · 10.3 KB
/
data preprocessing.R
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
library(tibble)
library(caTools)
library(dplyr)
library(tidytext)
library(tidyr)
library(ggplot2)
library(wordcloud)
library(reshape2)
library(VIM) # kNN Imputation
library(quantmod) # get currency
# DATA PREPROCESSING
# Read csv
dataset <- read.csv("documents/R Programming/data/movie_metadata.csv", stringsAsFactors = TRUE)
str(dataset)
# quick look
glimpse(dataset)
summary(dataset)
# Remove Duplicates
# calculate number of duplicate rows
sum(duplicated(dataset))
# delete duplicate rows
dataset <- distinct(dataset)
summary(dataset)
# NA imputation for all columns
for(column in 1:length(dataset))
dataset[,c(column)][dataset[,c(column)] == 0 | dataset[,c(column)] == ""] <- NA
# Detect outlier
# duration
summary(dataset$duration)
hist(dataset$duration, main = "Duration")
boxplot(dataset$duration, horizontal = TRUE, main = "Duration")
upper_bench <- 118 + 1.5 * IQR(dataset$duration, na.rm = TRUE)
upper_bench
lower_bench <- 93 - 1.5 * IQR(dataset$duration, na.rm = TRUE)
lower_bench
dataset <- subset(dataset,duration < upper_bench & duration > lower_bench)
boxplot(dataset$duration, horizontal = TRUE,main = "Duration")
# num_critic_for_reviews
summary(dataset$num_critic_for_reviews)
boxplot(dataset$num_critic_for_reviews, horizontal = TRUE, main = "Number Critic For Reviews")
upper_bench <- 194 + 1.5 * IQR(dataset$num_critic_for_reviews, na.rm = TRUE)
upper_bench
lower_bench <- 51 - 1.5 * IQR(dataset$num_critic_for_reviews, na.rm = TRUE)
lower_bench
dataset <- subset(dataset,num_critic_for_reviews < upper_bench & num_critic_for_reviews > lower_bench)
# budget
summary(dataset$budget)
boxplot(dataset$budget, main = "Budget")
upper_bench <- 12220000000 + 1.5 * IQR(dataset$budget, na.rm = TRUE)
upper_bench
lower_bench <- 6000000 - 1.5 * IQR(dataset$budget, na.rm = TRUE)
lower_bench
dataset <- subset(dataset,budget < upper_bench & budget > lower_bench)
boxplot(dataset$budget, main = "Budget", outline = FALSE)
# gross
summary(dataset$gross)
boxplot(dataset$gross, main = "Gross")
upper_bench <- 58918501 + 1.5 * IQR(dataset$gross, na.rm = TRUE)
upper_bench
lower_bench <- 6105175 - 1.5 * IQR(dataset$gross, na.rm = TRUE)
lower_bench
dataset <- subset(dataset,gross < upper_bench & gross > lower_bench)
boxplot(dataset$gross, main = "Gross", outline = FALSE)
# Missing Values
# calculate total missing values
colSums(sapply(dataset, is.na))
# linear regression imputation
columns <- c()
for(i in 1:dim(dataset)[2])
{
if(is.numeric(dataset[,i])|| is.integer(dataset[,i]))
{
columns[i]=T
}
else
{
columns[i]=F
}
}
temp <- na.omit(dataset[,columns])
correlation <- c()
for(i in 1:dim(temp)[2])
{
correlation[i] <- cor(temp[,i],temp[,'movie_facebook_likes'])
}
symnum(correlation)
# create indicator variable
Ind <- function(t)
{
x <- dim(length(t))
x[which(!is.na(t))] = 1
x[which(is.na(t))] = 0
return(x)
}
dataset$I <- Ind(dataset$movie_facebook_likes)
lm(movie_facebook_likes~num_critic_for_reviews, data = dataset)
summary(lm(movie_facebook_likes~num_critic_for_reviews, dataset))
# Missing Value imputation
for(i in 1:nrow(dataset))
if(dataset$I[i] == 0)
dataset$movie_facebook_likes[i] = round(-4051.6 + 102.2 * dataset$num_critic_for_reviews[i])
dataset$I <- NULL
# linear regression imputation for cast_total_facebook_likes
for(i in 1:dim(temp)[2])
{
correlation[i] <- cor(temp[,i],temp[,'cast_total_facebook_likes'])
}
symnum(correlation)
# create indicator variable
dataset$I <- Ind(dataset$cast_total_facebook_likes)
lm(cast_total_facebook_likes~actor_1_facebook_likes, data = dataset)
summary(lm(cast_total_facebook_likes~actor_1_facebook_likes, dataset))
# Missing Value imputation
for(i in 1:nrow(dataset))
if(dataset$I[i] == 0)
dataset$cast_total_facebook_likes[i] = round(2238.182 + 1.125 * dataset$actor_1_facebook_likes[i],0)
dataset$I <- NULL
# mean imputation for director_facebook_likes
dataset$director_facebook_likes[is.na(dataset$director_facebook_likes)] <- round(mean(dataset$director_facebook_likes[!is.na(dataset$director_facebook_likes)]),0)
# mean imputation for actor_1_facebook_likes
dataset$actor_1_facebook_likes[is.na(dataset$actor_1_facebook_likes)] <- round(mean(dataset$actor_1_facebook_likes[!is.na(dataset$actor_1_facebook_likes)]),0)
# mean imputation for actor_2_facebook_likes
dataset$actor_2_facebook_likes[is.na(dataset$actor_2_facebook_likes)] <- round(mean(dataset$actor_2_facebook_likes[!is.na(dataset$actor_2_facebook_likes)]),0)
# mean imputation for actor_3_facebook_likes
dataset$actor_3_facebook_likes[is.na(dataset$actor_3_facebook_likes)] <- round(mean(dataset$actor_3_facebook_likes[!is.na(dataset$actor_3_facebook_likes)]),0)
# mean imputation for facenumber_in_poster
dataset$facenumber_in_poster[is.na(dataset$facenumber_in_poster)] <- round(mean(dataset$facenumber_in_poster[!is.na(dataset$facenumber_in_poster)]),0)
# mean imputation for cast_total_facebook_likes
dataset$cast_total_facebook_likes[is.na(dataset$cast_total_facebook_likes)] <- round(mean(dataset$cast_total_facebook_likes[!is.na(dataset$cast_total_facebook_likes)]),0)
# median imputation for aspect_ratio
dataset$aspect_ratio[is.na(dataset$aspect_ratio)] <- round(median(dataset$aspect_ratio[!is.na(dataset$aspect_ratio)]),2)
# convert currency
getFX("KRW/USD", from = Sys.Date()-2, to = Sys.Date()-2)
KRW <- as.data.frame(KRWUSD) %>%
as.numeric()
getFX("JPY/USD", from = Sys.Date()-2, to = Sys.Date()-2)
JPY <- as.data.frame(JPYUSD) %>%
as.numeric()
getFX("TRY/USD", from = Sys.Date()-2, to = Sys.Date()-2)
TRY <- as.data.frame(TRYUSD) %>%
as.numeric()
getFX("HUF/USD", from = Sys.Date()-2, to = Sys.Date()-2)
HUF <- as.data.frame(HUFUSD) %>%
as.numeric()
getFX("THB/USD", from = Sys.Date()-2, to = Sys.Date()-2)
THB <- as.data.frame(THBUSD) %>%
as.numeric()
dataset <- transform(dataset, budget = ifelse(country == "South Korea", budget*KRW, budget))
dataset <- transform(dataset, budget = ifelse(country == "Japan", budget*JPY, budget))
dataset <- transform(dataset, budget = ifelse(country == "Turkey", budget*TRY, budget))
dataset <- transform(dataset, budget = ifelse(country == "Hungary", budget*HUF, budget))
dataset <- transform(dataset, budget = ifelse(country == "Thailand", budget*THB, budget))
dataset <- transform(dataset, gross = ifelse(country == "South Korea", gross*KRW, gross))
dataset <- transform(dataset, gross = ifelse(country == "Japan", gross*JPY, gross))
dataset <- transform(dataset, gross = ifelse(country == "Turkey", gross*TRY, gross))
dataset <- transform(dataset, gross = ifelse(country == "Hungary", gross*HUF, gross))
dataset <- transform(dataset, gross = ifelse(country == "Thailand", gross*THB, gross))
# remove rows
dataset <- dataset[!is.na(dataset$plot_keywords),]
dataset <- dataset[!is.na(dataset$language),]
# content_rating
table(dataset$content_rating)
dataset$content_rating[dataset$content_rating == "M"] <- 'PG'
dataset$content_rating[dataset$content_rating == "GP"] <- 'PG'
dataset$content_rating[dataset$content_rating == "X"] <- 'NC-17'
dataset$content_rating[dataset$content_rating == "Approved"] <- 'R'
dataset$content_rating[dataset$content_rating == "Passed"] <- 'R'
dataset$content_rating[dataset$content_rating == "Unrated"] <- 'R'
dataset$content_rating[dataset$content_rating == "Not Rated"] <- 'R'
dataset$content_rating <- factor(dataset$content_rating)
table(dataset$content_rating)
# language
dataset$country <- factor(dataset$country)
table(dataset$country)
plot(dataset$country)
levels(dataset$country) <- c(levels(dataset$country), "Others")
dataset$country[dataset$country != "USA" & dataset$country != "UK"] <- "Others"
dataset$country <- factor(dataset$country)
table(dataset$country)
plot(dataset$country)
# DATA TRANSFORMATION
dataset$id <- seq.int(nrow(dataset))
# Attribute Subset Selection
head(dataset)
aggregate(gross~language,dataset,sum)
aggregate(gross~title_year,dataset,sum)
dataset <- dataset %>%
mutate(profit = ifelse(gross - budget > 0, 1, 0))
# Generalization
tidy_plot_keyword <- tibble(line = 1:length(dataset[,1]),text = as.character(dataset$plot_keywords))
tidy_plot_keyword <- unnest_tokens(tidy_plot_keyword,word,text)
# tibble line-sentiment value
sentiment_category <- tidy_plot_keyword %>%
inner_join(get_sentiments("nrc")) %>%
filter(sentiment %in% c("positive",
"negative")) %>%
mutate(sentiment_value = ifelse(sentiment %in% c('positive'), 1,
-1)) %>%
group_by(line) %>%
summarise(sentiment_value = mean(sentiment_value)) %>%
#mutate(sentiment_value = ifelse(sentiment_value > 0, 1, 0)) %>%
mutate(id = line) %>%
data.frame()
dataset <- full_join(dataset,sentiment_category, by = 'id')
dataset <- dataset[!is.na(dataset$sentiment_value),]
dataset$line <- NULL
dataset$id <- NULL
dataset$sentiment_value = as.double(dataset$sentiment_value)
# encoding categorical variable
dataset$duration <- cut(dataset$duration,
breaks = c(0,60,120,180),
labels = c(0,1,2))
dataset$country <- factor(dataset$country,
levels = c("USA", "UK", "Others"),
labels = c(0, 1, 2))
dataset$content_rating <- factor(dataset$content_rating,
levels = c("G", "NC-17", "PG", "PG-13", "R"),
labels = c(0, 1, 2, 3, 4))
dataset$sentiment_value <- cut(dataset$sentiment_value,
breaks = c(-2,0,2),
labels = c(0,1))
# Normalization
# Feature Scaling
dataset[-c(2,11,16,18,19)] <- scale(dataset[-c(2,11,16,18,19)])
# Feature Selection
# movie_imdb_link
dataset$movie_imdb_link <- NULL
# movie_title
dataset$movie_title <- NULL
# plot_keywords
dataset$plot_keywords <- NULL
# actor_1_name
dataset$actor_1_name <- NULL
# actor_2_name
dataset$actor_2_name <- NULL
dataset$actor_3_name <- NULL
dataset$genres <- NULL
xtabs(~ color, data = dataset)
barplot(xtabs(~ color, data = dataset), xlab = "color", ylab = "number of movies")
dataset$color <- NULL
xtabs(~ director_name, data = dataset)
barplot(xtabs(~ director_name, data = dataset), xlab = "color", ylab = "number of movies")
dataset$director_name <- NULL
# stepwise selection
model <- lm(profit~.,dataset)
stepAIC(model,direction = "both")
#write.csv(dataset, file="cleaned.csv")
# Data Partition
# Splitting the dataset into the Training set and Test set
set.seed(123)
split = sample.split(dataset$profit, SplitRatio = 2/3)
training_set = subset(dataset, split == TRUE)
test_set = subset(dataset, split == FALSE)