-
Notifications
You must be signed in to change notification settings - Fork 0
/
coxphf-tidiers.R
103 lines (81 loc) · 2.34 KB
/
coxphf-tidiers.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
#' Tidy a coxphf object
#'
#' @param x A `coxphf` object.
#' @param conf.int Logical indicating whether or not to include
#' a confidence interval in the tidied output. Defaults to FALSE.
#' @param conf.level The confidence level to use for the confidence
#' interval if conf.int = TRUE. Must be strictly greater than 0
#' and less than 1. Defaults to 0.95, which corresponds to a
#' 95 percent confidence interval.
#' @param exponentiate Logical indicating whether or not to display coefficient
#' estimates on an exponential scale.
#' @param ... Unused, included for generic consistency only.
#' @return A tidy [tibble::tibble()] summarizing component-level
#' information about the model
#'
#' import tibble
#' @export
tidy.coxphf <- function(x, conf.int = FALSE, conf.level = 0.95, exponentiate = FALSE, ...){
result <- with(
x,
tibble::tibble(
term = names(coefficients),
estimate = coefficients,
std.error = sqrt(diag(var)),
statistic = qchisq(1 - prob, 1),
p.value = prob
)
)
if(exponentiate){
result$estimate <- exp(result$estimate)
}
if(conf.int){
ci <- stats::confint(x, level = conf.level)
colnames(ci) <- c("conf.low", "conf.high")
if(exponentiate) ci <- exp(ci)
result <- cbind(
result,
ci
) |> tibble::tibble()
}
return(result)
}
#' Glance at a coxphf object
#'
#' @param x A `coxphf` object.
#' @param ... Unused, included for generic consistency only.
#' @return A tidy [tibble::tibble()] summarizing component-level
#' information about the model
#'
#' @export
glance.coxphf <- function(x, ...){
result <- with(
x,
tibble::tibble(
n = n,
nevent = sum(y[, "status"], na.rm = TRUE),
logLik = as.numeric(stats::logLik(x)),
AIC = stats::AIC(x)
)
)
return(result)
}
#' Augment a coxphf object
#'
#' @param x A `coxphf` object.
#' @param data the dataset used to fit the model.
#' @param ... Unused, included for generic consistency only.
#' @return A tidy [tibble::tibble()] summarizing component-level
#' information about the model
#'
#' @export
augment.coxphf <- function(x, data = x$y, ...){
result <- cbind(
data,
".linear.predictor" = x$linear.predictor
) |> tibble::as_tibble()
return(result)
}
#' @importFrom generics glance
#' @export
generics::glance