-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexpected_over_observed_ratio.R
65 lines (59 loc) · 2.02 KB
/
expected_over_observed_ratio.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
library(survival)
library(plyr)
km.survival <- function(data,t) {
sf <- survfit(Surv(studytime, ascvd) ~ 1, data=data)
survival <- 1 - min(sf$surv[sf$time<=t])
return(survival)
}
survival.per.group <- function(data, baseline=NULL, t=10) {
if (is.null(baseline)) {
risk <- data$risk
} else {
risk <- baseline$risk
}
data$risk_bin <- cut(risk, breaks=c(0, 0.05, 0.075, 0.1, 1),
labels=c("< 5%", "5% to < 7.5%", "7.5% to < 10%", "\u2265 10%"))
survival.table <- ddply(
data, ~risk_bin,
function(group.data) {
cbind(observed=km.survival(group.data, t),
expected=mean(group.data$risk),
n_participants=nrow(group.data))
}
)
return(survival.table)
}
print.survival_table <- function(table) {
cat("\t")
write.table(table, sep="\t", quote=F, eol="\n", row.names=T, col.names=T)
cat("\n")
}
expected_over_observed_table <- function(data, baseline=NULL, t=10, model.name=NULL) {
survival.table <- survival.per.group(data, baseline, t)
survival.table$expected_over_observed_ratio <- survival.table$expected / survival.table$observed
eoo.table <- t(data.frame(apply(survival.table, 1, function(row) {
paste(specify.decimal(as.numeric(row['expected_over_observed_ratio']), 2),
" (n = ",
as.integer(row['n_participants']),
")",
sep="")
})))
colnames(eoo.table) <- survival.table$risk_bin
if (!is.null(model.name)) {
rownames(eoo.table) <- c(model.name)
} else {
rownames(eoo.table) <- c()
}
class(eoo.table) <- "expected_over_observed_table"
return(eoo.table)
}
concat.expected_over_observed_table <- function(...) {
eoo.table <- rbind(...)
class(eoo.table) <- "expected_over_observed_table"
return(eoo.table)
}
print.expected_over_observed_table <- function(table) {
cat("\t")
write.table(table, sep="\t", quote=F, eol="\n", row.names=T, col.names=T)
cat("\n")
}