-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities
333 lines (284 loc) · 13.2 KB
/
utilities
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
#Auxiliary function to plot the relationship between clustering variables
createCorrelationPlots <- function(filename, df, sampleSize = 500) {
if(!is.null(filename)){
corrPlot <- ggpairs(sample_n(df, sampleSize), aes(col="red", alpha=0.4), diag=list(continuous="density"), axisLabels='show')
png(filename, height=1500, width=1500)
print(corrPlot)
dev.off()
}
}
#' Function to create clusters and a standard report of the mean of some variables
#' The clustering is performed using kmeans algorith with the euclidean distance
#' Character and factor variables will be dummified before entering the clustering
#' As kmeans does not admit Infinite or NA values, they are na.roughfix'd (replaced by the median)
#'
#'
#' @param dt Master table with the prediction
#' @param clusteringVarnames Names of the variables that will be used for the clustering
#' @param shownVarnames Variables that will be included in the report but not used for the clustering
#' @param nClusters Number of clusters that will be created
#' @param scoreColName Name of the column that contains the model score, used for clustering only the highest values
#' @param nRowsToCluster Number of rows that will be considered for the clustering. Incompatible with "percRowsToCluster"
#' @param percRowsToCluster Percentage of rows that will be considered for the clustering. Incompatible with "nRowsToCluster"
#' @param scaleData Whether to scale data before clustering or not (subtract mean and divide by sd).
#' Recommended to set as true as euclidean distance is sensitive to order of magnitude of data
#' @param file Name of the file where the report will be saved (must be an xlsx file)
#' @param template Excel template where the format and coloring of the output file is saved
#' @param plotFilename Name of the file where a correlation plot of the different clustering variables will be saved
#' The output will be saved as png. If NULL no plotting will be done
#' @param plotSampleSize Sample size that will be used for the plot, as most likely all data won't be manageable
#' @param verbose If TRUE, prints some information about its execution
#'
#' @return returns a list with two elements:
#' clusteredData: The data that has been clustered with the clustering information
#' report: The summary report of the clusters
#' @export
#'
#' @examples
kMeansClustering <- function(dt,
clusteringVarnames,
shownVarnames,
nClusters,
scoreColName,
nRowsToCluster = NULL,
percRowsToCluster = NULL,
scaleData = TRUE,
file = NULL,
template = NULL,
plotFilename = NULL,
plotSampleSize = 500,
verbose = TRUE){
originalColNames <- colnames(dt)
setorderv(dt, scoreColName, order = -1)
#Error control
if(!is.null(nRowsToCluster) & !is.null(percRowsToCluster)){
stop("Only one of the following nRowsToCluster or percRowsToCluster should be not NULL")
}
if(!is.null(nRowsToCluster)){
rowsForClustering <- nRowsToCluster
}else if(!is.null(percRowsToCluster)){
rowsForClustering <- ceiling(percRowsToCluster * nrow(dt))
}else{
stop("Only one of the following nRowsToCluster or percRowsToCluster should be not NULL")
}
#We cant add to the clustering more variables than the table has
rowsForClustering <- min(nrow(dt), rowsForClustering)
#######################################################################
### Dummify factor and character variables
#######################################################################
tic()
cond_cat(verbose, "Dummifying factors and characters...\n")
varsToTreat <- unique(c(clusteringVarnames, shownVarnames))
varsToDummify <- intersect(varsToTreat, colnames(dt)[sapply(dt,class) %in% c("factor","character")])
numericVars <- setdiff(varsToTreat, varsToDummify)
#Store the new dummified variables names so that we add them to the clustering afterwards
dummifiedVars <- c()
dummifiedVarsForClustering <- c()
for(varToDummify in varsToDummify){
vals <- unique(dt[[varToDummify]])
dt[, (varToDummify %+% "_" %+% vals) := lapply(vals, function(x){as.numeric(get(varToDummify) == x)})]
dummifiedVars <- c(dummifiedVars, varToDummify %+% "_" %+% vals)
if(varToDummify %in% clusteringVarnames){
dummifiedVarsForClustering <- c(dummifiedVarsForClustering, varToDummify %+% "_" %+% vals)
}
}
#Add the dummy variables to the clustering. We put them in this patricular order so that all clustering variables are at the beginning
varsToTreat <- c(dummifiedVarsForClustering, numericVars, setdiff(dummifiedVars, dummifiedVarsForClustering))
clusteringVarnames <- c(dummifiedVarsForClustering,
intersect(numericVars, clusteringVarnames))
toc(quiet = !verbose)
#######################################################################
### Clustering
#######################################################################
#Only clusterize the top N rows
topClusterized <- dt[1:rowsForClustering]
dataToKmeans <- topClusterized[, clusteringVarnames, with = FALSE]
tic()
cond_cat(verbose, "Removing infinite values...\n")
dataToKmeans <- dataToKmeans[, lapply(.SD, function(x){ifelse(is.infinite(x), NA, x)})]
toc(quiet = !verbose)
tic()
cond_cat(verbose, "Checking columns quality...\n")
x <- sapply(clusteringVarnames, function(x){uniqueN(dataToKmeans[[x]], na.rm = T)})
if(any(x == 1)){
stop("Variables: " %+% paste(names(x)[x==1], collapse = ", ") %+% " have at most one non-NA value, which makes it unable to do kmeans on them, please run the clustering without these variables.")
}
toc(quiet = !verbose)
if(scaleData){
tic()
cond_cat(verbose, "Scaling data...\n")
dataToKmeans <- scale(dataToKmeans)
toc(quiet = !verbose)
}
if(!is.null(plotFilename)){
tic()
cond_cat(verbose, "Plotting correlations...\n")
createCorrelationPlots(plotFilename, topClusterized[, clusteringVarnames, with = FALSE], sampleSize = plotSampleSize)
toc(quiet = !verbose)
}
tic()
cond_cat(verbose, "Running kmeans...\n")
set.seed(1804)
auxKMeans <- kmeans(na.roughfix(dataToKmeans), centers= nClusters)
#We will redefine the clusters numeration so that cluster 1 is the smallest and cluster N is the largest
#This is done because the reports orders the clusters by size
clustersNewOrder <- names(sort(table(auxKMeans$cluster))) #This sorts the clusters by size
topClusterized[, cluster := "Cluster_" %+% str_pad(mapvalues(auxKMeans$cluster, clustersNewOrder, seq_len(nClusters)), width = 2, side = "left", pad = "0")]
toc(quiet = !verbose)
print(paste0("template file exists: ", file.exists(template)))
clustersSummary <- summarizeClusters(clusteredTable = topClusterized,
varsToShow = varsToTreat,
file = file,
template = template,
allPopulationTable = dt,
clusteringVarnames = clusteringVarnames,
verbose = verbose)
return(list(clusteredData = topClusterized, report = clustersSummary))
}
#' Function that creates a summary report with the mean of some variables for each cluster
#'
#' @param clusteredTable Data that has been clustered
#' @param varsToShow Names of the variables that will be shown in the report
#' @param file Name of the file where the report will be saved (must be an xlsx file)
#' @param template Excel template where the format and coloring of the output file is saved
#' @param allPopulationTable Table with info about all the data.
#' Useful when not all the data has been clusteredand want to compare the clustered data vs all the population
#' @param clusterColName Name of the column that contains the cluster
#' @param clusteredTableName Name that will be given in the report to the clustered Table
#' @param clusteringVarnames Variables that were used for the clustering. If any, a column will be added to the report telling if the variable was used for the clustering or its just being displayed.
#' @param verbose If TRUE, prints some information about its execution
#'
#' @return Returns the clusters summary report
#' @export
#'
#' @examples
summarizeClusters <- function(clusteredTable,
varsToShow,
file = NULL,
template = NULL,
allPopulationTable = NULL,
clusterColName = "cluster",
clusteredTableName = "Top Risky",
clusteringVarnames = c(),
verbose = FALSE
){
tic()
cond_cat(verbose, "Creating clusters report...\n")
clusteredTable[, clusterSize := .N, by=.(cluster)]
clusteredTable[, clusterPercentage := .N / nrow(clusteredTable), by = cluster]
if(!is.null(allPopulationTable)){
allPopulationTable[, clusterSize := nrow(allPopulationTable)]
allPopulationTable[, cluster := "Total Population"]
allPopulationTable[, clusterPercentage := 1]
}
unclusteredTable <- copy(clusteredTable)
unclusteredTable[, clusterSize := nrow(unclusteredTable)]
unclusteredTable[, cluster := clusteredTableName]
unclusteredTable[, clusterPercentage := 1]
masterTable <- rbind(clusteredTable, unclusteredTable, allPopulationTable, fill=TRUE)
clustersSummary <- masterTable[, lapply(.SD, mean, na.rm=TRUE), by=.(cluster), .SDcols = c("clusterSize", "clusterPercentage", varsToShow)]
setorder(clustersSummary, clusterSize)
#Transpose to make the data more readable
varNames <- colnames(clustersSummary)
clustersSummary <- transpose(clustersSummary)
clustersSummary <- cbind(data.table(Cluster = varNames), clustersSummary)
clustersSummary[, sapply(.SD, first)]
setnames(clustersSummary, sapply(clustersSummary, first))
clustersSummary <- clustersSummary[-1]
clustersSummary[, (colnames(clustersSummary)[-1]) := lapply(.SD, as.numeric), .SDcols = colnames(clustersSummary)[-1]]
if(length(clusteringVarnames) != 0){
clustersSummary[, usedForClustering := ifelse(varNames[-1] %in% clusteringVarnames, "Yes", "No")]
}
saveClustersSummary(clustersSummary = clustersSummary,
file = file,
template = template)
toc(quiet = !verbose)
return(clustersSummary)
}
#' Saves the clusters summary report to an Excel file based on a template
#'
#' @param clustersSummary Clusters report (output from summarizeClusters)
#' @param file Name of the file where the report will be saved (must be an xlsx file)
#' @param template Excel template where the format and coloring of the output file is saved
#'
#' @return Nothing
#' @export
#'
#' @examples
saveClustersSummary <- function(clustersSummary,
file = NULL,
template = NULL){
if (!is.null(file)) {
if(!is.null(template)){
if(!file.exists(template)){
warning("The provided template does not exist, running without template")
wb <- XLConnect::loadWorkbook(file, create=TRUE)
}else{
wb <- XLConnect::loadWorkbook(template, create=TRUE)
}
}else{
wb <- XLConnect::loadWorkbook(file, create=TRUE)
}
XLConnect::createSheet(wb,name="clustersSummary")
setStyleAction(wb,XLC$STYLE_ACTION.NONE)
XLConnect::writeWorksheet(wb,clustersSummary,sheet="clustersSummary", startRow=1, startCol=1)
cs <- createCellStyle(wb)
for(row in seq_len(nrow(clustersSummary))){
if(all(sapply(clustersSummary[row, -c("cluster", "usedForClustering"), with = FALSE], function(x){x %between% c(0,1)}))){
XLConnect::setCellStyle(wb, "clustersSummary!B" %+% (row+1) %+% ":ZZ" %+% (row+1), cellstyle = cs)
}
}
XLConnect::setBorder(cs, side = "all", type = XLC$BORDER.THIN, color = XLC$COLOR.BLACK)
XLConnect::setDataFormat(cs, "0%")
XLConnect::saveWorkbook(wb, file=file)
}
return(invisible(NULL))
}
#---------------------------------------------------------------------------------------
`%gn%` <- function(x, y) {
grep(y, ignore.case = T, x = names(x))
}
#' Wrapper for grepping values. Is NOT case sensitive
#'
#' @param x String vector
#' @param y Pattern
#'
#' @return Elements of string that fit the pattern
#' @export
#'
#' @examples
#'
#' c("hola", "adios", "cocacola") %gv% "ola"
#'
`%gv%` <- function(x, y) {
grep(y, ignore.case = T, x = x, value = T)
}
`%g%` <- function(x, y) {
grep(y, ignore.case = T, x = x)
}
#' Wrapper for paste0. Easy, simple and fast way to concatenate two strings.
#'
#' @param x
#' @param y
#'
#' @return
#' @export
#'
#' @examples
`%+%` <- function(x, y) {
paste0(x,y)
}
#' Conditional cat
#'
#' @param condFlag If TRUE, message in printed, otherwise not
#' @param ... Message
#'
#' @return
#' @export
#'
#' @examples
cond_cat <- function(condFlag = TRUE, ...){
if(condFlag){
cat(...)
}
}