-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_pre_processing.R
617 lines (387 loc) · 14.8 KB
/
data_pre_processing.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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# *************** Data Mining Mid Term **************** #
# #
# Group Members #
# Altamera, Kristel Suzeth #
# Cardiño, Joemar #
# Enorme, Karl Cedric #
# Saba, Ainah #
# San Joaquin, Gabriel Jean #
# #
# ***************************************************** #
# ****************** Data Exploration ***************** #
# install required packages and Use Library
if (!require(ggplot2)) install.packages("ggplot2")
if (!require(dplyr)) install.packages("dplyr")
if (!require(textstem)) install.packages("textstem")
library(ggplot2)
library(dplyr)
library(textstem)
# load the data from the CSV file
df <- read.csv("dataset.csv")
# set the width of the console to 120 characters
options(width = 120)
# check the structure of the data frame
str(df)
# checking dataset of each columns
# we can see the columns if has missing values
summary(df)
head(df$kzProductName, 5)
head(df$Summary, 5)
# obs and var
obs_count <- nrow(df)
var_count <- ncol(df)
colm_name <- colnames(df)
cat("obs. or rows->", obs_count, "\n")
cat("var. or cols->", var_count, "\n")
cat("column ->", colm_name, "\n")
# explore sample of the data (5 only since data is large)
head(df, 5)
# present rows value of each column temporarily
head(df$kzProductName, 5)
head(df$ProductPrice, 5)
head(df$Rate, 5)
head(df$Review, 5)
head(df$Summary, 5)
head(df$Sentiment, 5)
# ******************** Decision Tree ****************** #
# before pre-processing
# Load required library
if (!require(rpart)) install.packages("rpart")
if (!require(rpart.plot)) install.packages("rpart.plot")
if (!require(textstem)) install.packages("textstem")
library(tm)
library(rpart)
library(rpart.plot)
# Create a corpus of the text summaries
corpus <- Corpus(VectorSource(df$Summary))
# Create a document term matrix
dtm <- DocumentTermMatrix(corpus, control = list(stopwords = TRUE, minDocFreq = 10))
dtm <- removeSparseTerms(dtm, 0.99) # Remove sparse terms (allocation of memory)
dtm <- as.matrix(dtm) # Convert to matrix
# Add sentiment to the matrix
sentiment <- df$Sentiment
dtm_sentiment <- cbind(dtm, sentiment)
# Convert dtm_sentiment to a data frame
dtm_sentiment_df <- as.data.frame(dtm_sentiment)
# Fit the decision tree model
tree_model <- rpart(sentiment ~ ., data = dtm_sentiment_df, method = "class")
selected_features <- as.character(rownames(as.data.frame(summary(tree_model)$importance[,4] > 0)))
dtm_subset <- dtm[, selected_features] # Subset dtm using selected features
dtm_sentiment <- cbind(dtm_subset, sentiment) # Combine subset dtm with sentiment column
# r plot for decision tree (for balanced clean data)
rpart.plot(tree_model, extra = 2, type = 5, cex = 0.5)
rpart.plot(tree_model, extra = 2, fallen.leaves = FALSE, type = 5, cex = 0.5)
# ******************** The Sentiment ****************** #
unique(df$Sentiment)
sentiment_counts <- df %>% count(Sentiment)
sentiment_counts$total <- obs_count
sentiment_counts$Percentage <- paste0(
round((sentiment_counts$n / sentiment_counts$total) * 100, 2),
"%"
)
sentiment_counts
# plot start
options("scipen" = 100, "digits" = 4)
# this is not case sensitive (positive can also be Positive)
df_bar <- data.frame(
Sentiment = c("Positive", "Negative", "Neutral"),
Count = c(
sentiment_counts$n[sentiment_counts$Sentiment == "positive"],
sentiment_counts$n[sentiment_counts$Sentiment == "negative"],
sentiment_counts$n[sentiment_counts$Sentiment == "neutral"]
)
)
ggplot(df_bar, aes(x = Sentiment, y = Count, fill = Sentiment)) +
geom_bar(stat = "identity") +
ggtitle("Sentiment Analysis") +
xlab("Sentiment") +
ylab("Count")
# ****************** The kzProductName **************** #
# Create a frequency table of the kzProductName column
freq_table <- table(df$kzProductName)
# Sort the table in descending order
sorted_table <- sort(freq_table, decreasing = TRUE)
# Select the top 3 values
top_3_values <- head(sorted_table, n = 3)
# Calculate total number of values in the column
total <- sum(freq_table)
# Calculate percentage for each of the top 3 values
percentages <- round((top_3_values / total) * 100, 2)
# Calculate the percentage of all other values
other_percentage <- 100 - sum(percentages)
# Combine the values and percentages into a data frame
top_3_df <- data.frame(
kzProductName = c(names(top_3_values), "Others"),
Frequency = c(top_3_values, sum(freq_table) - sum(top_3_values)),
Percentage = c(paste0(percentages, "%"), paste0(other_percentage, "%"))
)
# Format the output to use fewer decimal places
top_3_df$Percentage <- format(
round(as.numeric(sub("%", "", top_3_df$Percentage)) / 100, 2),
nsmall = 2,
digits = 2
)
# Print the resulting data frame
print(top_3_df, row.names = FALSE)
# ********************* The Price ********************* #
normal_price <- sum(is.na(df$ProductPrice))
num_valid <- sum(!is.na(df$ProductPrice))
num_outlier <- sum(df$ProductPrice < 0)
# Print the results
cat("Number of normal prices: ", normal_price, "\n")
cat("Number of valid prices: ", num_valid, "\n")
cat("Number of outlier prices: ", num_outlier, "\n")
# see outlier prices
outlier_prices <- df$ProductPrice[df$ProductPrice < 0]
outlier_prices
# na.rm = TRUE argument tells R to ignore missing values when calculating the sum
# warning is present since it has outliers
df$ProductPrice <- as.double(df$ProductPrice)
total_price <- sum(df$ProductPrice, na.rm = TRUE)
# format the total price with commas and a dollar sign
formatted_price <- paste0(
"$",
format(total_price, big.mark = ",", scientific = FALSE)
)
# print the formatted total price
formatted_price # this is suppoed to be $935,082,929 when data is cleaned for ProductPrice
# find the lowest price
lowest_price <- min(df$ProductPrice, na.rm = TRUE)
# find the highest price
highest_price <- max(df$ProductPrice, na.rm = TRUE)
# print the lowest and highest prices
cat("The lowest price is", lowest_price, "\n")
cat("The highest price is", highest_price, "\n")
# remove rows with missing values in ProductPrice
df <- df[!is.na(df$ProductPrice), ]
# define breaks based on available values
breaks <- seq(min(unique(df$ProductPrice)),
max(unique(df$ProductPrice)),
length.out = 10
)
# create histogram with customized options
hist(df$ProductPrice,
breaks = breaks,
col = "lightblue",
border = "white",
main = "Distribution of Product Prices",
xlab = "Price (in USD)",
ylab = "Count",
xlim = c(min(breaks), max(breaks)),
ylim = c(0, max(hist(df$ProductPrice, breaks = breaks)$counts) * 1.1)
)
# price representation - will be represented again when it is cleaned to see differences
# ********************** The Rate ********************* #
# Summary statistics for Rate
summary(df$Rate)
# see outlier prices
any(df$Rate < 0)
outlier_rate <- df$Rate[df$Rate < 1 | df$Rate > 5]
outlier_rate
# convert to make graphing
df$Rate <- as.numeric(df$Rate)
# Visualize the distribution of Rate using a histogram
ggplot(df, aes(Rate)) +
geom_histogram(binwidth = 1, fill = "blue", alpha = 0.5) +
labs(x = "Rating", y = "Count") +
ggtitle("Distribution of Ratings")
# Calculate the mean rating
mean(df$Rate, na.rm = TRUE)
# Calculate the median rating
median(df$Rate, na.rm = TRUE)
# Calculate the mode rating
if (!require(modeest)) install.packages("modeest")
library(modeest)
mlv(df$Rate)
ggplot(df, aes(y = Rate)) +
geom_boxplot(fill = "blue", alpha = 0.5) +
labs(y = "Rating") +
ggtitle("Distribution of Ratings")
# ********************** The Review ******************* #
# Summary statistics for Review
summary(df$Review)
# Top 10 most frequent Review
top_review <- df %>%
count(Review, sort = TRUE) %>%
head(10)
top_review
# Bar plot of top 10 most frequent Review
ggplot(top_review, aes(x = Review, y = n)) +
geom_col() +
xlab("Review") +
ylab("Count") +
ggtitle("Top 10 most frequent Review")
# ********************* The Summary ******************* #
# Summary statistics for Summary
summary(df$Summary)
# Top 10 most common summaries
top_summary <- df %>%
count(Summary, sort = TRUE) %>%
head(10)
top_summary
# Bar plot of top 10 most frequent Review
ggplot(top_summary, aes(x = Summary, y = n)) +
geom_col() +
xlab("Summary") +
ylab("Count") +
ggtitle("Top 10 most frequent Summary")
# ***************************************************** #
# ****************** Data Pre-Process ***************** #
# load necessary libraries for cleaning the text
if (!require(tm)) install.packages("tm")
if (!require(ggplot2)) install.packages("ggplot2")
if (!require(textstem)) install.packages("textstem")
library(tm)
library(ggplot2)
library(textstem)
# Read in the dataset
df <- read.csv("dataset.csv", stringsAsFactors = FALSE)
# checking
summary(df)
# Check for missing values
sum(is.na(df))
# *********************** General ********************* #
# remove unnecessary columns
df <- df[c("kzProductName", "ProductPrice", "Rate", "Summary", "Sentiment")]
# rename column product name
colnames(df)[1] <- "ProductName"
# text pre-processing (lowering text)
df$ProductName <- tolower(df$ProductName)
head(df$ProductName, 5)
df$Summary <- tolower(df$Summary)
head(df$Summary, 5)
df$Sentiment <- tolower(df$Sentiment)
head(df$Sentiment, 5)
# Create a corpus from the text data
corpus <- Corpus(VectorSource(df$ProductName))
# Clean and preprocess the text
corpus <- corpus %>%
tm_map(content_transformer(iconv), to = "ASCII//TRANSLIT") %>%
tm_map(removePunctuation) %>%
tm_map(removeNumbers) %>%
tm_map(removeWords, stopwords("english")) %>%
tm_map(content_transformer(tolower)) %>%
tm_map(stripWhitespace) %>%
tm_map(function(x) lemmatize_strings(x, pos = "all"))
# remove single characters from the text
df$ProductName <- gsub("\\b\\w\\b", "", df$ProductName)
# Convert the preprocessed corpus to a plain text document
df$ProductName <- sapply(corpus, as.character)
# ********************* The Price ********************* # 248
# remove non-numeric characters and convert to numeric
df$ProductPrice <- as.double(gsub("[^0-9.]+", "", df$ProductPrice))
# check for missing values
missing_indices <- which(is.na(df$ProductPrice))
df[missing_indices, "ProductPrice"]
num_missing <- sum(is.na(df$ProductPrice))
num_missing
num_mismatched <- sum(is.na(as.double(df$ProductPrice))) - num_missing
num_mismatched
num_valid <- sum(!is.na(df$ProductPrice))
num_valid
# compute the mean and standard deviation of ProductPrice (excluding missing and mismatched values)
mean_price <- mean(df$ProductPrice[!is.na(df$ProductPrice) & !is.na(as.double(df$ProductPrice))])
sd_price <- sd(df$ProductPrice[!is.na(df$ProductPrice) & !is.na(as.double(df$ProductPrice))])
# print the results
print(paste("Valid:", num_valid))
print(paste("Mismatched:", num_mismatched))
print(paste("Missing:", num_missing))
print(paste("Mean:", mean_price))
print(paste("Std. Deviation:", sd_price))
# calculate the total price
total_price <- sum(df$ProductPrice, na.rm = TRUE)
# format the total price with commas and a dollar sign
formatted_price <- paste0("$", format(total_price, big.mark = ",", scientific = FALSE))
# print the formatted total price
formatted_price
# find the lowest price
lowest_price <- min(df$ProductPrice, na.rm = TRUE)
# find the highest price
highest_price <- max(df$ProductPrice, na.rm = TRUE)
# print the lowest and highest prices
cat("The lowest price is", lowest_price, "\n")
cat("The highest price is", highest_price, "\n")
# define breaks based on available values
breaks <- seq(min(unique(df$ProductPrice)),
max(unique(df$ProductPrice)),
length.out = 10
)
# create histogram with customized options
hist(df$ProductPrice,
breaks = breaks,
col = "lightblue",
border = "white",
main = "Distribution of Product Prices",
xlab = "Price (in USD)",
ylab = "Count",
xlim = c(min(breaks), max(breaks)),
ylim = c(0, max(hist(df$ProductPrice, breaks = breaks)$counts) * 1.1)
)
# ********************* The Rate ********************** #
summary(df$Rate)
# remove non-numeric characters and convert to numeric
df$Rate <- as.numeric(gsub("[^0-9.]+", "", df$Rate))
head(df$Rate, 5)
# Visualize the distribution of Rate using a histogram
ggplot(df, aes(Rate)) +
geom_histogram(binwidth = 1, fill = "blue", alpha = 0.5) +
labs(x = "Rating", y = "Count") +
ggtitle("Distribution of Ratings")
# Calculate the mean rating
mean(df$Rate, na.rm = TRUE)
# Calculate the median rating
median(df$Rate, na.rm = TRUE)
# Calculate the mode rating
if (!require(modeest)) install.packages("modeest")
library(modeest)
mlv(df$Rate)
ggplot(df, aes(y = Rate)) +
geom_boxplot(fill = "blue", alpha = 0.5) +
labs(y = "Rating") +
ggtitle("Distribution of Ratings")
# ******************* The Summary ********************* #
# check values
head(df$Summary, 5)
# Create a corpus and remove numbers and punctuation
corpus <- Corpus(VectorSource(df$Summary))
corpus <- tm_map(corpus, removeNumbers)
corpus <- tm_map(corpus, removePunctuation)
# Remove stopwords
corpus <- tm_map(corpus, removeWords, stopwords("english"))
# Lemmatize the words
corpus <- tm_map(corpus, function(x) lemmatize_strings(x, pos = "all"))
# Remove extra spaces
corpus <- tm_map(corpus, stripWhitespace)
# Replace the original column with the cleaned data
df$Summary <- unlist(sapply(corpus, as.character))
df$Summary
# [need to have a spell corrector for more accuracy]
# ******************** Decision Tree ****************** #
# Load required library
if (!require(rpart)) install.packages("rpart")
if (!require(rpart.plot)) install.packages("rpart.plot")
if (!require(tm)) install.packages("tm")
library(tm)
library(rpart)
library(rpart.plot)
# Create a corpus of the text summaries
corpus <- Corpus(VectorSource(df$Summary))
# Create a document term matrix
dtm <- DocumentTermMatrix(corpus, control = list(stopwords = TRUE, minDocFreq = 10))
dtm <- removeSparseTerms(dtm, 0.99) # Remove sparse terms (allocation of memory)
dtm <- as.matrix(dtm) # Convert to matrix
# Add sentiment to the matrix
sentiment <- df$Sentiment
dtm_sentiment <- cbind(dtm, sentiment)
# Convert dtm_sentiment to a data frame
dtm_sentiment_df <- as.data.frame(dtm_sentiment)
# Fit the decision tree model
tree_model <- rpart(sentiment ~ ., data = dtm_sentiment_df, method = "class")
selected_features <- as.character(rownames(as.data.frame(summary(tree_model)$importance[,4] > 0)))
dtm_subset <- dtm[, selected_features] # Subset dtm using selected features
dtm_sentiment <- cbind(dtm_subset, sentiment) # Combine subset dtm with sentiment column
# r plot for decision tree (for balanced clean data)
rpart.plot(tree_model, extra = 2, type = 5, cex = 0.5)
rpart.plot(tree_model, extra = 2, fallen.leaves = FALSE, type = 5, cex = 0.5)
# cleaned data frame is named `cleaned_df`
write.csv(df, file = "clean_data.csv", row.names = FALSE)