-
Notifications
You must be signed in to change notification settings - Fork 1
/
.Rhistory
512 lines (512 loc) · 20.4 KB
/
.Rhistory
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
par(new=T, mar=c(bottom_buffer,0,0,0)+all_buffer)
plot(tsne$Y,
col=ifelse(Y==1, yes=caseColors[1], no=caseColors[2]), # color code by outcome
pch=ifelse(Y==Yhat, yes=symb[1], no=symb[2]), # symbol code by correct classification
lwd=ifelse(Y==Yhat, yes=0, no=1),
cex = pointSize,
xlim=c(min(tsne$Y[,1]), max(tsne$Y[,1])), ylim=c(min(tsne$Y[,2]), max(tsne$Y[,2])),
xaxt='n', yaxt='n', xlab="", ylab="")
## Legend
if(legend_under_plot){
legend(x=min(tsne$Y[,1])+legend_offset[1], y=min(tsne$Y[,2])-1+legend_offset[2], ncol=2,
legend=c("True positive", "True negative", "False negative", "False positive"),
bg=legend_bg_col, col=c(caseColors, caseColors),
pch=c(symb[1], symb[1], symb[2], symb[2]), box.lty = 0, cex=textSize,
xpd=T)
} else { ## legend inside plot
legend(legend_position, inset=legend_offset,
legend=c(shade_name, "Y=1", "Y=0"),
bg=legend_bg_col, col=c(boxColor, caseColors),
pch=c(15, rep(symb, 2)), box.lty = 0, cex=textSize)
}
}
#' Get predicted outcomes
#'
#' Get the outcomes that a rule set would predict, i.e. Y=1 iff x \in ruleSet
#'
#' @param data data frame out independent variables without outcome
#' @param ruleSet rule set
#' @return predicted outcomes for each observation
.getYhat <- function(data, ruleSet){
# when p_pos=1 and p_neg=0, .getY returns yhat (.getY returns 1 iff x \in ruleSet)
return(.getY(data, ruleSet))
}
#' Generate outcome
#'
#' Generates outcomes based on a rule set and the probability that the outcome
#' is positive conditional on whether an observation satisfies the rule set
#'
#' @param X data frame with binary data for independent variables
#' @param ruleSet true rule set with which to generate data
#' @param p_pos probability of yn=1 if xn satisfies rule (default 1),
#' OR vector of probabilities (p_pos[i] corresponds to ruleSet[i])
#' @param p_neg probability of yn=1 if xn does NOT satisfy rule (default 0),
#' OR vector of probabilities (p_neg[i] corresponds to ruleSet[i])
#' @return a vector of outcomes
.getY <- function(X, ruleSet, p_pos=1, p_neg = 0){
nameSet <- list()
valueSet <- list()
n <- nrow(X)
for(i in 1:length(ruleSet)){
rule <- ruleSet[[i]]
nameSet[[i]] <- getNames(rule)
valueSet[[i]] <- getValues(rule)
}
# Outcome
Y <- rep(0, n)
rand <- runif(n)
pp <- p_pos
pn <- p_neg
for(i in 1:length(nameSet)){
Y_i <- apply(X[nameSet[[i]]], 1, function(x) all(x==valueSet[[i]])) # Cases that satisfy ith rule
if(length(p_pos) > 1){
pp <- p_pos[i]
}
if(length(p_neg) > 1){
pn <- p_neg[i]
}
Y <- as.numeric(Y == 1 | (Y_i == 1 & rand <= pp) )# 1 if satisfies ith or any previous rule
Y <- as.numeric(Y == 1 | (Y_i == 0 & rand <= pn) )# 1 if satisfies ith or any previous rule
}
return(Y)
}
plot_tsne(X = lipset_df, Y = lipset_Y, ruleSet=ruleset, pointSize = 1.25, symb = c(20, 4),
caseColors=prettyGraphs::add.alpha(RColorBrewer::brewer.pal(11, "RdGy")[c(8,10)], alpha=1))
## Functions for making t-SNE plot
#' Make a t-SNE plot
#'
#' Makes a t-SNE plot of the data and using a rule set. Color codes based on
#' actual outcome and symbol codes based on classification outcome
#'
#' @param X data, excluding outcome
#' @param Y outcome
#' @param ruleSet rule set for classification. Will only use variables that
#' appear in the rule set to train tsne model
#' @param caseColors a vector of colors, the first for Y=1 the second for Y=0
#' @param symb a numeric vector to determine symbol type (pch for plot), the
#' first for Yhat=1 and the second for Yhat=0
#' @param pointSize graphical parameter for size of points (cex for plot)
#' @param textSize graphical parameter for size of text (cex for legend)
#' @param bottom_buffer graphical parameter for adding white space to the
#' bottom of the plot to make room for legend
#' @param all_buffer graphical parameter for adding white space around plot
#' @param legend_under_plot logical for whether the legend should be under the
#' plot. If false, the legend will be inside the plot
#' @param legend_bg_col the background color of the legend
#' @param legend_offset a vector of how much to offset the legend along each
#' axis
#' @param jitter_factor the factor input for the jitter function
#' @param jitter_amount the amount input for the jitter function
#' @param max_iter the maximum iteration to run tsne for
#' @param highlight the index (or indices) of the rule in ruleSet to be highlighted.
#' The resulting graph will highlight ruleSet[highlight]
#' @return a t-SNE plot
plot_tsne <- function(X, Y, ruleSet, caseColors, symb=c(1, 4),
pointSize=1, textSize=1,
bottom_buffer=1.25, all_buffer=1,
legend_under_plot=T, legend_bg_col="transparent",
legend_offset=c(0,0), legend_position="bottomright",
jitter_factor=1, jitter_amount=NULL,
max_iter=1000,
highlight=NULL, box_color=NULL){
## Include only outcome and features in ruleSet
incl <- c() # variables to include
for (rule in ruleSet) {
for (cond in rule) {
split <- strsplit(cond, "_")[[1]]
is_neg <- as.numeric(split[length(split)]=="neg")
incl <- c(incl, paste(split[1:(length(split)-is_neg)], collapse="_"))
}
}
## Run t-SNE
train <- cbind.data.frame(Y, X[, colnames(X)[which(colnames(X) %in% incl)]])
tsne <- Rtsne::Rtsne(train, dims=2, perplexity=5, verbose=F, max_iter=max_iter, check_duplicates=F)#, partial_pca=T)
coord_nojitter <- tsne$Y
Yhat <- .getYhat(X, ruleSet) # Classification based on ruleSet
## Jitter
tsne$Y <- jitter(tsne$Y, factor=jitter_factor, amount=jitter_amount)
## Standardize coordinates
rangeX <- max(tsne$Y[,1])-min(tsne$Y[,1])
rangeY <- max(tsne$Y[,2])-min(tsne$Y[,2])
tsne$Y[,1] <- tsne$Y[,1]/rangeX*100
tsne$Y[,2] <- tsne$Y[,2]/rangeY*100
## Highlight; plot before points otherwise will cover them up
boxWidth = (max(tsne$Y[,1]) - min(tsne$Y[,1]))/25
boxHeight = (max(tsne$Y[,2]) - min(tsne$Y[,2]))/25
par(mar=c(bottom_buffer,0,0,0)+all_buffer)
plot(2*max(tsne$Y), xlim=c(min(tsne$Y[,1]), max(tsne$Y[,1])), ylim=c(min(tsne$Y[,2]), max(tsne$Y[,2])),
xlab="", ylab="",
xaxt='n', yaxt='n')
for (a in ruleSet[highlight]) { # For each rule we want to plot
if (length(rule)==1) { # One condition
# Features and values
split <- strsplit(a, "_")[[1]]
is_neg <- as.numeric(split[length(split)]=="neg")
feature <- paste(split[1:(length(split)-is_neg)], collapse="_")
value <- as.numeric(!is_neg)
tsne_pos <- tsne$Y[X[,feature]==value,]
Yhat[Yhat==0] <- as.numeric(X[,feature]==value)[Yhat==0] # If satisfies rule, then classified as positive (if doesn't, not recoded)
} else { # multiple conditions
split_conds <- strsplit(a, "_")
features <- c()
values <- c()
for (j in 1:length(a) ) {
split <- split_conds[[j]]
is_neg <- as.numeric(split[length(split)]=="neg")
features[j] <- paste(split[1:(length(split)-is_neg)], collapse="_")
values[j] <- as.numeric(!is_neg)
}
values_matrix <- matrix(rep(values, nrow(X)), ncol=length(values), byrow=T)
tsne_pos <- tsne$Y[ apply(X[,features]==values_matrix, 1, all), ] # tsne coordinates of cases that satisfy the rule
Yhat[Yhat==0] <- as.numeric(apply(X[,features]==values_matrix, 1, all))[Yhat==0]
}
if (!is.null(nrow(tsne_pos))) { # Check multiple rows
for (j in 1:nrow(tsne_pos)) { # For each case that satisfies the rule
# Draw a box around it
rect(xleft=tsne_pos[j,1]-boxWidth/2, xright=tsne_pos[j,1]+boxWidth/2,
ybottom=tsne_pos[j,2]-boxHeight/2, tsne_pos[j,2]+boxHeight/2,
border=NA,
lwd=10,
col="grey")
}
} else { # Vector, only 1 "row"
rect(xleft=tsne_pos[1]-boxWidth/2, xright=tsne_pos[1]+boxWidth/2,
ybottom=tsne_pos[2]-boxHeight/2, tsne_pos[2]+boxHeight/2,
border=NA,
lwd=10,
col=boxColor)
}
}
## Plot points
par(new=T, mar=c(bottom_buffer,0,0,0)+all_buffer)
plot(tsne$Y,
col=ifelse(Y==1, yes=caseColors[1], no=caseColors[2]), # color code by outcome
pch=ifelse(Y==Yhat, yes=symb[1], no=symb[2]), # symbol code by correct classification
lwd=ifelse(Y==Yhat, yes=0, no=1),
cex = pointSize,
xlim=c(min(tsne$Y[,1]), max(tsne$Y[,1])), ylim=c(min(tsne$Y[,2]), max(tsne$Y[,2])),
xaxt='n', yaxt='n', xlab="", ylab="")
## Legend
if(legend_under_plot){
legend(x=min(tsne$Y[,1])+legend_offset[1], y=min(tsne$Y[,2])-1+legend_offset[2], ncol=2,
legend=c("True positive", "True negative", "False negative", "False positive"),
bg=legend_bg_col, col=c(caseColors, caseColors),
pch=c(symb[1], symb[1], symb[2], symb[2]), box.lty = 0, cex=textSize,
xpd=T)
} else { ## legend inside plot
legend(legend_position, inset=legend_offset,
legend=c(shade_name, "Y=1", "Y=0"),
bg=legend_bg_col, col=c(boxColor, caseColors),
pch=c(15, rep(symb, 2)), box.lty = 0, cex=textSize)
}
}
#' Get predicted outcomes
#'
#' Get the outcomes that a rule set would predict, i.e. Y=1 iff x \in ruleSet
#'
#' @param data data frame out independent variables without outcome
#' @param ruleSet rule set
#' @return predicted outcomes for each observation
.getYhat <- function(data, ruleSet){
# when p_pos=1 and p_neg=0, .getY returns yhat (.getY returns 1 iff x \in ruleSet)
return(.getY(data, ruleSet))
}
#' Generate outcome
#'
#' Generates outcomes based on a rule set and the probability that the outcome
#' is positive conditional on whether an observation satisfies the rule set
#'
#' @param X data frame with binary data for independent variables
#' @param ruleSet true rule set with which to generate data
#' @param p_pos probability of yn=1 if xn satisfies rule (default 1),
#' OR vector of probabilities (p_pos[i] corresponds to ruleSet[i])
#' @param p_neg probability of yn=1 if xn does NOT satisfy rule (default 0),
#' OR vector of probabilities (p_neg[i] corresponds to ruleSet[i])
#' @return a vector of outcomes
.getY <- function(X, ruleSet, p_pos=1, p_neg = 0){
nameSet <- list()
valueSet <- list()
n <- nrow(X)
for(i in 1:length(ruleSet)){
rule <- ruleSet[[i]]
nameSet[[i]] <- .getNames(rule)
valueSet[[i]] <- getValues(rule)
}
# Outcome
Y <- rep(0, n)
rand <- runif(n)
pp <- p_pos
pn <- p_neg
for(i in 1:length(nameSet)){
Y_i <- apply(X[nameSet[[i]]], 1, function(x) all(x==valueSet[[i]])) # Cases that satisfy ith rule
if(length(p_pos) > 1){
pp <- p_pos[i]
}
if(length(p_neg) > 1){
pn <- p_neg[i]
}
Y <- as.numeric(Y == 1 | (Y_i == 1 & rand <= pp) )# 1 if satisfies ith or any previous rule
Y <- as.numeric(Y == 1 | (Y_i == 0 & rand <= pn) )# 1 if satisfies ith or any previous rule
}
return(Y)
}
#' Get variable names for a rule
#'
#' Get the names of variables as they appear in the data corresponding to the
#' conditions in a rule
#'
#' @param rule a rule, formatted as a vector of conditions
#' @return vector of names of variables correspoding to the conditions in rule
.getNames <- function(rule){
split <- strsplit(rule, "_")
names <- c()
for(i in 1:length(split)){
names[i] <- paste(split[[i]][split[[i]] != "neg"], collapse="_")
}
return(names)
}
plot_tsne(X = lipset_df, Y = lipset_Y, ruleSet=ruleset, pointSize = 1.25, symb = c(20, 4),
caseColors=prettyGraphs::add.alpha(RColorBrewer::brewer.pal(11, "RdGy")[c(8,10)], alpha=1))
## Functions for making t-SNE plot
#' Make a t-SNE plot
#'
#' Makes a t-SNE plot of the data and using a rule set. Color codes based on
#' actual outcome and symbol codes based on classification outcome
#'
#' @param X data, excluding outcome
#' @param Y outcome
#' @param ruleSet rule set for classification. Will only use variables that
#' appear in the rule set to train tsne model
#' @param caseColors a vector of colors, the first for Y=1 the second for Y=0
#' @param symb a numeric vector to determine symbol type (pch for plot), the
#' first for Yhat=1 and the second for Yhat=0
#' @param pointSize graphical parameter for size of points (cex for plot)
#' @param textSize graphical parameter for size of text (cex for legend)
#' @param bottom_buffer graphical parameter for adding white space to the
#' bottom of the plot to make room for legend
#' @param all_buffer graphical parameter for adding white space around plot
#' @param legend_under_plot logical for whether the legend should be under the
#' plot. If false, the legend will be inside the plot
#' @param legend_bg_col the background color of the legend
#' @param legend_offset a vector of how much to offset the legend along each
#' axis
#' @param jitter_factor the factor input for the jitter function
#' @param jitter_amount the amount input for the jitter function
#' @param max_iter the maximum iteration to run tsne for
#' @param highlight the index (or indices) of the rule in ruleSet to be highlighted.
#' The resulting graph will highlight ruleSet[highlight]
#' @return a t-SNE plot
plot_tsne <- function(X, Y, ruleSet, caseColors, symb=c(1, 4),
pointSize=1, textSize=1,
bottom_buffer=1.25, all_buffer=1,
legend_under_plot=T, legend_bg_col="transparent",
legend_offset=c(0,0), legend_position="bottomright",
jitter_factor=1, jitter_amount=NULL,
max_iter=1000,
highlight=NULL, box_color=NULL){
## Include only outcome and features in ruleSet
incl <- c() # variables to include
for (rule in ruleSet) {
for (cond in rule) {
split <- strsplit(cond, "_")[[1]]
is_neg <- as.numeric(split[length(split)]=="neg")
incl <- c(incl, paste(split[1:(length(split)-is_neg)], collapse="_"))
}
}
## Run t-SNE
train <- cbind.data.frame(Y, X[, colnames(X)[which(colnames(X) %in% incl)]])
tsne <- Rtsne::Rtsne(train, dims=2, perplexity=5, verbose=F, max_iter=max_iter, check_duplicates=F)#, partial_pca=T)
coord_nojitter <- tsne$Y
Yhat <- .getYhat(X, ruleSet) # Classification based on ruleSet
## Jitter
tsne$Y <- jitter(tsne$Y, factor=jitter_factor, amount=jitter_amount)
## Standardize coordinates
rangeX <- max(tsne$Y[,1])-min(tsne$Y[,1])
rangeY <- max(tsne$Y[,2])-min(tsne$Y[,2])
tsne$Y[,1] <- tsne$Y[,1]/rangeX*100
tsne$Y[,2] <- tsne$Y[,2]/rangeY*100
## Highlight; plot before points otherwise will cover them up
boxWidth = (max(tsne$Y[,1]) - min(tsne$Y[,1]))/25
boxHeight = (max(tsne$Y[,2]) - min(tsne$Y[,2]))/25
par(mar=c(bottom_buffer,0,0,0)+all_buffer)
plot(2*max(tsne$Y), xlim=c(min(tsne$Y[,1]), max(tsne$Y[,1])), ylim=c(min(tsne$Y[,2]), max(tsne$Y[,2])),
xlab="", ylab="",
xaxt='n', yaxt='n')
for (a in ruleSet[highlight]) { # For each rule we want to plot
if (length(rule)==1) { # One condition
# Features and values
split <- strsplit(a, "_")[[1]]
is_neg <- as.numeric(split[length(split)]=="neg")
feature <- paste(split[1:(length(split)-is_neg)], collapse="_")
value <- as.numeric(!is_neg)
tsne_pos <- tsne$Y[X[,feature]==value,]
Yhat[Yhat==0] <- as.numeric(X[,feature]==value)[Yhat==0] # If satisfies rule, then classified as positive (if doesn't, not recoded)
} else { # multiple conditions
split_conds <- strsplit(a, "_")
features <- c()
values <- c()
for (j in 1:length(a) ) {
split <- split_conds[[j]]
is_neg <- as.numeric(split[length(split)]=="neg")
features[j] <- paste(split[1:(length(split)-is_neg)], collapse="_")
values[j] <- as.numeric(!is_neg)
}
values_matrix <- matrix(rep(values, nrow(X)), ncol=length(values), byrow=T)
tsne_pos <- tsne$Y[ apply(X[,features]==values_matrix, 1, all), ] # tsne coordinates of cases that satisfy the rule
Yhat[Yhat==0] <- as.numeric(apply(X[,features]==values_matrix, 1, all))[Yhat==0]
}
if (!is.null(nrow(tsne_pos))) { # Check multiple rows
for (j in 1:nrow(tsne_pos)) { # For each case that satisfies the rule
# Draw a box around it
rect(xleft=tsne_pos[j,1]-boxWidth/2, xright=tsne_pos[j,1]+boxWidth/2,
ybottom=tsne_pos[j,2]-boxHeight/2, tsne_pos[j,2]+boxHeight/2,
border=NA,
lwd=10,
col="grey")
}
} else { # Vector, only 1 "row"
rect(xleft=tsne_pos[1]-boxWidth/2, xright=tsne_pos[1]+boxWidth/2,
ybottom=tsne_pos[2]-boxHeight/2, tsne_pos[2]+boxHeight/2,
border=NA,
lwd=10,
col=boxColor)
}
}
## Plot points
par(new=T, mar=c(bottom_buffer,0,0,0)+all_buffer)
plot(tsne$Y,
col=ifelse(Y==1, yes=caseColors[1], no=caseColors[2]), # color code by outcome
pch=ifelse(Y==Yhat, yes=symb[1], no=symb[2]), # symbol code by correct classification
lwd=ifelse(Y==Yhat, yes=0, no=1),
cex = pointSize,
xlim=c(min(tsne$Y[,1]), max(tsne$Y[,1])), ylim=c(min(tsne$Y[,2]), max(tsne$Y[,2])),
xaxt='n', yaxt='n', xlab="", ylab="")
## Legend
if(legend_under_plot){
legend(x=min(tsne$Y[,1])+legend_offset[1], y=min(tsne$Y[,2])-1+legend_offset[2], ncol=2,
legend=c("True positive", "True negative", "False negative", "False positive"),
bg=legend_bg_col, col=c(caseColors, caseColors),
pch=c(symb[1], symb[1], symb[2], symb[2]), box.lty = 0, cex=textSize,
xpd=T)
} else { ## legend inside plot
legend(legend_position, inset=legend_offset,
legend=c(shade_name, "Y=1", "Y=0"),
bg=legend_bg_col, col=c(boxColor, caseColors),
pch=c(15, rep(symb, 2)), box.lty = 0, cex=textSize)
}
}
#' Get predicted outcomes
#'
#' Get the outcomes that a rule set would predict, i.e. Y=1 iff x \in ruleSet
#'
#' @param data data frame out independent variables without outcome
#' @param ruleSet rule set
#' @return predicted outcomes for each observation
.getYhat <- function(data, ruleSet){
# when p_pos=1 and p_neg=0, .getY returns yhat (.getY returns 1 iff x \in ruleSet)
return(.getY(data, ruleSet))
}
#' Generate outcome
#'
#' Generates outcomes based on a rule set and the probability that the outcome
#' is positive conditional on whether an observation satisfies the rule set
#'
#' @param X data frame with binary data for independent variables
#' @param ruleSet true rule set with which to generate data
#' @param p_pos probability of yn=1 if xn satisfies rule (default 1),
#' OR vector of probabilities (p_pos[i] corresponds to ruleSet[i])
#' @param p_neg probability of yn=1 if xn does NOT satisfy rule (default 0),
#' OR vector of probabilities (p_neg[i] corresponds to ruleSet[i])
#' @return a vector of outcomes
.getY <- function(X, ruleSet, p_pos=1, p_neg = 0){
nameSet <- list()
valueSet <- list()
n <- nrow(X)
for(i in 1:length(ruleSet)){
rule <- ruleSet[[i]]
nameSet[[i]] <- .getNames(rule)
valueSet[[i]] <- getValues(rule)
}
# Outcome
Y <- rep(0, n)
rand <- runif(n)
pp <- p_pos
pn <- p_neg
for(i in 1:length(nameSet)){
Y_i <- apply(X[nameSet[[i]]], 1, function(x) all(x==valueSet[[i]])) # Cases that satisfy ith rule
if(length(p_pos) > 1){
pp <- p_pos[i]
}
if(length(p_neg) > 1){
pn <- p_neg[i]
}
Y <- as.numeric(Y == 1 | (Y_i == 1 & rand <= pp) )# 1 if satisfies ith or any previous rule
Y <- as.numeric(Y == 1 | (Y_i == 0 & rand <= pn) )# 1 if satisfies ith or any previous rule
}
return(Y)
}
#' Get variable names for a rule
#'
#' Get the names of variables as they appear in the data corresponding to the
#' conditions in a rule
#'
#' @param rule a rule, formatted as a vector of conditions
#' @return vector of names of variables correspoding to the conditions in rule
.getNames <- function(rule){
split <- strsplit(rule, "_")
names <- c()
for(i in 1:length(split)){
names[i] <- paste(split[[i]][split[[i]] != "neg"], collapse="_")
}
return(names)
}
#' Get rule values
#'
#' Get the values of each condition in a rule
#'
#' @param rule a rule, formatted as a vector of conditions
#' @return vector of values for each condition in rule
getValues <- function(rule){
split <- strsplit(rule, "_")
values <- c()
for(i in 1:length(split)){
if(length(split[[i]]) == 3){
values[i] <- 0
} else {
values[i] <- 1
}
}
return(values)
}
plot_tsne(X = lipset_df, Y = lipset_Y, ruleSet=ruleset, pointSize = 1.25, symb = c(20, 4),
caseColors=prettyGraphs::add.alpha(RColorBrewer::brewer.pal(11, "RdGy")[c(8,10)], alpha=1))
plot_tsne(X = lipset_df, Y = lipset_Y, ruleSet=ruleset,
pointSize = 1.25, symb = c(20, 4),
caseColors=prettyGraphs::add.alpha(RColorBrewer::brewer.pal(11, "RdYlGn")[c(3,10)], alpha=1))
plot_tsne(X = lipset_df, Y = lipset_Y, ruleSet=ruleset,
pointSize = 1.25, symb = c(20, 4),
caseColors=prettyGraphs::add.alpha(RColorBrewer::brewer.pal(11, "RdYlGn")[c(2,11)], alpha=1))
plot_tsne(X = lipset_df, Y = lipset_Y, ruleSet=ruleset,
pointSize = 1.25, symb = c(20, 4),
caseColors=prettyGraphs::add.alpha(RColorBrewer::brewer.pal(11, "RdYlGn")[c(2,9)], alpha=1))
# get aggregated rule set
ruleset <- brs::agg_BRS(fit = out_lipset, X = lipset_df, Y=lipset_Y, maxLen=3)
# get aggregated rule set
ruleset <- brs::agg_BRS(fit = out_lipset, X = lipset_df, Y=lipset_Y, maxLen=3)
plot_chord(ruleSet=ruleset, featureGroups=fgs,
#linkColors=RColorBrewer::brewer.pal(11, "RdGy")[c(8,10)],
linkColors=RColorBrewer::brewer.pal(9, "Set3")[c(6,5)],
gridColors = "grey",
textSize = 1, side_mar=0, top_mar=0)
# Feature names (without values) as they appear in X and their corresponding labels
fgs <- cbind(unique(unlist(lapply(colnames(lipset_df), function(x) strsplit(x, "_")[[1]][[1]]))),
c("Wealth", "Urbanization", "Education", "Industrialization"))
fgs
devtools::build_rmd("vignettes/brs-vignette.Rmd")
vignette(brs)
vignette(package="brs")
install.packages("../brs_0.0.0.9007.tar.gz", repos = NULL, type="source")
vignette(package="brs")
vignette(package="brs")
browseVignettes()
browseVignettes()