Remove NAs from variables feeding into tbl_uvregression and geepack::geeglm without keeping only complete records

Viewed 152

I'm using gtsummary::tbl_uvregression to construct a univariate regression table modeled with geepack::geeglm.

Some variables contain NA values that are breaking the GEE models in the table.

x There was an error constructing model geepack::geeglm(formula = tts_participant ~ omb_race, data = ., family = poisson, id = School Name, corstr = "independence", scale.fix = TRUE) See error below. Error in mutate_cols(): ! Problem with mutate() column model. i model = map(...). x Error in geese.fit(xx, yy, id, offset, soffset, w, waves = waves, zsca, : nrow(zsca) and length(y) not match Caused by error: ! Error in geese.fit(xx, yy, id, offset, soffset, w, waves = waves, zsca, : nrow(zsca) and length(y) not match

You would generally set data = na.omit(data) for geepack::geeglm. However, I only want to remove NAs for the variables that I'm looking at in the table. na.action = na.omit does not work. I would like to avoid computing a gtsummary::tbl_uvregression table for each individual variable.

How do I instruct tbl_uvregression to remove the NAs only for the specific model being generated. Here is my attempt:

tbl_uvregression(
method = geepack::geeglm,
y = tts_participant,
include = -`School Name`,
method.args = list(
  family = poisson,
  data = na.omit(data),
  #na.action = na.omit,
  id = `School Name`,
  corstr = "independence",
  scale.fix = TRUE
),
exponentiate = TRUE,
add_estimate_to_reference_rows = FALSE

)

Thank you!

1 Answers

The geepack::geeglm() is funny how it doesn't handle the NA values for us. When I've come across this, I write a small wrapper function for geeglm() that deletes the missing values before passing to geeglm(). Example below! Happy Programming!

library(gtsummary)
library(tidyverse)
packageVersion("gtsummary")
#> [1] '1.5.2'

my_geeglm <- function(formula, data, id, ...) {
  # capture id input (since it's unquoted)
  id <- rlang::enexpr(id)
  
  # keep compelte cases amoung the variables needed in the model
  data <-
    select(data, all_of(all.vars(formula)), !!id) %>%
    dplyr::filter(complete.cases(.))
  
  # build GEE model
  rlang::inject(
    geepack::geeglm(
      formula = formula,
      data = data, 
      id = !!id, # inserting unquoted id column name
      ...
    )
  )
}

data(dietox, package = "geepack")

dietox %>%
  select(Pig, Weight, Cu, Feed) %>%
  tbl_uvregression(
    y = Weight, 
    method = my_geeglm,
    method.args = list(id = Pig, family = poisson("identity"), corstr = "ar1"),
    include = -Pig
  ) %>%
  as_kable() # convert to kable to show in SO
Characteristic N Beta 95% CI p-value
Cu 861
Cu000 — —
Cu035 -0.49 -3.5, 2.5 0.7
Cu175 1.8 -1.9, 5.5 0.3
Feed 789 0.43 0.42, 0.45 <0.001

Created on 2022-02-09 by the reprex package (v2.0.1)

Related