-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode_Searchreport.R
More file actions
94 lines (75 loc) · 2.48 KB
/
Code_Searchreport.R
File metadata and controls
94 lines (75 loc) · 2.48 KB
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
#Set working directory
setwd("F:/Cosmodea")
remove(list = ls())
#Attach Libraries
library(data.table)
library(FSelector)
library(stringr)
library(dplyr)
library(caret)
library(lubridate)
library(klaR)
#Import file
Report <- fread("Search term report(1).csv")
#Summary of file
summary(Report)
#Change the column names
setnames(Report,
c("Match type", "Search term", "Added/Excluded", "Campaign",
"Ad group", "Clicks", "Impressions", "CTR", "Avg. CPC", "Cost",
"Avg. position", "Conversions", "Cost / conv.", "Conv. rate",
"All conv.", "View-through conv."),
c("Match_type", "Search_term", "Added/Excluded", "Campaign",
"Ad_group", "Clicks", "Impressions", "CTR", "Avg.CPC", "Cost",
"Avg.position", "Conversions", "Cost/conv", "Conv.rate",
"All_conv", "View_through_conv"))
#Convert CTR, Cost, Conv.rate & Conv.rate into numeric
Report <- Report %>%
mutate(
CTR = as.numeric(sub("%","",CTR)),
Cost = as.numeric(str_replace_all(Cost,",","")),
`Cost/conv` = as.numeric(str_replace_all(`Cost/conv`,",","")),
Conv.rate = as.numeric(sub("%","",Conv.rate))
)
#Check Structure of file
str(Report)
#Convert character variables into factor
Report[,c(1:4)] <- lapply(Report[,c(1:4)], factor)
#Check for important varibales#
att.scores <- random.forest.importance(Search_term ~ ., Report)
names(att.scores)
att_numeric <- att.scores$attr_importance
sort(att.scores$attr_importance,decreasing = T)
att.scores$attr_importance <- colnames(SAheart[,-10])
#PLot
cor(Report_new[,-1])
#Check the uniques items of Search_term
unique(Report$Search_term)
match(Report, Report$Search_term)
a <- as.data.frame(table(Report$Search_term))
write.csv(a,"Table.csv")
#Make New data
Report_new <- Report[,-c(1,3,4,5)]
Report_new$Search_term <- as.factor(Report_new$Search_term)
#Check for appropriate K value
wtss<-c()
for(i in 1:20)
{
model1<-kmeans(Report_new,i,iter.max = 5)
wtss<-c(wtss,model1$tot.withinss)
}
plot(wtss)
btss<-c()
for(i in 1:50)
{
model2<-kmeans(Report_new,i,iter.max = 5)
btss<-c(btss,model2$betweenss)
}
plot(btss)
model3 <- kmodes(Report_new,8,iter.max = 5)
model3
model4 <- kmeans(Report_new,8,iter.max = 100,nstart = 20)
Report_Searchterm <- Report$`Search term`
Report_1 <- fread("tf.csv")
Report_1 <- Report_1[-17744,]
MODEL_7 <- kmeans(Report_1,7,iter.max = 1,algorithm = "Forgy")