ewenharrison / finalfit
1
#' Cox proprotional hazards mixed effects models: \code{finalfit} model wrapper
2
#'
3
#' Using \code{finalfit} conventions, produces multivariable Cox Proportional
4
#' Hazard regression mixed effects models for a set of explanatory variables and
5
#' random effects structure against a survival object.
6
#'
7
#' Uses \code{\link[coxme]{coxme}} with \code{finalfit} modelling conventions.
8
#' Output can be passed to \code{\link{fit2df}}. This function is in development
9
#' given the coxme package is new/
10
#'
11
#' @param .data Data frame.
12
#' @param dependent Character vector of length 1:  name of survival object in
13
#'   form \code{Surv(time, status)}.
14
#' @param explanatory Character vector of any length: name(s) of explanatory
15
#'   variables.
16
#' @param random_effect Character vector of length 1, either, (1) name of random
17
#'   intercept variable, e.g. "var1", (automatically convered to "(1 | var1)");
18
#'   or, (2) the full \code{lme4} specification, e.g. "(var1 | var2)". Note
19
#'   parenthesis MUST be included in (2) but NOT included in (1).
20
#' @return A multivariable \code{\link[coxme]{coxme}} fitted model. Output is of
21
#'   class \code{coxme}.
22
#'
23
#' @seealso \code{\link{fit2df}, \link{finalfit_merge}}
24
#' @family finalfit model wrappers
25
#' @export
26
#'
27
#' @examples
28
#' # Cox Proportional Hazards mixed effects analysis.
29
#' library(finalfit)
30
#' library(dplyr)
31
#'
32
#' explanatory = c("age.factor", "sex.factor", "obstruct.factor", "perfor.factor")
33
#' dependent = "Surv(time, status)"
34
#' random_effect = "hospital"
35
#' colon_s %>%
36
#' 	coxphmixed(dependent, explanatory, random_effect)
37
#' 	
38
coxphmixed <- function(.data, dependent, explanatory, random_effect){
39 0
	requireNamespace("survival")
40
	
41
	# If single term random effect, default to random intercept model
42 0
	if(!grepl("\\|", random_effect)) random_effect = paste0("(1 | ", random_effect, ")")
43
	
44 0
	coxme::coxme(as.formula(ff_formula(dependent, explanatory, random_effect)), data=.data)
45
}

Read our documentation on viewing source code .

Loading