## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>",
                      fig.width = 7, fig.height = 4.5)

## -----------------------------------------------------------------------------
library(fuzzySurvival)
library(survival)

## -----------------------------------------------------------------------------
# the failure is entirely plausible anywhere in the second quarter
q2 <- fuzzy_number_interval(4, 6)
q2
alpha_cuts(q2, c(0, 0.5, 1))

## -----------------------------------------------------------------------------
# a rounded recollection: certainly 3 to 9, concentrated in the middle
recalled <- fuzzy_number_trapezoid(3, 5, 7, 9)
alpha_cuts(recalled, c(0, 0.25, 0.5, 0.75, 1))

## -----------------------------------------------------------------------------
plot(c(recalled, q2))

## -----------------------------------------------------------------------------
round(membership(recalled, c(2, 3, 4, 5, 6, 7, 8, 9, 10)), 3)

## -----------------------------------------------------------------------------
tm <- fuzzy_number_crisp(c(2, 6, 9))
tm[2] <- fuzzy_number_interval(4, 6)   # "failed in the second quarter"
fuzzy_surv(tm, c(1, 1, 1))

## -----------------------------------------------------------------------------
fuzzy_surv(c(2, 5, 7, 9), c(1, 0.5, 1, 1))   # "lost to follow-up, maybe failed"

## -----------------------------------------------------------------------------
data(fuzzy_dfs)
fit <- fkm(fuzzy_surv(ft, fs) ~ 1, data = fuzzy_dfs)
fit
summary(fit, times = c(6, 12, 24))[, c("time", "n.risk", "lower", "surv", "upper")]

## -----------------------------------------------------------------------------
fitg <- fkm(fuzzy_surv(ft, fs) ~ arm, data = fuzzy_dfs)
fitg
summary(fitg, times = 12)[[1]][, c("time", "n.risk", "lower", "surv", "upper")]

## -----------------------------------------------------------------------------
plot(fitg, type = "diff")

## -----------------------------------------------------------------------------
flogrank(fuzzy_surv(ft, fs) ~ arm, data = fuzzy_dfs)

## -----------------------------------------------------------------------------
fct <- fcox(fuzzy_surv(ft, fs) ~ age + arm, data = fuzzy_dfs)
fct

## -----------------------------------------------------------------------------
alpha_cuts(coef(fct, exponentiate = TRUE), c(0, 0.5, 1))

## -----------------------------------------------------------------------------
plot(fct)
plot(fct, type = "membership")

## -----------------------------------------------------------------------------
defuzz(fct$p.value, "centroid")

## -----------------------------------------------------------------------------
defuzz(recalled, "centroid")
defuzz(recalled, c("min", "centroid", "max"))

## -----------------------------------------------------------------------------
defuzz(fuzzy_number_triangle(2, 5, 8),
       c("midrange", "centroid", "gmirl", "sgmrl", "yager"))

## -----------------------------------------------------------------------------
tn <- as_fuzzy_number(fuzzy_dfs$ft)
lo <- tn$support[, 1]; hi <- tn$support[, 2]; w <- hi - lo
graded <- fuzzy_number_trapezoid(lo, lo + 0.4 * w, lo + 0.6 * w, hi)
fitg2 <- fkm(fuzzy_surv(I(list(graded)), fuzzy_dfs$fs) ~ 1)
wid <- fitg2$upper[, match(24, fitg2$time)] - fitg2$lower[, match(24, fitg2$time)]
round(wid[c(1, 6, 11, 16, 21)], 5)

## -----------------------------------------------------------------------------
truefit <- survfit(Surv(true_time, true_status) ~ 1, data = fuzzy_dfs)
tt2 <- summary(truefit)$time
truth <- vapply(tt2, function(t) summary(truefit, times = t)$surv, numeric(1))
gap <- abs(defuzz(fit, "centroid", times = fit$time) -
             fuzzySurvival:::km_eval(tt2, truth, fit$time))
data.frame(fuzziness = max(fit$upper[1, ] - fit$lower[1, ]),
           coarsening_bias = max(gap))

## -----------------------------------------------------------------------------
set.seed(7)
d <- sim_fuzzy_surv(n = 300, beta = c(age = 0.35, arm = -0.55),
                    prop_vague = 0.7, prop_uncertain = 0.2)
fit_sim <- fkm(fuzzy_surv(ft, fs) ~ 1, data = d)
plot(fit_sim)

## -----------------------------------------------------------------------------
cut_sim <- as_fuzzy_number(d$ft)$acut(0)
mid <- signif((cut_sim$lower[, 1] + cut_sim$upper[, 1]) / 2, 12)
obs_km <- fuzzySurvival:::km_fractional(mid, d$fs)
rv <- fuzzySurvival:::km_eval(obs_km$time, obs_km$surv, fit_sim$time)
all(rv >= fit_sim$lower[1, ] - 1e-8) && all(rv <= fit_sim$upper[1, ] + 1e-8)

## -----------------------------------------------------------------------------
s <- fkm_fuzzy_surv(fit, times = c(6, 12, 24, 36))
alpha_cuts(s, c(0, 0.5, 1))
defuzz(s, "centroid")

## -----------------------------------------------------------------------------
plot(fit)

## -----------------------------------------------------------------------------
L <- fit$lower; U <- fit$upper
c(nested_lower = all(vapply(seq_len(ncol(L)),
                           function(k) all(diff(L[, k]) >= -1e-9), logical(1))),
  nested_upper = all(vapply(seq_len(ncol(U)),
                           function(k) all(diff(U[, k]) <= 1e-9), logical(1))))

## -----------------------------------------------------------------------------
set.seed(42)
n <- 80
tt <- rexp(n, 0.05) * 10
st <- rbinom(n, 1, 0.7)
tt[!st] <- tt[!st] + runif(sum(!st), 0, 5)

fitc <- fkm(fuzzy_surv(tt, st) ~ 1)
ref <- vapply(fitc$time, function(t) summary(survfit(Surv(tt, st) ~ 1),
                                            times = t)$surv, numeric(1))
ref[is.na(ref)] <- 1
max(abs(defuzz(fitc, "midrange", level = 1, times = fitc$time) - ref))
all(fitc$lower == fitc$upper)

