Vignette: Generating a Publication-Ready 'Table 1' with crosstable
Analysis by Gemini
2025-06-30
Source:vignettes/clinicopath-descriptives-11-function-crosstable.Rmd
clinicopath-descriptives-11-function-crosstable.Rmd
# Create a sample data frame for a cancer trial
set.seed(123)
n_patients <- 200
cancer_trial <- data.frame(
Age = rnorm(n_patients, mean = 65, sd = 10),
Gender = factor(sample(c("Male", "Female"), n_patients, replace = TRUE)),
Tumor_Grade = factor(sample(c("I", "II", "III"), n_patients, replace = TRUE,
prob = c(0.3, 0.5, 0.2)), ordered = TRUE),
Biomarker_X = rnorm(n_patients, mean = 100, sd = 25),
Treatment_Group = factor(sample(c("Therapy", "Placebo"), n_patients, replace = TRUE))
)
# Ensure Age is numeric and integer
cancer_trial$Age <- as.integer(cancer_trial$Age)
head(cancer_trial)
## Age Gender Tumor_Grade Biomarker_X Treatment_Group
## 1 59 Male II 98.16110 Therapy
## 2 62 Male I 70.78371 Placebo
## 3 80 Male II 84.13129 Placebo
## 4 65 Male II 99.27896 Therapy
## 5 66 Male II 116.76740 Therapy
## 6 82 Male III 58.73634 Therapy
# This code simulates how the jamovi module would be called in an R environment.
# You would need the ClinicoPathDescriptives package installed.
# Load the library
library(ClinicoPath)
## Registered S3 method overwritten by 'future':
## method from
## all.equal.connection parallelly
## Warning: replacing previous import 'dplyr::select' by 'jmvcore::select' when
## loading 'ClinicoPath'
## Warning: replacing previous import 'cutpointr::roc' by 'pROC::roc' when loading
## 'ClinicoPath'
## Warning: replacing previous import 'cutpointr::auc' by 'pROC::auc' when loading
## 'ClinicoPath'
## Warning: replacing previous import 'magrittr::extract' by 'tidyr::extract' when
## loading 'ClinicoPath'
## Warning in check_dep_version(): ABI version mismatch:
## lme4 was built with Matrix ABI version 1
## Current Matrix ABI version is 0
## Please re-install lme4 from source or restore original 'Matrix' package
## Warning: replacing previous import 'jmvcore::select' by 'dplyr::select' when
## loading 'ClinicoPath'
## Registered S3 methods overwritten by 'ggpp':
## method from
## heightDetails.titleGrob ggplot2
## widthDetails.titleGrob ggplot2
## Warning: replacing previous import 'DataExplorer::plot_histogram' by
## 'grafify::plot_histogram' when loading 'ClinicoPath'
## Warning: replacing previous import 'ROCR::plot' by 'graphics::plot' when
## loading 'ClinicoPath'
## Warning: replacing previous import 'dplyr::select' by 'jmvcore::select' when
## loading 'ClinicoPath'
## Warning: replacing previous import 'tibble::view' by 'summarytools::view' when
## loading 'ClinicoPath'
# Run the crosstable analysis with NEJM style
results <- crosstable(
data = cancer_trial,
vars = c("Age", "Gender", "Tumor_Grade", "Biomarker_X"),
group = "Treatment_Group",
sty = "nejm" # Specify the New England Journal of Medicine style
)
# The result is an HTML table, which would render directly in jamovi.
# In R, you would access it like this:
# results$tablestyle4
htmltools::HTML(results$tablestyle4$content)
Cross Table for Dependent treatment_group
N | Placebo | Therapy | Test Statistic | |
---|---|---|---|---|
(N=93) | (N=107) | |||
Age | 200 | 58.0 64.0 71.0 | 58.0 64.0 70.0 | F1,198=0.01, P=0.923 |
Gender : Male | 200 | 0.5 42/93 | 0.5 51/107 | Χ2 1=0.13, P=0.722 |
Tumor_Grade | 200 | Χ2 2=0.88, P=0.652 | ||
I | 0.3 29/93 | 0.3 27/107 | ||
II | 0.5 45/93 | 0.5 56/107 | ||
III | 0.2 19/93 | 0.2 24/107 | ||
Biomarker_X | 200 | 91.7 104.1 118.2 | 83.9 98.2 114.9 | F1,198=3.22, P=0.073 |
N is the number of non-missing value. 1Kruskal-Wallis. 2Pearson. 3Wilcoxon.