-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEmail_Campaigns_EDA_Adv.R
91 lines (69 loc) · 2.95 KB
/
Email_Campaigns_EDA_Adv.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
library(RMySQL)
library(data.table)
library(lubridate)
library(ggplot2)
try(source(file = "Cleaning_Functions.R"))
campaign <- as.data.table(read.csv("TravelCampaigns.csv")) #we use data.table for faster and more
campaign[,X:=NULL]
campaign[,email:=NULL]
campaign[,MessageId:=NULL]
campaign[,routerId:=NULL]
campaign[,programId:=NULL]
#efficient data manipulation
print(nrow(campaign)) # number of rows of the data set
print(ncol(campaign)) # number of columns for the data set
campaign$Open <- as.factor(campaign$Open)
levels(campaign$Open) <- c("NoOpen","Open")
#Cleaning subject
campaign$subject <- as.character(campaign$subject)
campaign$subject <- clean_subject(subject = campaign$subject)
summary(campaign)
table(campaign$Open)
print(paste0("Proportion of Opens ",table(campaign$Open)[2]/table(campaign$Open)[1]))
#Advanced Data vizualisation
campaign <- na.omit(campaign)
#Plots related to subjects
#Adding variables related to subject: if brand mentionned, if price quoted, if some words in all caps
campaign[,target_types:=user_targeting_type(campaign$subject)]
pl <- ggplot(campaign, aes(target_types, fill = Open)) + geom_bar(position = "fill")
plot(pl)
#Detecting quoted prices and promotions
campaign <- cbind(campaign, detect_prices(campaign$subject))
pl <- ggplot(campaign, aes(prices, fill = Open)) + geom_bar(position = "fill")
plot(pl)
pl <- ggplot(campaign, aes(promotion, fill = Open)) + geom_bar(position = "fill")
plot(pl)
#Detecting all caps
campaign[,caps:=detect_caps(campaign$subject)]
pl <- ggplot(campaign, aes(caps, fill = Open)) + geom_bar(position = "fill")
plot(pl)
#For the rest of the analysis we need to transform the timesend variable to have days of the week, months and hours
time_treatment_df <- convert_timestamp(campaign$timesend)
campaign <- cbind(campaign, time_treatment_df)
campaign[,timesend:=NULL] #deleting the timestamp variable
#Continuous variables
#Density plot code_post_densite_mean
pl <- ggplot(campaign, aes(code_post_densite_mean)) + geom_area(stat = "bin")
plot(pl)
#Density plot code_post_alt_mean
pl <- ggplot(campaign, aes(code_post_alt_mean)) + geom_area(stat = "bin")
plot(pl)
#boxplots against Open/NoOpen
pl <- ggplot(campaign, aes(Open, code_post_alt_mean)) + geom_boxplot()
plot(pl)
pl <- ggplot(campaign[code_post_densite_mean <=15,], aes(Open, code_post_densite_mean)) + geom_boxplot()
plot(pl)
#Correlation densité de population et altitude moyenne: négative, densité plus faible quand l'altitude augmente
cor.test(campaign$code_post_densite_mean, campaign$code_post_alt_mean)
#scatterplots
#alt_mean
pl <- ggplot(campaign, aes(y=code_post_alt_mean, x=Open)) + geom_point(position = "jitter", alpha = 0.1)
plot(pl)
#densite_pop
pl <- ggplot(campaign, aes(Open, code_post_densite_mean)) + geom_point(alpha = 0.01)
plot(pl)
#Other scatter plots
pl <- ggplot(campaign, aes(code_post_alt_mean, code_post_densite_mean)) +
geom_point(aes(colour=as.factor(Open)))
plot(pl)
#See the impact of unemployment, poverty and salary