Skip to content

Instantly share code, notes, and snippets.

View ddsjoberg's full-sized avatar

Daniel Sjoberg ddsjoberg

View GitHub Profile
@ddsjoberg
ddsjoberg / bigglm.R
Last active May 15, 2022 20:45
bigglm() gtsummary example, step by step
library(gtsummary)
library(broom.helpers)
library(tidyverse)
packageVersion("gtsummary")
#> [1] '1.6.0'
# build model
mod <- biglm::bigglm(response ~ age + trt, data = trial, family = binomial())
# build a fancy tidy data frame one step at a time
library(gtsummary)
library(tidyverse)
trial %>%
tbl_strata(
strata = trt,
~ .x %>%
nest(data = -grade) %>%
arrange(grade) %>%
rowwise() %>%
@ddsjoberg
ddsjoberg / gt_rainbow_stripes.R
Last active June 3, 2021 12:19
Function to animate rainbow row striping in a gt table
gt_rainbow_stripes <- function(x, rep_n = 5) {
x <-
gt::cols_width(x, label ~ gt::px(130)) %>%
gt::tab_header(
title = x[["_heading"]]$title,
subtitle = "But Make it G - A - Y"
) %>%
gt::tab_options(heading.subtitle.font.size = 20,
heading.subtitle.font.weight = "bolder")
@ddsjoberg
ddsjoberg / multinom_pivot_wider.R
Created May 11, 2021 12:14
function to display multinomial regression models in wide format
set.seed(20210511)
library(gtsummary)
library(magrittr)
multinom_pivot_wider <- function(x) {
# check inputs match expectatations
if (!inherits(x, "tbl_regression") || !inherits(x$model_obj, "multinom")) {
stop("`x=` must be class 'tbl_regression' summary of a `nnet::multinom()` model.")
}
# ANALYSIS Rmd FILE
```{r}
# create a gtsummary table
tbl <- trial %>% tbl_summary()
# save it to file
saveRDS(tbl, file = "my_tbl_summary1.Rds")
```
@ddsjoberg
ddsjoberg / split_gtsummary_tbl.R
Created February 9, 2021 14:48
Split a gtsummary table
split_gtsummary_tbl <- function(x, .split_after) {
# get row index where splits occur
df_index <-
.split_after %>%
purrr::map_int(~x$table_body$variable %in% .x %>%
which() %>%
max()) %>%
{union(., nrow(x$table_body))} %>%
sort() %>%
use_significance_stars <- function(x) {
if (!"estimate" %in% names(x$table_body)) return(x)
# extracting old estimate fun
old_est_fun <- switch(
!is.null(x$table_header),
x$table_header %>%
dplyr::filter(column == "estimate") %>%
purrr::pluck("fmt_fun", 1)
)
library(gtsummary)
trial %>%
select(marker, trt) %>%
tbl_summary(
by = trt,
missing = "no",
statistic = everything() ~ "{mean} ({sd})",
# use a function to style output, rather than specify number of decimal places
digits = everything() ~ style_sigfig
# Original stack overflow post
# https://stackoverflow.com/questions/65673290/merging-tbl-svysummary-and-stacked-tbl-regression-tables-with-different-variable
library(gtsummary)
packageVersion("gtsummary")
#> '1.3.6'
# 1. build reg models for varying outcomes,
# 2. show covariate for age in table for all outcomes,
# 3. adjust all models for marker level
#' @import dplyr %>%
#' @import purrr %||%
tbl_cmh <- function(data, case, exposure, strata,
label = NULL,
estimate_fun = gtsummary::style_ratio,
overall_or = TRUE,
overall_label = "Crude") {
# converting selectors to character names ------------------------------------
case <- dplyr::select(data, {{ case }}) %>% names()
exposure <- dplyr::select(data, {{ exposure }}) %>% names()