ewenharrison / finalfit
1
#' Mixed effects binomial logistic regression models: \code{finalfit} model wrapper
2
#'
3
#' Using \code{finalfit} conventions, produces mixed effects binomial logistic
4
#'   regression models for a set of explanatory variables against a binary dependent.
5
#'
6
#' Uses \code{lme4::\link[lme4]{glmer}} with \code{finalfit} modelling conventions. Output can be
7
#'   passed to \code{\link{fit2df}}. This is only currently set-up to take a single random effect
8
#'   as a random intercept. Can be updated in future to allow multiple random intercepts,
9
#'   random gradients and interactions on random effects if there is a need
10
#'
11
#' @param .data Dataframe.
12
#' @param dependent Character vector of length 1, name of depdendent variable (must have 2 levels).
13
#' @param explanatory Character vector of any length: name(s) of explanatory variables.
14
#' @param random_effect Character vector of length 1, either, (1) name of random
15
#'   intercept variable, e.g. "var1", (automatically convered to "(1 | var1)");
16
#'   or, (2) the full \code{lme4} specification, e.g. "(var1 | var2)". Note
17
#'   parenthesis MUST be included in (2) but NOT included in (1).
18
#' @param ... Other arguments to pass to \code{lme4::\link[lme4]{glmer}}.
19
#' @return A list of multivariable \code{lme4::\link[lme4]{glmer}} fitted model outputs.
20
#'   Output is of class \code{glmerMod}.
21
#'
22
#' @seealso \code{\link{fit2df}, \link{finalfit_merge}}
23
#' @family finalfit model wrappers
24
#' @export
25
#'
26
#' @examples
27
#' library(finalfit)
28
#' library(dplyr)
29
#'
30
#' explanatory = c("age.factor", "sex.factor", "obstruct.factor", "perfor.factor")
31
#' random_effect = "hospital"
32
#' dependent = "mort_5yr"
33
#'
34
#' colon_s %>%
35
#'   glmmixed(dependent, explanatory, random_effect) %>%
36
#' 	 fit2df(estimate_suffix=" (multilevel)")
37

38
glmmixed <- function(.data, dependent, explanatory, random_effect, ...){
39
	# If single term random effect, default to random intercept model
40 1
	if(!grepl("\\|", random_effect)) random_effect = paste0("(1 | ", random_effect, ")")
41 1
  lme4::glmer(paste0(dependent, "~", paste(explanatory, collapse="+"), " + ", random_effect),
42 1
              data=.data, family="binomial", control=lme4::glmerControl(optimizer="bobyqa",
43 1
                                                                        optCtrl=list(maxfun=200000)), ...)
44
}

Read our documentation on viewing source code .

Loading