rqwraps2

Combining the mean and standard deviation and the mean and confidence intervals from qwraps2


I am using R markdown and qwraps2 package

I have a dataset with 2 numeric variables that I want to get mean, standard deviation and 95% confidence interval for. The dataset is below:

structure(list(PREOP_mTFA = c(163.5, 164.9, 168.7, 170.3, 162.8, 
166.7, 171, 165.9, 165.9, 170.8, 170.5, 173.3, 167.7, 170.7, 
159, 170.9, 168.2, 171.2, 164, 166.6, 169.1, 171.2, 175.9), PREOP_mLDFA = c(86, 
95, 90, 86, 92, 89, 92, 96, 90, 86, 89, 87, 93, 90, 98, 89, 90, 
88, 92, 91, 89, 90, 88)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -23L))

However, I need to get it in the following format : mean ± standard deviation [ lower confidence interval, upper confidence interval]

I used the following codes to get the mean mean ± standard deviation

library (qwraps2)
SUMMARY_DLO_PRE_TEST <- 
  list("Preoperative mTFA" =
         list("mean (sd)" = ~ qwraps2::mean_sd(PREOP_mTFA, na_rm = TRUE, denote_sd = "pm", digits = 1, show_n = "never")),
       "Preoperative mLDFA" =
         list("mean (sd)" = ~ qwraps2::mean_sd(PREOP_mLDFA, na_rm = TRUE, denote_sd = "pm", digits = 1, show_n = "never"))
  )
DLO_PRE_TEST <- summary_table(DLO,SUMMARY_DLO_PRE_TEST)
DLO_PRE_TEST

And I used the following code to get the mean [ lower confidence interval, upper confidence interval]

mci_PREOP_mTFA_TEST <- qwraps2::mean_ci(DLO$PREOP_mTFA, na_rm = TRUE)
frmtci(mci_PREOP_mTFA_TEST, est= 1, lcl= 2, ucl = 3, format = "est [lcl, ucl]", show_level = TRUE, digits = 1)

mci_PREOP_mLDFA_TEST <- qwraps2::mean_ci(DLO$PREOP_mLDFA, na_rm = TRUE)
frmtci(mci_PREOP_mLDFA_TEST, est= 1, lcl= 2, ucl = 3, format = "est [lcl, ucl]", show_level = TRUE, digits = 1)

However, the mean comes in the output from every code, the mean and standard deviation code then the mean and confidence interval code

Is there a way to combine the 2 codes to get the format in this way: mean ± standard deviation [ lower confidence interval, upper confidence interval] Bearing in mind that I use qwraps2 to get data into nice tables.

I tried the mentioned code in the body of the question. I am expecting an output from one code in this format: mean ± standard deviation [ lower confidence interval, upper confidence interval]


Solution

  • You need to do it this way:

    library(qwraps2)
    library(dplyr)
    
    DLO <- structure(list(
      PREOP_mTFA = c(163.5, 164.9, 168.7, 170.3, 162.8, 166.7, 171, 165.9, 165.9, 170.8, 170.5, 173.3, 167.7, 170.7, 159, 170.9, 168.2, 171.2, 164, 166.6, 169.1, 171.2, 175.9),
      PREOP_mLDFA = c(86, 95, 90, 86, 92, 89, 92, 96, 90, 86, 89, 87, 93, 90, 98, 89, 90, 88, 92, 91, 89, 90, 88)
    ), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -23L))
    
    mean_sd_ci <- function(x) {
      mean_val <- mean(x, na.rm = TRUE)
      sd_val <- sd(x, na.rm = TRUE)
      ci <- qwraps2::mean_ci(x, na_rm = TRUE)
      formatted_output <- sprintf("%.1f ± %.1f [%.1f, %.1f]", mean_val, sd_val, ci[2], ci[3])
      return(formatted_output)
    }
    
    SUMMARY_DLO_PRE_TEST <- 
      list("Preoperative mTFA" =
             list("mean ± sd [CI]" = ~ mean_sd_ci(PREOP_mTFA)),
           "Preoperative mLDFA" =
             list("mean ± sd [CI]" = ~ mean_sd_ci(PREOP_mLDFA))
      )
    
    DLO_PRE_TEST <- summary_table(DLO, SUMMARY_DLO_PRE_TEST)
    DLO_PRE_TEST
    

    which will give you the table:

    \begin{tabular}{l|l}
    \hline
     & DLO (N = 23)\\
    \hline
    \bf{Preoperative mTFA} & ~\\
    \hline
    ~~ mean ± sd [CI] & 168.2 ± 3.8 [166.6, 169.8]\\
    \hline
    \bf{Preoperative mLDFA} & ~\\
    \hline
    ~~ mean ± sd [CI] & 90.3 ± 3.1 [89.0, 91.5]\\
    \hline
    \end{tabular}