-
Notifications
You must be signed in to change notification settings - Fork 2
/
Algorithms.R
92 lines (72 loc) · 2.72 KB
/
Algorithms.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
source("Data.R") # For validation functions
source("AlgorithmUtil.R") # For algorithm lookup/registation functions
# Source all algorithms here!
source("Gail89.R")
source("Gail08.R")
source("BCRAT.R")
source("CAREGail.R")
source("Rosner96.R")
# print(bc_risk_algorithms)
# list installed algorithms
list_algorithms = function() {
names(bc_risk_algorithms)
}
bc_risk_algorithm = function(algorithm, population, years=5, filter_output="*", aux_params = list()) {
if(is.character(algorithm) && algorithm %in% list_algorithms()) {
# get the algorithm
algorithm_f = get_algorithm(algorithm)$func # bc_risk_algorithms[[algorithm]]$func
# ensure algorithm has required fields
check_required_fields(algorithm, population)
} else {
stop(paste("bc risk algorithm:", algorithm, "not found"))
}
out = do.call( algorithm_f
, append(list(population=population, years=years), aux_params)
)
# return only absolute risk and/or relative risk
if(filter_output == "AR") {
filter_output = "^AR [1-9][0-9]?$"
} else if(filter_output == "RR") {
filter_output = "^RR$"
} else if(filter_output == "AR+RR" || filter_output == "RR+AR") {
filter_output = "^(AR [1-9][0-9]?)|(RR)$"
}
# filter columns by regex
out=out[,grep(filter_output, colnames(out)),drop=FALSE]
if(ncol(out) == 0) {
stop(paste("algorithm:", algorithm, "does not return anything matching", filter_output))
}
out
}
# Calculate individual absolute breast cancer risk on a population
bc_absolute_risk = function(algorithm, population, years=5, aux_params=list()) {
if(!get_algorithm(algorithm)$ar) {
stop("Algorithm ", algorithm, "does not output absolute risk")
}
bc_risk_algorithm(algorithm, population, years, filter_output="AR", aux_params)
}
# Calculate expected number of breast cancer cases to occur for a population
bc_expected_incidence = function( algorithm, population
, years=5, aux_params=list()) {
colSums(bc_absolute_risk(algorithm, population, years, aux_params))
}
# Calculate per-100,000 cancer hazard rate
bc_hazard_rate = function( algorithm, population, years
, rate_size=10^5, aux_params=list()) {
bc_expected_incidence(algorithm, population, years, aux_params) / nrow(population) * rate_size
}
list_algorithms()
# Required field table
req_fields = function( ) {
all_fields = c()
for(alg in bc_risk_algorithms) {
all_fields = unique(append(all_fields, alg$req_fields))
}
tbl = do.call(rbind, Map(function(alg) all_fields %in% alg$req_fields
, bc_risk_algorithms))
colnames(tbl) <- all_fields
tbl = t(tbl)
write.csv(tbl, file="required_fields.csv")
print(tbl)
}
req_fields()