An "all-in-one" function that takes a single dependent variable with a vector of explanatory variable names (continuous or categorical variables) to produce a final table for publication including summary statistics. The appropriate model is selected on the basis of dependent variable and whether a random effect is specified.

finalfit.lm method (not called directly)

finalfit.glm method (not called directly)

finalfit.coxph method (not called directly)

finalfit(.data, dependent, explanatory, explanatory_multi = NULL,
  random_effect = NULL, keep_models = FALSE, metrics = FALSE,
  add_dependent_label = TRUE, dependent_label_prefix = "Dependent: ",
  dependent_label_suffix = "", ...)

finalfit.lm(.data, dependent, explanatory, explanatory_multi = NULL,
  random_effect = NULL, keep_models = FALSE, metrics = FALSE,
  add_dependent_label = TRUE, dependent_label_prefix = "Dependent: ",
  dependent_label_suffix = "", ...)

finalfit.glm(.data, dependent, explanatory, explanatory_multi = NULL,
  random_effect = NULL, keep_models = FALSE, metrics = FALSE,
  add_dependent_label = TRUE, dependent_label_prefix = "Dependent: ",
  dependent_label_suffix = "", ...)

finalfit.coxph(.data, dependent, explanatory, explanatory_multi = NULL,
  random_effect = NULL, keep_models = FALSE, metrics = FALSE,
  add_dependent_label = TRUE, dependent_label_prefix = "Dependent: ",
  dependent_label_suffix = "", ...)

Arguments

.data

Dataframe.

dependent

Character vector of length 1: quoted name of depdendent variable. Can be continuous, a binary factor, or a survival object of form Surv(time, status)

explanatory

Character vector of any length: quoted name(s) of explanatory variables.

explanatory_multi

Character vector of any length: quoted name(s) of a subset of explanatory variables to generate reduced multivariable model (must only contain variables contained in explanatory).

random_effect

Character vector of length 1: quoted name of random effects variable. When included mixed effects model generated (lme4::glmer lme4::lmer).

keep_models

Logical: include full multivariable model in output when working with reduced multivariable model (explanatory_multi) and/or mixed effect models (random_effect).

metrics

Logical: include useful model metrics in output in publication format.

add_dependent_label

Add the name of the dependent label to the top left of table.

dependent_label_prefix

Add text before dependent label.

dependent_label_suffix

Add text after dependent label.

...

Other arguments to pass to fit2df: estimate_name, digits, confint_type, confint_level, confint_sep.

Value

Returns a dataframe with the final model table.

Examples

