-
Notifications
You must be signed in to change notification settings - Fork 1
/
make-datasets.R
130 lines (98 loc) · 4.94 KB
/
make-datasets.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
# ---- init ----
rm(list=ls())
source("config.R")
source("methods.R")
source("utils.R")
library(dplyr)
library(sampling)
# ---- db-read ----
db = read.csv(DATABASE.LOCATION, header=TRUE)
db = db %>%
select(GivenId, MalignancyCharacter, one_of(COLS.ALL)) %>%
rename(PatientId=GivenId)
# ---- db-preprocess ----
db[with(db, which(MalignancyCharacter == 2)), "MalignancyCharacter"] = 1
db[with(db, which(is.na(PapBloodFlow) & Pap == 0)), "PapBloodFlow"] = 0
db[with(db, which(is.na(APapDimension) & Pap == 0)), "APapDimension"] = 0
db[with(db, which(is.na(SeptumThickness) & Septum == 0)), "SeptumThickness"] = 0
db[with(db, which(grepl("^Sms", PatientId) & is.na(Ri))), "Ri"] = 1
db[with(db, which(grepl("^Sz", PatientId) & is.na(Ri) & Color == 1)), "Ri"] = 1
# ---- db-divide ----
db.completeCases = filter(db, complete.cases(db))
db.incompleteCases = filter(db, !complete.cases(db) &
complete.cases(db[, c("MalignancyCharacter", COLS.SURE)]))
inTrainingSet.benignSize = round(nrow(filter(db.completeCases, MalignancyCharacter==0)) *
TRAINING.SIZE/nrow(db.completeCases))
inTrainingSet.malignantSize = round(nrow(filter(db.completeCases, MalignancyCharacter==1)) *
TRAINING.SIZE/nrow(db.completeCases))
db.completeCases = arrange(db.completeCases, MalignancyCharacter) # sort before strata
inTrainingSet = strata(db.completeCases, "MalignancyCharacter",
c(inTrainingSet.benignSize, inTrainingSet.malignantSize),
"srswor")$ID_unit
inTestSet = apply(select(db.incompleteCases, one_of(COLS.OBSC)), 1,
function(row) {sum(is.na(row))/length(row) <= OBSCURE.MAX})
db.training = db.completeCases[inTrainingSet, ]
db.test = bind_rows(db.completeCases[-inTrainingSet, ], # add a few remaining compl. cases
db.incompleteCases[inTestSet, ])
# ---- build-training-simulations-data ----
training.data = data.frame()
for (obsc.lvl in OBSCURE.PERCENTAGES)
{
for (obsc.rep in 1:OBSUCRE.REPEAT)
{
printDebug(paste(obsc.lvl, obsc.rep))
# sample the database to obtain balanced dataset
db.training.sample = bind_rows(sample_n(filter(db.training, MalignancyCharacter==0),
PROBE.SIZE.NEGATIVE),
sample_n(filter(db.training, MalignancyCharacter==1),
PROBE.SIZE.POSITIVE))
if (obsc.lvl > 0)
{
naMat = matrix(0, nrow=nrow(db.training.sample), ncol=length(COLS.OBSC))
# put NA values into the matrix to obtain required percentage
# of missing values
naMat[sample(1:length(naMat), floor(obsc.lvl*length(naMat)))] = NA
# set NA value in random places in patient database
# (NA added to any value results in NA)
db.training.sample[, COLS.OBSC] = db.training.sample[, COLS.OBSC] + naMat
}
results = do.call(cbind,
sapply(seq_along(METHODS),
function(m)
{
method = METHODS[[m]]
name = METHODS.NAME[[m]]
cols = METHODS.COL[[m]]
t(apply(db.training.sample[cols], 1, method))
},
simplify=FALSE))
training.data = bind_rows(training.data,
with(db.training.sample,
data.frame(PatientId,
ObscureLevel=obsc.lvl,
ObscureRepeat=obsc.rep,
MalignancyCharacter,
results)))
}
}
# ---- build-test-data ----
results = do.call(cbind,
sapply(seq_along(METHODS),
function(m)
{
method = METHODS[[m]]
name = METHODS.NAME[[m]]
cols = METHODS.COL[[m]]
t(apply(db.test[cols], 1, method))
},
simplify=FALSE))
obscLevels = apply(select(db.test, one_of(COLS.OBSC)), 1,
function(row){round(sum(is.na(row))/length(row), 2)})
test.data = with(db.test, data.frame(PatientId,
ObscureLevel=obscLevels,
ObscureRepeat=1,
MalignancyCharacter,
results))
# ---- save-results ----
write.csv(training.data, TRAINING.LOCATION, row.names=FALSE)
write.csv(test.data, TEST.LOCATION, row.names=FALSE)