使用svydesign对象和tbl_svysummary?对表1进行分层

dced5bon  于 2023-02-20  发布在  其他
关注(0)|答案(1)|浏览(167)

我试图为NHANES调查数据创建一个表1,首先通过肥胖与非肥胖的二元变量分层,然后通过对照/治疗组状态的二元变量(“wlp_yn”)再次分层。我想获得分类特征的计数(%)和连续基线特征的平均值(SE)。对于这些计数和平均值,我也试图获得p值。
我尝试过使用tbl_svysummary()、svyby()、tbl_strata()和CreateTableOne(),但都没有成功。
在下面的代码中,我将整个数据集划分为一个较小的数据集,其中只包含对照组数据(“obese_adults”),以便首先对表进行划分。我还从年龄作为特征开始(“age_group”是“RIDAGEYR”连续变量的分类版本)。我无法理解它,但我很好奇是否有其他的编码方式?

add_p_svysummary_ex1 <-
  obese_adults %>%
  tbl_svysummary(by = wlp_yn, percent = "row", include = c(age_group, RIDAGEYR), 
statistic = list(all_continuous() ~ "{mean} ({sd})")) %>%
  add_p()
add_p_svysummary_ex1
  

svyby(~RIDAGEYR, ~age_group+wlp_yn, obese_adults, svymean) # avg age of each age group

先谢了!真的很感激你的帮助。
编辑:这是重现性代码的简化版本

# DEMO
demo <- nhanes('DEMO')
demo_vars <- names(demo)
demo2 <- nhanesTranslate('DEMO', demo_vars, data = demo)

# PRESCRIPTION MEDICATIONS
rxq_rx <- nhanes('RXQ_RX')
rxq_rx_vars <- names(rxq_rx)
rxq_rx2 <- nhanesTranslate('RXQ_RX', rxq_rx_vars, data = rxq_rx)
rxq_rx2 <- rxq_rx2 %>% select("SEQN", "RXD240B") %>% filter(!is.na(RXD240B)) %>% group_by(SEQN) %>% dplyr::summarise(across(everything(), ~toString(na.omit(.))))

nhanesAnalysis = join_all(list(demo2, rxq_rx2), by = "SEQN", type = "full")

# Reconstructing survey weights for combining 1999-2018 - Combining ten survey cycles (twenty years) 
nhanesAnalysis$wtint20yr <- ifelse(nhanesAnalysis$SDDSRVYR %in% c(1,2), (2/10 * nhanesAnalysis$WTINT4YR), # for 1999-2002
                                          (1/10 * nhanesAnalysis$WTINT2YR)) # for 2003-2018

# sample weights 
nhanesDesign <- svydesign(id      = ~SDMVPSU,
                          strata  = ~SDMVSTRA,
                          weights = ~wtint20yr,
                          nest    = TRUE,
                          data    = nhanesAnalysis)

# subset
obese_adults  <- subset(nhanesDesign, (obesity == 1 & !is.na(BMXBMI) & RIDAGEYR >= 60))
iqih9akk

iqih9akk1#

这是你要找的吗?一个双假分裂:

library(gtsummary)
library(tidyverse)

data(mtcars)

mtcars %>%
  select(am, cyl, hp, vs) %>%
  dplyr::mutate(
    vs = factor(vs, labels = c("Obese", "Non-Obese")),
    am = factor(am, labels = c("Control", "Treatment")),
    cyl = paste(cyl, "Cylinder")
  ) %>%
  tbl_strata(
    strata = vs,
    ~.x %>%
      tbl_summary(
        by = am,
        type = where(is.numeric) ~ "continuous"
      ) %>%
      modify_header(all_stat_cols() ~ "**{level}**")
  )

我不知道您为什么喜欢在这里使用tbl_svysummary(),它是为测量权重而设计的。

相关问题