'Is it possible to create a stratified table (tbl_strata) using tbl_svysummary()?

I am pretty new to survey data and gtsumarry package. I'm trying to create a stratified table from the survey data using the following code, and I get the error "Error: Problem with mutate() input tbl".

# Reading the subset of the data

fileUrl <- "https://raw.github.com/Shadi-Sadie/Paper-1-Cancer-Screening-and-Immigrants/master/Cleaned%20Data/subset.csv"
SData<-read.csv( fileUrl , header = TRUE, sep ="," )

# Setting the weight 

options( "survey.replicates.mse" = TRUE)
svy <- svrepdesign(repweights = "PWGTP[0-9]+",
                   weights = ~PWGTP,
                   combined.weights = TRUE,
                   type = "JK1",
                   scale = 4/80, rscales = rep(1, 80),
                   data = SData)

# creating the table

SData %>%
        select(CITG, HICOV, ESRG , EXPANSION) %>%
        tbl_strata(
                strata = CITG,
                .tbl_fun =
                        ~ .x %>% tbl_svysummary(
                                by = EXPANSION,
                                include = c(CITG, HICOV, ESRG , EXPANSION),
                                label = list(CITG ~ "Nativity",
                                             HICOV~ "Any health insurance",
                                             ESRG~ "Employment",
                                             EXPANSION ~ "Expansion" )
                        )
        )

If it is possible to use tbl_svysummary() with the tbl_strata() could anyone tell me where I'm doing wrong?



Solution 1:[1]

Thanks for updating with a reproducible post. I made the following changes:

  1. You were passing the data frame to tbl_strata() and it needed to be updated to the survey design object.
  2. The stratifying variable should no be listed in the tbl_summary(include=) argument.

Happy Porgramming!

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

fileUrl <- "https://raw.github.com/Shadi-Sadie/Paper-1-Cancer-Screening-and-Immigrants/master/Cleaned%20Data/subset.csv"
SData <- read.csv(fileUrl, header = TRUE, sep = ",")

# Setting the weight
options("survey.replicates.mse" = TRUE)
svy <- survey::svrepdesign(
  repweights = "PWGTP[0-9]+",
  weights = ~PWGTP,
  combined.weights = TRUE,
  type = "JK1",
  scale = 4 / 80, rscales = rep(1, 80),
  data = SData
)

# creating the table
tbl <-
  svy %>%
  tbl_strata(
    strata = CITG,
    .tbl_fun =
      ~ .x %>% tbl_svysummary(
        by = EXPANSION,
        include = c(HICOV, ESRG, EXPANSION),
        label = list(
          HICOV = "Any health insurance",
          ESRG = "Employment",
          EXPANSION = "Expansion"
        )
      )
  )

enter image description here Created on 2022-05-01 by the reprex package (v2.0.1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1