从输入 tbl_uvregression 和 geepack::geeglm 的变量中删除 NA,而不只保留完整记录

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

我正在使用 gtsummary::tbl_uvregression 构建一个用 geepack::geeglm 建模的单变量回归 table。

一些变量包含破坏 table 中 GEE 模型的 NA 值。

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

您通常会为 geepack::geeglm 设置 data = na.omit(data)。但是,我只想删除我在 table 中查看的变量的 NA。 na.action = na.omit 不起作用。我想避免为每个单独的变量计算 gtsummary::tbl_uvregression table。

如何指示 tbl_uvregression 仅删除正在生成的特定模型的 NA。这是我的尝试:

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

)

谢谢!

geepack::geeglm() 很有趣,它不为我们处理 NA 值。当我遇到这个问题时,我为 geeglm() 编写了一个小包装函数,它在传递给 geeglm() 之前删除缺失值。下面的例子!编程愉快!

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

reprex package (v2.0.1)

于 2022-02-09 创建