# NOT RUN {
dat <- survival::rats
m1 <- emfrail(formula = Surv(time, status) ~ rx + sex + cluster(litter),
data = dat)
m1
summary(m1)
# }
# NOT RUN {
# for the Inverse Gaussian distribution
m2 <- emfrail(formula = Surv(time, status) ~ rx + sex + cluster(litter),
data = dat,
distribution = emfrail_dist(dist = "pvf"))
m2
# for the PVF distribution with m = 0.75
m3 <- emfrail(formula = Surv(time, status) ~ rx + sex + cluster(litter),
data = dat,
distribution = emfrail_dist(dist = "pvf", pvfm = 0.75))
m3
# for the positive stable distribution
m4 <- emfrail(formula = Surv(time, status) ~ rx + sex + cluster(litter),
data = dat,
distribution = emfrail_dist(dist = "stable"))
m4
# }
# NOT RUN {
# Compare marginal log-likelihoods
# }
# NOT RUN {
models <- list(m1, m2, m3, m4)
logliks <- lapply(models,
function(x) -x$outer_m$value)
names(logliks) <- lapply(models,
function(x) with(x$distribution,
ifelse(dist == "pvf",
paste(dist, "/", pvfm),
dist))
)
logliks
# }
# NOT RUN {
# Draw the profile log-likelihood
# }
# NOT RUN {
fr_var <- seq(from = 0.01, to = 1.4, length.out = 20)
# For gamma the variance is 1/theta (see parametrizations)
pll_gamma <- emfrail_pll(formula = Surv(time, status) ~ rx + sex + cluster(litter),
data = dat,
values = 1/fr_var )
plot(fr_var, pll_gamma,
type = "l",
xlab = "Frailty variance",
ylab = "Profile log-likelihood")
# }
# NOT RUN {
# The same can be done with coxph, where variance is refered to as "theta"
pll_cph <- sapply(fr_var, function(fr)
coxph(data = dat, formula = Surv(time, status) ~ rx + sex + frailty(litter, theta = fr),
method = "breslow")$history[[1]][[3]])
# Same for inverse gaussian
pll_if <- emfrail_pll(data = dat,
formula = Surv(time, status) ~ rx + sex + cluster(litter),
distribution = emfrail_dist(dist = "pvf"),
.values = 1/fr_var )
# Same for pvf with a psoitive pvfm parameter
pll_pvf <- emfrail_pll(data = dat,
formula = Surv(time, status) ~ rx + sex + cluster(litter),
distribution = emfrail_dist(dist = "pvf", pvfm = 1.5),
.values = 1/fr_var )
miny <- min(c(pll_gamma, pll_cph, pll_if, pll_pvf))
maxy <- max(c(pll_gamma, pll_cph, pll_if, pll_pvf))
plot(fr_var, pll_gamma,
type = "l",
xlab = "Frailty variance",
ylab = "Profile log-likelihood",
ylim = c(miny, maxy))
points(fr_var, pll_cph, col = 2)
lines(fr_var, pll_if, col = 3)
lines(fr_var, pll_pvf, col = 4)
legend(legend = c("gamma (emfrail)", "gamma (coxph)", "inverse gaussian", "pvf, m=1.5"),
col = 1:4,
lty = 1,
x = 0,
y = (maxy + miny)/2)
# }
# NOT RUN {
# Recurrent events
mod_rec <- emfrail(Surv(start, stop, status) ~ treatment + cluster(id), bladder1)
# The warnings appear from the Surv object, they also appear in coxph.
summary(mod_rec)
# Create a histogram of the estimated frailties
plot(mod_rec, type = "hist")
# or, with ggplot:
# }
# NOT RUN {
library(ggplot2)
sum_mod_rec <- summary(mod_rec)
ggplot(sum_mod_rec$frail, aes(x = z)) +
geom_histogram() +
ggtitle("Estimated frailties")
# Plot the frailty estimates with quantiles of the estimated distribution
ggplot(sum_mod_rec$frail, aes(x = id, y = z)) +
geom_point() +
ggtitle("Estimated frailties") +
geom_errorbar(aes(ymin = lower_q, ymax = upper_q))
# We can do the same plot but with the ordered frailties:
ord <- order(sum_mod_rec$frail$z)
# we need new x coordinates for that:
ordering <- 1:length(ord)
ggplot(sum_mod_rec$frail[ord,], aes(x = ordering, y = z)) +
geom_point() +
ggtitle("Estimated frailties") +
geom_errorbar(aes(ymin = lower_q, ymax = upper_q))
# How do we know which id is which one now?
# We can make an interactive plot with ggplotly
# To add text to elements we add id in aes()
library(plotly)
ggplotly(
ggplot(sum_mod_rec$frail[ord,], aes(x = ordering, y = z)) +
geom_point(aes(id = id)) +
ggtitle("Estimated frailties") +
geom_errorbar(aes(ymin = lower_q, ymax = upper_q, id = id))
)
# }
# NOT RUN {
# Plot marginal and conditional curves
# For recurrent events, the survival is not very meaningful
plot(mod_rec, type = "pred", lp = 0, quantity = "cumhaz")
#The strong frailty "drags down" the intensity
# Left truncation
# N.B. This code takes a longer time to run
# }
# NOT RUN {
# We simulate some data with truncation times
set.seed(1)
x <- sample(c(0,1), 5 * 300, TRUE)
u <- rep(rgamma(300,1,1), each = 5)
stime <- rexp(5*300, rate = u * exp(0.5 * x))
status <- ifelse(stime > 5, 0, 1)
stime[status] <- 5
ltime <- runif(5 * 300, min = 0, max = 2)
d <- data.frame(id = rep(1:300, each = 5),
x = x,
stime = stime,
u = u,
ltime = ltime,
status = 1)
d_left <- d[d$stime > d$ltime,]
# The worst thing that can be done is to
# Ignore the left truncation:
mod_1 <- emfrail(Surv(stime, status)~ x + cluster(id), d_left)
# The so-and-so thing is to consider the delayed entry time,
# But do not "update" the frailty distribution accordingly
mod_2 <- emfrail(Surv(ltime, stime, status)~ x + cluster(id), d_left)
# This is identical with
mod_cox <- coxph(Surv(ltime, stime, status)~ x + frailty(id), data = d_left)
# The correct thing is to update the frailty.
mod_3 <- emfrail(Surv(ltime, stime, status)~ x + cluster(id), d_left,
distribution = emfrail_dist(left_truncation = TRUE))
summary(mod_1)
summary(mod_2)
summary(mod_3)
# }
Run the code above in your browser using DataLab