library(finalfit) library(dplyr) # Summary, univariable and multivariable analyses of the form: # glm(depdendent ~ explanatory, family="binomial") # lmuni(), lmmulti(), lmmixed(), glmuni(), glmmulti(), glmmixed(), glmmultiboot(), # coxphuni(), coxphmulti() data(colon_s) # Modified from survival::colon explanatory = c("age.factor", "sex.factor", "obstruct.factor", "perfor.factor") dependent = 'mort_5yr' colon_s %>% finalfit(dependent, explanatory)
#> Waiting for profiling to be done...
#> Waiting for profiling to be done...
#> Waiting for profiling to be done...
#> Waiting for profiling to be done...
#> Waiting for profiling to be done...
#> Dependent: Mortality 5 year Alive Died #> 2 Age <40 years 31 (6.1) 36 (8.9) #> 1 40-59 years 208 (40.7) 131 (32.4) #> 3 60+ years 272 (53.2) 237 (58.7) #> 8 Sex Female 243 (47.6) 194 (48.0) #> 9 Male 268 (52.4) 210 (52.0) #> 4 Obstruction No 408 (82.1) 312 (78.6) #> 5 Yes 89 (17.9) 85 (21.4) #> 6 Perforation No 497 (97.3) 391 (96.8) #> 7 Yes 14 (2.7) 13 (3.2) #> OR (univariable) OR (multivariable) #> 2 - - #> 1 0.54 (0.32-0.92, p=0.023) 0.57 (0.34-0.98, p=0.041) #> 3 0.75 (0.45-1.25, p=0.270) 0.81 (0.48-1.36, p=0.426) #> 8 - - #> 9 0.98 (0.76-1.27, p=0.889) 0.98 (0.75-1.28, p=0.902) #> 4 - - #> 5 1.25 (0.90-1.74, p=0.189) 1.25 (0.90-1.76, p=0.186) #> 6 - - #> 7 1.18 (0.54-2.55, p=0.672) 1.12 (0.51-2.44, p=0.770)
# Multivariable analysis with subset of explanatory # variable set used in univariable analysis explanatory = c("age.factor", "sex.factor", "obstruct.factor", "perfor.factor") explanatory_multi = c("age.factor", "obstruct.factor") dependent = "mort_5yr" colon_s %>% finalfit(dependent, explanatory, explanatory_multi)
#> Waiting for profiling to be done...
#> Waiting for profiling to be done...
#> Waiting for profiling to be done...
#> Waiting for profiling to be done...
#> Waiting for profiling to be done...
#> Dependent: Mortality 5 year Alive Died #> 2 Age <40 years 31 (6.1) 36 (8.9) #> 1 40-59 years 208 (40.7) 131 (32.4) #> 3 60+ years 272 (53.2) 237 (58.7) #> 8 Sex Female 243 (47.6) 194 (48.0) #> 9 Male 268 (52.4) 210 (52.0) #> 4 Obstruction No 408 (82.1) 312 (78.6) #> 5 Yes 89 (17.9) 85 (21.4) #> 6 Perforation No 497 (97.3) 391 (96.8) #> 7 Yes 14 (2.7) 13 (3.2) #> OR (univariable) OR (multivariable) #> 2 - - #> 1 0.54 (0.32-0.92, p=0.023) 0.57 (0.34-0.98, p=0.041) #> 3 0.75 (0.45-1.25, p=0.270) 0.81 (0.48-1.36, p=0.424) #> 8 - - #> 9 0.98 (0.76-1.27, p=0.889) - #> 4 - - #> 5 1.25 (0.90-1.74, p=0.189) 1.26 (0.90-1.76, p=0.176) #> 6 - - #> 7 1.18 (0.54-2.55, p=0.672) -
# Summary, univariable and multivariable analyses of the form: # lme4::glmer(dependent ~ explanatory + (1 | random_effect), family="binomial") explanatory = c("age.factor", "sex.factor", "obstruct.factor", "perfor.factor") explanatory_multi = c("age.factor", "obstruct.factor") random_effect = "hospital" dependent = "mort_5yr" colon_s %>% finalfit(dependent, explanatory, explanatory_multi, random_effect)
#> Waiting for profiling to be done...
#> Waiting for profiling to be done...
#> Waiting for profiling to be done...
#> Waiting for profiling to be done...
#> Dependent: Mortality 5 year Alive Died #> 2 Age <40 years 31 (6.1) 36 (8.9) #> 1 40-59 years 208 (40.7) 131 (32.4) #> 3 60+ years 272 (53.2) 237 (58.7) #> 8 Sex Female 243 (47.6) 194 (48.0) #> 9 Male 268 (52.4) 210 (52.0) #> 4 Obstruction No 408 (82.1) 312 (78.6) #> 5 Yes 89 (17.9) 85 (21.4) #> 6 Perforation No 497 (97.3) 391 (96.8) #> 7 Yes 14 (2.7) 13 (3.2) #> OR (univariable) OR (multilevel) #> 2 - - #> 1 0.54 (0.32-0.92, p=0.023) 0.73 (0.38-1.40, p=0.342) #> 3 0.75 (0.45-1.25, p=0.270) 1.01 (0.53-1.90, p=0.984) #> 8 - - #> 9 0.98 (0.76-1.27, p=0.889) - #> 4 - - #> 5 1.25 (0.90-1.74, p=0.189) 1.24 (0.83-1.85, p=0.292) #> 6 - - #> 7 1.18 (0.54-2.55, p=0.672) -
# Include model metrics: colon_s %>% finalfit(dependent, explanatory, explanatory_multi, metrics=TRUE)
#> Waiting for profiling to be done...
#> Waiting for profiling to be done...
#> Waiting for profiling to be done...
#> Waiting for profiling to be done...
#> Waiting for profiling to be done...
#> [[1]] #> Dependent: Mortality 5 year Alive Died #> 2 Age <40 years 31 (6.1) 36 (8.9) #> 1 40-59 years 208 (40.7) 131 (32.4) #> 3 60+ years 272 (53.2) 237 (58.7) #> 8 Sex Female 243 (47.6) 194 (48.0) #> 9 Male 268 (52.4) 210 (52.0) #> 4 Obstruction No 408 (82.1) 312 (78.6) #> 5 Yes 89 (17.9) 85 (21.4) #> 6 Perforation No 497 (97.3) 391 (96.8) #> 7 Yes 14 (2.7) 13 (3.2) #> OR (univariable) OR (multivariable) #> 2 - - #> 1 0.54 (0.32-0.92, p=0.023) 0.57 (0.34-0.98, p=0.041) #> 3 0.75 (0.45-1.25, p=0.270) 0.81 (0.48-1.36, p=0.424) #> 8 - - #> 9 0.98 (0.76-1.27, p=0.889) - #> 4 - - #> 5 1.25 (0.90-1.74, p=0.189) 1.26 (0.90-1.76, p=0.176) #> 6 - - #> 7 1.18 (0.54-2.55, p=0.672) - #> #> [[2]] #> [1] "Number in dataframe = 929, Number in model = 894, Missing = 35, AIC = 1226.8, C-statistic = 0.555, H&L = Chi-sq(8) 0.06 (p=1.000)" #>
# Summary, univariable and multivariable analyses of the form: # survival::coxph(dependent ~ explanatory) explanatory = c("age.factor", "sex.factor", "obstruct.factor", "perfor.factor") dependent = "Surv(time, status)" colon_s %>% finalfit(dependent, explanatory)
#> Dependent: Surv(time, status) HR (univariable) #> 2 Age <40 years - #> 1 40-59 years 0.76 (0.53-1.09, p=0.132) #> 3 60+ years 0.93 (0.66-1.31, p=0.668) #> 8 Sex Female - #> 9 Male 1.01 (0.84-1.22, p=0.888) #> 4 Obstruction No - #> 5 Yes 1.29 (1.03-1.62, p=0.028) #> 6 Perforation No - #> 7 Yes 1.17 (0.70-1.95, p=0.556) #> HR (multivariable) #> 2 - #> 1 0.79 (0.55-1.13, p=0.196) #> 3 0.98 (0.69-1.40, p=0.926) #> 8 - #> 9 1.02 (0.85-1.23, p=0.812) #> 4 - #> 5 1.30 (1.03-1.64, p=0.026) #> 6 - #> 7 1.08 (0.64-1.81, p=0.785)
# Rather than going all-in-one, any number of subset models can # be manually added on to a summary_factorlist() table using finalfit.merge(). # This is particularly useful when models take a long-time to run or are complicated. # Note requirement for fit_id=TRUE. # `fit2df` is a subfunction extracting most common models to a dataframe. explanatory = c("age.factor", "sex.factor", "obstruct.factor", "perfor.factor") dependent = 'mort_5yr' colon_s %>% finalfit(dependent, explanatory, metrics=TRUE)
#> Waiting for profiling to be done...
#> Waiting for profiling to be done...
#> Waiting for profiling to be done...
#> Waiting for profiling to be done...
#> Waiting for profiling to be done...
#> [[1]] #> Dependent: Mortality 5 year Alive Died #> 2 Age <40 years 31 (6.1) 36 (8.9) #> 1 40-59 years 208 (40.7) 131 (32.4) #> 3 60+ years 272 (53.2) 237 (58.7) #> 8 Sex Female 243 (47.6) 194 (48.0) #> 9 Male 268 (52.4) 210 (52.0) #> 4 Obstruction No 408 (82.1) 312 (78.6) #> 5 Yes 89 (17.9) 85 (21.4) #> 6 Perforation No 497 (97.3) 391 (96.8) #> 7 Yes 14 (2.7) 13 (3.2) #> OR (univariable) OR (multivariable) #> 2 - - #> 1 0.54 (0.32-0.92, p=0.023) 0.57 (0.34-0.98, p=0.041) #> 3 0.75 (0.45-1.25, p=0.270) 0.81 (0.48-1.36, p=0.426) #> 8 - - #> 9 0.98 (0.76-1.27, p=0.889) 0.98 (0.75-1.28, p=0.902) #> 4 - - #> 5 1.25 (0.90-1.74, p=0.189) 1.25 (0.90-1.76, p=0.186) #> 6 - - #> 7 1.18 (0.54-2.55, p=0.672) 1.12 (0.51-2.44, p=0.770) #> #> [[2]] #> [1] "Number in dataframe = 929, Number in model = 894, Missing = 35, AIC = 1230.7, C-statistic = 0.56, H&L = Chi-sq(8) 5.69 (p=0.682)" #>
explanatory = c("age.factor", "sex.factor", "obstruct.factor", "perfor.factor") explanatory_multi = c("age.factor", "obstruct.factor") random_effect = "hospital" dependent = 'mort_5yr' # Separate tables colon_s %>% summary_factorlist(dependent, explanatory, fit_id=TRUE) -> example.summary colon_s %>% glmuni(dependent, explanatory) %>% fit2df(estimate_suffix=" (univariable)") -> example.univariable
#> Waiting for profiling to be done...
#> Waiting for profiling to be done...
#> Waiting for profiling to be done...
#> Waiting for profiling to be done...
colon_s %>% glmmulti(dependent, explanatory) %>% fit2df(estimate_suffix=" (multivariable)") -> example.multivariable
#> Waiting for profiling to be done...
colon_s %>% glmmixed(dependent, explanatory, random_effect) %>% fit2df(estimate_suffix=" (multilevel") -> example.multilevel # Pipe together example.summary %>% finalfit_merge(example.univariable) %>% finalfit_merge(example.multivariable) %>% finalfit_merge(example.multilevel) %>% select(-c(fit_id, index)) %>% dependent_label(colon_s, dependent) -> example.final example.final
#> Dependent: Mortality 5 year Alive Died #> 2 Age <40 years 31 (46.3) 36 (53.7) #> 1 40-59 years 208 (61.4) 131 (38.6) #> 3 60+ years 272 (53.4) 237 (46.6) #> 8 Sex Female 243 (55.6) 194 (44.4) #> 9 Male 268 (56.1) 210 (43.9) #> 4 Obstruction No 408 (56.7) 312 (43.3) #> 5 Yes 89 (51.1) 85 (48.9) #> 6 Perforation No 497 (56.0) 391 (44.0) #> 7 Yes 14 (51.9) 13 (48.1) #> OR (univariable) OR (multivariable) OR (multilevel #> 2 - - - #> 1 0.54 (0.32-0.92, p=0.023) 0.57 (0.34-0.98, p=0.041) 0.75 (0.39-1.44, p=0.382) #> 3 0.75 (0.45-1.25, p=0.270) 0.81 (0.48-1.36, p=0.426) 1.03 (0.55-1.96, p=0.916) #> 8 - - - #> 9 0.98 (0.76-1.27, p=0.889) 0.98 (0.75-1.28, p=0.902) 0.80 (0.58-1.11, p=0.180) #> 4 - - - #> 5 1.25 (0.90-1.74, p=0.189) 1.25 (0.90-1.76, p=0.186) 1.23 (0.82-1.83, p=0.320) #> 6 - - - #> 7 1.18 (0.54-2.55, p=0.672) 1.12 (0.51-2.44, p=0.770) 1.03 (0.43-2.51, p=0.940)