spgarbet / tangram

Table Grammar package for R
66 stars 3 forks source link

Trouble adding a column #72

Closed kylerove closed 2 years ago

kylerove commented 2 years ago

Hey,

I'm doing an analysis for a randomized study and have a table with the two arms and various outcomes. I want to run standard statistical tests on categorical and continuous variables and output the p value. I also want an extra column that lists the 95% confidence interval for the point estimate. I tried to create my own transform table based on the example, but it returns blank. I'm sure it's something I'm doing incorrectly.

rct_cat_by_cat <- function(tb, row, col, display_percent=TRUE,...)
{
  datar          <- row$data
  datac          <- col$data
  grid           <- table(row$data, col$data)
  lvls           <- levels(datac)
  colN   <- lapply(lvls, function(cat)
    cell_n(length(datac[datac == cat & !is.na(datac)]), subcol=cat))

  # Tests
  tests <- chiTests(grid)
  effectEst <- riskDifferenceBinomial(datar, datac)

  # Build the table
  tb                                                                          %>%
  # Create Headers
  row_header(derive_label(row))                                               %>%
  col_header(lvls, "Effect estimate or risk difference (95% CI)", "P-value")  %>%
  col_header(colN, "",                                          ""         )  %>%
  # Now add quantiles for the counts
  table_apply(colnames(grid), FUN=
                function(tbl, colnm) {
                  # Compute each data set
                  rownm <- rownames(grid)[[1]]
                  denom <- sum(grid[,colnm])
                  numer <- grid[rownm, colnm]
                  x     <- if(display_percent) paste0(numer, " (", render_f(100*numer/denom, 1), ")") else
                             as.character(numer)

                  # Add a column that is a quantile
                  add_col(tbl, cell(x, subcol = colnm, subrow = rownm))
                })                                                            %>%
  # Now add a statistical test for the final column
  add_col(effectEst)                                                          %>%
  add_col(tests)

  tb

And this is called by the transform:

rct <- list(
  Type = hmisc_data_type,
  Numerical   = list(
    Numerical   = NA,
    Categorical = rct_cont_by_cat
  ),
  Categorical = list(
    Numerical   = NA,
    Categorical = rct_cat_by_cat
  ),
  Footnote    = "Count (Percent). ^1^ Student's t-test. ^2^ Fisher exact. ^3^ Risk difference with 95% CI. ^4^ Effect estimate with 95% CI."
)

rct_cont_by_cat is not shown. Hope all is well!

kylerove commented 2 years ago

Simpler solution would probably be just to change the format of the p-value cell to include the 95% confidence interval but I wanted to try a custom transform table because I'm sure I'll need to do this for other projects in the future.

spgarbet commented 2 years ago

I just now saw this (buried in emails). I'll see what I can do with it. Creating your own is the way to go. Most errors are just incorrect rowxcol counts. I need some better tools to detect this and give better error messages.

spgarbet commented 2 years ago

You missed the assigned to the returned variable.

library(tangram)

rct_cat_by_cat <- function(tb, row, col, display_percent=TRUE,...)
{
  datar          <- row$data
  datac          <- col$data
  grid           <- table(row$data, col$data)
  lvls           <- levels(datac)
  colN   <- lapply(lvls, function(cat)
    cell_n(length(datac[datac == cat & !is.na(datac)]), subcol=cat))

  # Tests
  tests <- "chiTests(grid)"
  effectEst <- "riskDifferenceBinomial(datar, datac)"

  # Build the table
  tb  <-
  # Create Headers
  row_header(tb, derive_label(row))                                               %>%
  col_header(lvls, "Effect estimate or risk difference (95% CI)", "P-value")  %>%
  col_header(rep("",length(lvls)), colN, "",                    ""         )  %>%
  # Now add quantiles for the counts
  table_apply(colnames(grid), FUN=
                function(tbl, colnm) {
                  # Compute each data set
                  rownm <- rownames(grid)[[1]]
                  denom <- sum(grid[,colnm])
                  numer <- grid[rownm, colnm]
                  x     <- if(display_percent) paste0(numer, " (", render_f(100*numer/denom, 1), ")") else
                             as.character(numer)

                  # Add a column that is a quantile
                  add_col(tbl, cell(x, subcol = colnm, subrow = rownm))
                })                                                            %>%
  # Now add a statistical test for the final column
  add_col(effectEst)                                                          %>%
  add_col(tests)

  tb
}

rct <- list(
  Type = hmisc_data_type,
  Numerical   = list(
    Numerical   = NA,
    Categorical = NA
  ),
  Categorical = list(
    Numerical   = NA,
    Categorical = rct_cat_by_cat
  ),
  Footnote    = "Count (Percent). ^1^ Student's t-test. ^2^ Fisher exact. ^3^ Risk difference with 95% CI. ^4^ Effect estimate with 95% CI."
)

tangram(drug ~ spiders, data=pbc, transforms=rct)