-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathcoxphuni.R
47 lines (44 loc) · 1.68 KB
/
coxphuni.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
#' Cox proprotional hazards univariable models: \code{finalfit} model wrapper
#'
#' Using \code{finalfit} conventions, produces multiple univariable Cox Proportional Hazard
#' regression models for a set of explanatory variables against a survival object.
#'
#' Uses \code{\link[survival]{coxph}} with \code{finalfit} modelling conventions. Output can be
#' passed to \code{\link{fit2df}}.
#'
#' @param .data Data frame.
#' @param dependent Character vector of length 1: name of survival object in form \code{Surv(time, status)}.
#' @param explanatory Character vector of any length: name(s) of explanatory variables.
#' @return A list of univariable \code{\link[survival]{coxph}} fitted model outputs.
#' Output is of class \code{coxphlist}.
#'
#' @seealso \code{\link{fit2df}, \link{finalfit_merge}}
#' @family finalfit model wrappers
#' @export
#' @import survival
#'
#' @examples
#' # Cox Proportional Hazards univariable analysis.
#' library(finalfit)
#' library(dplyr)
#'
#' explanatory = c("age.factor", "sex.factor", "obstruct.factor", "perfor.factor")
#' dependent = "Surv(time, status)"
#' colon_s %>%
#' coxphuni(dependent, explanatory) %>%
#' fit2df()
coxphuni <- function(.data, dependent, explanatory){
requireNamespace("survival")
# Remove cluster and strata terms
drop = grepl("cluster[(].*[)]", explanatory) |
grepl("strata[(].*[)]", explanatory) |
grepl("frailty[(].*[)]", explanatory)
explanatory = explanatory[!drop]
result <- list()
for (i in 1:length(explanatory)){
result[[i]] <- coxph(as.formula(paste0(dependent, "~", explanatory[i])), data=.data)
result[[i]]$call$formula <- formula(result[[i]])
}
class(result) = "coxphlist"
return(result)
}