Creating a Table and Graph with Median Length of stay by Age Group per Month

I have a two part question

I am attempting to create a table in R with the Median length of stay in the hospital (and the IQR) by age group per month. I attempted the following code, but got a "error: object ‘AgeGroup’ not found. I checked my dataset by using “names” and this variable does exist. Is my code incorrect?

  ```

tbl_summary_ex3 ←
hospclnmatch27dec2023 %>%
select(LOS, yearmonth, AgeGroup) %>%
tbl_summary(by = yearmonth, AgeGroup,
statistic = LOS ~ “{median} ({p25} - {p75})”)




I am also attempting to create a graph in R that will display Median length of stay in the hospital by age group per month. (y axis would be LOS and x axis would be months ) I don't know how to execute this in ggplot. What is an example of code I would use?

![image|690x333](upload://fjbGZkmn460LEzVMyF25PD5FjKl.png)

Carmon, could you share a sample of your dataframe? You can use dput(head(hospclnmatch27dec2023,10)) and copy and paste what appears in your console
Lucca

1 Like

Hi Carmon,

Apparently, you would like to create a two levels (“by”) table, by age group and per month.
You can use the tbl_strata from the same package to add strata.

Your code would look like this then:

tbl_summary_ex3 <- 
  hospclnmatch27dec2023 %>%
 select(LOS, yearmonth, AgeGroup) %>%
  tbl_strata(
    strata = AgeGroup,
    ~.x %>%
      tbl_summary(
        by = yearmonth,
        statistic = LOS ~ “{median} ({p25} - {p75})”
      ) 
     )

laurent

2 Likes

I tried using the two level by table code, but I got the following error message:

tbl_summary_ex3 <- 
   hospclnmatch27dec2023 %>%
   select(LOS, yearmonth, AgeGroup) %>%
   tbl_strata(
     strata = AgeGroup,
     ~.x %>%
       tbl_summary(
         by = yearmonth,
         statistic = LOS ~ “{median} ({p25} - {p75})”
Error: unexpected input in:
"        by = yearmonth,
        statistic = LOS ~ “"

Here is the data frame

LOS = c(14, 0, 1, 3, 3, 11, 3, 6, 4, 9), AgeGroup = structure(c(“65-79 YEARS”,
“18-29 YEARS”, “65-79 YEARS”, “65-79 YEARS”, “80+ YEARS”,
“50-64 YEARS”, “50-64 YEARS”, “50-64 YEARS”, “80+ YEARS”,
“80+ YEARS”), yearmonth = structure(c(2022.75,
2021.91666666667, 2022.41666666667, 2023.5, 2020.83333333333,
2022.08333333333, 2020.83333333333, 2021.25, 2021, 2021.25
), class = “yearmon”)), row.names = c(NA, -10L), class = c(“tbl_df”,
“tbl”, “data.frame”))

1 Like

Hi Carmong,

here a reproductible code that will give you what you want.

I noticed the error above mentionned was due to quotation marks being the curly version and not the " ones.

To increase lisibility, I switched the “.combine_with” argument to “tbl_stack” (vertical) instead of the default value “tbl_merge”.
I also added few other interresting arguments to use in tbl_summary:

  • “include” to select the variables to display (no need to do a dplyr::select)

  • “label” to change the name of the variable to something more meaningful

  • “digits” to limit the number of digits

pacman::p_load(tidyverse, lubridate,gtsummary)
  

hospclnmatch27dec2023 <- 
  data.frame(
    LOS = c(14, 0, 1, 3, 3, 11, 3, 6, 4, 9),
    AgeGroup = c("65-79 YEARS","18-29 YEARS", "65-79 YEARS", "65-79 YEARS", "80+ YEARS",
                 "50-64 YEARS", "50-64 YEARS", "50-64 YEARS", "80+ YEARS", "80+ YEARS"),
    yearmonth = c(2022.75, 2021.91666666667, 2022.41666666667, 2023.5, 2020.83333333333,
                  2022.08333333333, 2020.83333333333, 2021.25, 2021, 2021.25)
           ) %>%
  mutate(AgeGroup = as.factor(AgeGroup),
         yearmonth = as.factor(format((date_decimal(yearmonth)), "%Y-%m")))

tbl_summary_ex3 <- 
  hospclnmatch27dec2023 %>%
  tbl_strata(
    strata = AgeGroup,
    .combine_with = "tbl_stack",
    .tbl_fun = ~.x %>%
      tbl_summary(data = hospclnmatch27dec2023,
                  by = yearmonth,
                  include = LOS,
                  label = LOS ~ "Length of stay (days)",
                  type = LOS ~ "continuous",
                  statistic = LOS ~ "{median} ({p25} - {p75})",
                  digits = LOS ~ 1)
  )

I tried the code that you recommended, but got the following error message below

hospclnmatch27dec2023 ←

  • data.frame(
  • LOS = c(14, 0, 1, 3, 3, 11, 3, 6, 4, 9),
    
  • AgeGroup = c("65-79 YEARS","18-29 YEARS", "65-79 YEARS", "65-79 YEARS", "80+ YEARS",
    
  •              "50-64 YEARS", "50-64 YEARS", "50-64 YEARS", "80+ YEARS", "80+ YEARS"),
    
  • yearmonth = c(2022.75, 2021.91666666667, 2022.41666666667, 2023.5, 2020.83333333333,
    
  •               2022.08333333333, 2020.83333333333, 2021.25, 2021, 2021.25)
    
  • ) %>%
  • mutate(AgeGroup = as.factor(AgeGroup),
  •      yearmonth = as.factor(format((date_decimal(yearmonth)), "%Y-%m")))
    

Error in data.frame(LOS = c(14, 0, 1, 3, 3, 11, 3, 6, 4, 9), AgeGroup = c(“65-79 YEARS”, :
could not find function “%>%”