Search code examples
gtsummarysmd

How to reference proportions when making a custom statistic in gtsummary


I am hoping to make a custom statistic column called Standardized Difference, which calculates the standardized mean difference for each categorical variable according to the formula: cat_smd=abs((p_1-p_0)/(sqrt(((p_1*(1-p_1)+p_0*(1-p_0))))/2)).

My challenge is that I don't know how to access/call/reference the proportions, but I know they have already been calculated in the tbl_summary. I think it involves {p}, but I don't know how to specify p_0 vs p_1. An example of some sample data and a table are below.

# create data
library(tidyverse)
library(gtsummary)


age = c(34, 26, 42, 39, 17, 32, 28, 35, 42, 42, 44, 21, 26, 19, 34, 38, 20, 23, 33, 39, 28, 30, 19, 30, 37, 22, 32, 26, 18, 26)

care = c(4, 2, 4, 4, 5, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 5, 4, 4, 4, 4, 4, 2, 2, 4, 4, 4, 4)

sex = c(1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1)

exp = c(1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0)

df = data.frame( age, sex, care, exp)

# create table summary
tbl = df %>%
  tbl_summary(by=exp,
              type = all_dichotomous() ~ "categorical") %>%
  add_overall() 

tbl

Example table

Desired statistic: cat_smd=abs((p_1-p_0)/(sqrt(((p_1*(1-p_1)+p_0*(1-p_0))))/2))

For example, for sex = 0, p_0 is 0.31, and p_1 is 0.50. The SMD would be 0.56.

Is anyone able to provide some advice about how to create this custom statistic?


Solution

  • I have answered this at: Add SMDs for each level of a categorical variable in gtsummary

    To summarise, you can use my package

    install.packages("devtools")
    devtools::install_github("zheer-kejlberg/Z.gtsummary.addons")
    library(Z.gtsummary.addons)
    

    And add SMDs for each level of categorical variables using %>% add_SMD(location = "level") or to get SMDs for numeric and binary variables as well %>% add_SMD(location = "both")

    For more examples, consult https://github.com/zheer-kejlberg/Z.gtsummary.addons