-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregatevisuals.R
442 lines (316 loc) · 12.6 KB
/
aggregatevisuals.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
# #!/usr/bin/env Rscript
# library("optparse")
#
#
# option_list = list(
# #make_option(c("-c", "--cores"), type="numeric", default=30,
# # help="set number of cores for parallelism", metavar="numeric"),
# make_option(c("-i", "--id"), type="numeric", default=NULL,
# help="set the client ID eg: -i 63, (will return results for MoneyGuru)", metavar="numeric"),
# make_option(c("-o", "--out"), type="numeric", default=54,
# help="set log output eg: -o 0, (will turn off logging) \n [default= %default / ie On]", metavar="numeric"),
# make_option(c("-l", "--lag"), type="numeric", default=1,
# help="set lag in days - [default= %default, ie today]", metavar="numeric")
# );
#
# opt_parser = OptionParser(option_list=option_list);
# opt = parse_args(opt_parser);
#
# if (is.null(opt$id)){
# print_help(opt_parser)
# stop("At least one argument must be supplied: clientID, using the -i switch \n\nExample: summarypipe -i 63 -o 0 (to process MoneyGuru.com's data for the past 90 days)\n\n\n", call.=FALSE)
# }
domains <- as.data.frame(unique(dt$Domain))
write.csv(domains, "domains.csv")
getwd()
setwd("/home/paul/Data/RandD/UK")
opt <- NULL
opt$start <- 1
opt$id <- 1
opt$lag <- 3
opt$out <- 0
opt$days <- 90
patt <- "^smonk"
options(stringsAsFactors = FALSE)
clientID <- opt$id
library(bookdown)
library(dplyr)
library(RcppRoll)
library(RollingWindow)
library(stringr)
library(foreach)
options(stringsAsFactors=T)
cwd <- "~/Data/RandD/AppData/"
kwpdir <- "~/Data/RandD/AppData/kwpdata/"
setwd(kwpdir)
#Change to correct folder
now <- Sys.Date() -(opt$lag)
now <- format(now, format="%Y-%m-%d")
now
clientID <- opt$id
clientID
dirname <- paste(now, clientID, sep="-")
dirname
fullpath <- paste(cwd, dirname,"/", sep = "")
fullpath
filepath <- paste(cwd, "kwpdata/", clientID, "_kwp.RDS", sep = "")
filepath
print("Processing...")
print(clientID)
print(filepath)
#Check for todays's client folder and if it's not there, put it there.
if (!dir.exists(fullpath)) {
dir.create(fullpath, showWarnings = TRUE, recursive = FALSE, mode = "0777")
print(paste("creating dir:", dirname, sep = " " ))
} else {
print("Data directory exists")
}
# Do cleaning in preparation for the loop tings!
if (dir.exists(fullpath)) {
cat("Getting list of files") #####################################
##################################### Load data
print("file pattern:-")
print(patt)
filelist <- list.files(path = fullpath, pattern = as.character(patt), all.files = FALSE,
full.names = FALSE, recursive = FALSE,
ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)
numfiles <- length(filelist)
} else {
stop("No directory of that name run 'pullrank.R' to populate with ranking data\n\n", call.=FALSE)
}
#####################################
require(stringr)
# #Go to client directory
setwd(fullpath)
print(paste("switched to:", fullpath, sep = " " ))
#Check for logs dir... and if it's not there, put it there.
if (!dir.exists(paste(fullpath,"logs", sep="/"))) {
dir.create(paste(fullpath,"logs", sep="/"), showWarnings = TRUE, recursive = FALSE, mode = "0777")
print(paste("creating dir:", fullpath, sep = " " ))
} else {
print("Log directory exists ")
}
#Test for output switch to logging console output
if (opt$out == 1){
print(paste("piping logs to: ",fullpath,"logs/aggregated.txt", sep = ""))
#create the summarylog.txt
writeLines(c(""), paste(fullpath,"logs/aggregated.txt", sep = ""))
#redirect output to summarylog.txt
sink(paste(fullpath,"logs/summarylog.txt", sep = ""), append = TRUE)
}
file_b <- NULL
###
### Load the 3 monthly samples
foreach(i = 1:numfiles, .packages = c("dplyr", "RollingWindow")) %do% {
file_i <- readRDS(paste(fullpath, filelist[i], sep = "/"))
file_b <- rbind(file_i, file_b)
}
df <- file_b %>% dplyr::select(-Category)
df$Keyword <- as.factor(df$Keyword)
df$Domain <- as.factor(df$Domain)
df$Category <- as.factor(df$Category)
df$SLD <- as.factor(df$SLD)
dt <- df
##### figure out leaderboard plus and minus 5 and their change
summary(dt)
three <- dt %>%
dplyr::filter(monSlice == 3) %>%
dplyr::select(monSlice, Keyword, Domain, rsum, pmean, isum, Product, NumberKeywords) %>%
dplyr::group_by(monSlice, Domain, Product) %>%
summarise(rsum = sum(rsum),
isum = sum(isum),
pmean = mean(pmean),
n = n())
Out_three<- three %>%
dplyr::ungroup() %>%
dplyr::select(Domain, rsum, pmean, isum, Product, n) %>%
dplyr::group_by(Product) %>% dplyr::arrange(desc(rsum))%>%
dplyr::mutate(id = row_number())
two <- dt %>%
dplyr::filter(monSlice == 2) %>%
dplyr::select(monSlice, Keyword, Domain, rsum, pmean, isum, Product, NumberKeywords) %>%
dplyr::group_by(monSlice, Domain, Product) %>%
summarise(rsum = sum(rsum),
isum = sum(isum),
pmean = mean(pmean),
n = n())
Out_two <- two %>%
dplyr::ungroup() %>%
dplyr::select(Domain, rsum, pmean, isum, Product, n) %>%
dplyr::group_by(Product) %>% dplyr::arrange(desc(rsum)) %>%
dplyr::mutate(id = row_number())
one <- dt %>%
dplyr::filter(monSlice == 1) %>%
dplyr::select(monSlice, Keyword, Domain, rsum, pmean, isum, Product, NumberKeywords) %>%
dplyr::group_by(monSlice, Domain, Product) %>%
summarise(rsum = sum(rsum),
isum = sum(isum),
pmean = mean(pmean),
n = n())
Out_one <- one %>%
dplyr::ungroup() %>%
dplyr::select(Domain, rsum, pmean, isum, Product, n) %>%
dplyr::group_by(Product) %>% dplyr::arrange(desc(rsum)) %>%
dplyr::mutate(id = row_number())
blocklist <- c("wikipedia.org", "aol.com" , "twitter.com", "facebook.com")
################################################################################################################################################################################################
################################################################################################
if (dir.exists(fullpath)) {
cat("Getting list of files")
filelist <- list.files(path = fullpath, pattern = "^smonk", all.files = FALSE,
full.names = FALSE, recursive = FALSE,
ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)
numfiles <- length(filelist)
} else {
stop("No directory of that name run 'pullrank.R' to populate with ranking data\n\n", call.=FALSE)
}
df <- file_b
df$Keyword <- as.factor(df$Keyword)
df$Domain <- as.factor(df$Domain)
df$Category <- as.factor(df$Category)
df$SLD <- as.factor(df$SLD)
HCL2 <- read.csv("~/Data/RandD/UK/HCL2.csv",stringsAsFactors = TRUE)
GamingTypes <- read.csv("~/Data/RandD/UK/GamingTypes.csv", stringsAsFactors = TRUE)
gt <- GamingTypes[,c(1,2)]
#######################Cont here
dtmaster <- inner_join(df, HCL2, by = "Keyword")
dt$Keyword <- as.factor(dt$Keyword)
dt$Product <- as.factor(dt$Product)
dt$monSlice <- as.factor(dt$monSlice)
mdf <- dt %>% dplyr::select(monSlice, Keyword, Domain, rsum, pmean, isum) %>%
dplyr::group_by(monSlice, Domain, Keyword) %>%
summarise(rsum = sum(rsum),
isum = sum(isum),
pmean = mean(pmean),
n = n()) %>% top_n(10, -rsum)
##### figure out leaderboard plus and minus 5 and their change
all_iGaming_Sites <- read.csv("~/Data/iGaming/all_iGaming_Sites.csv", stringsAsFactors = TRUE)
library(tldextract)
urllist <- as.character(all_iGaming_Sites$Website)
# get most recent TLD listings
tld <- getTLD() # optionally pass in a different URL than the default
manyhosts <- c(urllist)
newurls <- tldextract(urllist, tldnames=tld)
all_iGaming_Sites$SLD <- as.factor(newurls$domain)
all_iGaming_Sites$Domain <- as.factor(paste(newurls$domain, newurls$tld, sep="."))
igdf <- as.data.frame(all_iGaming_Sites$Domain)
write.csv(igdf,"~/Data/RandD/UK/igdf.csv")
# Do cleaning in preparation for the loop tings!
if (dir.exists(fullpath)) {
cat("Getting list of files")
filelist <- list.files(path = fullpath, pattern = "^sl7kw", all.files = FALSE,
full.names = FALSE, recursive = FALSE,
ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)
numfiles <- length(filelist)
} else {
stop("No directory of that name run 'pullrank.R' to populate with ranking data\n\n", call.=FALSE)
}
#####################################
require(stringr)
# #Go to client directory
setwd(fullpath)
print(paste("switched to:", fullpath, sep = " " ))
#Check for logs dir... and if it's not there, put it there.
if (!dir.exists(paste(fullpath,"logs", sep="/"))) {
dir.create(paste(fullpath,"logs", sep="/"), showWarnings = TRUE, recursive = FALSE, mode = "0777")
print(paste("creating dir:", fullpath, sep = " " ))
} else {
print("Log directory exists ")
}
#Test for output switch to logging console output
if (opt$out == 1){
print(paste("piping logs to: ",fullpath,"logs/aggregated.txt", sep = ""))
#create the summarylog.txt
writeLines(c(""), paste(fullpath,"logs/aggregated.txt", sep = ""))
#redirect output to summarylog.txt
sink(paste(fullpath,"logs/summarylog.txt", sep = ""), append = TRUE)
}
file_b7 <- NULL
file_i <- NULL
### Load the 3 monthly samples
foreach(i = 1:numfiles, .packages = c("dplyr", "RollingWindow")) %do% {
file_i <- readRDS(paste(fullpath, filelist[i], sep = "/"))
file_b7 <- rbind(file_i, file_b7)
}
mdf7 <- file_b7
df7m <- inner_join(mdf7, HCL2, by = "Keyword")
df7m$Keyword <- as.factor(df7m$Keyword)
df7m$Category <- as.factor(df7m$Category)
df7m$Product <- as.factor(df7m$Product)
df7m$monSlice <- as.factor(df7m$monSlice)
#######summaries
#Fix Dates
mdf <- df7m %>% dplyr::select(monSlice, Keyword, Domain, rsum, pmean, isum) %>%
dplyr::group_by(monSlice, Domain, Keyword) %>%
summarise(rsum = sum(rsum),
isum = sum(isum),
pmean = mean(pmean),
n = n()) %>% top_n(10, -rsum)
##### figure out leaderboard plus and minus 5 and their change
df7m <- inner_join(mdf7, HCL2, by = "Keyword")
df7m$Keyword <- as.factor(df7m$Keyword)
df7m$Category <- as.factor(df7m$Category)
df7m$Product <- as.factor(df7m$Product)
df7m$monSlice <- as.factor(df7m$monSlice)
df7m$Domain <- as.factor(df7m$Domain)
df7m$SLD <- as.factor(df7m$SLD)
df7m <- ungroup(df7m)
latest <- df7m %>%
dplyr::select(Keyword, Domain, rsum, pmean, isum, Product, NumberKeywords) %>%
dplyr::group_by(Domain, Product) %>%
summarise(rsum = sum(rsum),
isum = sum(isum),
pmean = mean(pmean),
n = n())
Out_latest_byProduct <- latest %>%
dplyr::ungroup() %>%
dplyr::select(Domain, rsum, pmean, isum, Product, n) %>%
dplyr::group_by(Product) %>% dplyr::arrange(desc(rsum)) %>%
dplyr::mutate(id = row_number())
Out_latest <- Out_latest_byProduct %>%
dplyr::ungroup() %>%
dplyr::select(Domain, rsum, pmean, isum, Product, n) %>%
dplyr::arrange(desc(rsum)) %>%
dplyr::mutate(id = row_number())
##### Biggest Movers
library(dplyr)
first <- Out_one %>% ungroup() %>% dplyr::select(Domain, Product,n, id)
second <- Out_two %>% ungroup() %>% dplyr::select(Domain, Product,n, id)
third <- Out_three %>% ungroup() %>% dplyr::select(Domain, Product,n, id)
colnames(first) <- c("Domain", "Product","n_1" , "id_1")
colnames(second) <- c("Domain", "Product", "n_2", "id_2")
colnames(third) <- c("Domain", "Product", "n_3", "id_3")
joined <- inner_join(second, third, by = c("Domain", "Product"))
joined$id_diff <- -(joined$id_3 - joined$id_2 )
joined$n_diff <- joined$n_3 - joined$n_2
joined %>% dplyr::arrange(id_diff) %>% top_n(100, )
q <- ggplot(df_cbr_1, aes(x = csum, y = pmean, size=nkw, color = Client, label=Keyword)) +
geom_point(alpha = 1/3, show.legend = FALSE ) +
scale_color_manual(values=c(y="#286A99",n="#EE6B33"))+
xlab("Clicks") +
ylab("Avg Position") +
scale_y_reverse()+
scale_x_log10()+
scale_size(range = c(2, 10)) +
geom_text(size=2, alpha=1, color = "#40403E")
q
q <- ggplot(df_cbr_1, aes(x = csum, y = pmean, size=nkw, color = Client, label=Domain)) +
geom_point(alpha = 1/3, show.legend = FALSE ) +
scale_color_manual(values=c(y="#286A99",n="#EE6B33"))+
xlab("Clicks") +
ylab("Avg Position") +
scale_y_reverse()+
scale_x_continuous()+
scale_size(range = c(2, 10)) +
geom_text(size=2, alpha=1, color = "#40403E")
q
q <- ggplot(df_cbr_1, aes(x = csum, y = pmean, size=nkw, color = Client, label=Domain)) +
geom_point(alpha = 1/3, show.legend = FALSE ) +
scale_color_manual(values=c(y="#286A99",n="#EE6B33"))+
xlab("Clicks") +
ylab("Avg Position") +
scale_y_reverse()+
scale_x_continuous()+
scale_size(range = c(2, 10)) +
geom_text(size=2, alpha=1, color = "#40403E")
q