Enhanced Cross Tables with danchaltiel/crosstable - Professional Clinical Research
Advanced cross-tabulation analysis with tidyselect syntax and automated reporting
ClinicoPath
2025-07-13
Source:vignettes/clinicopath-descriptives-13-enhancedcrosstable.Rmd
clinicopath-descriptives-13-enhancedcrosstable.Rmd
Introduction to Enhanced Cross Tables
The enhancedcrosstable
function in ClinicoPath
integrates the powerful danchaltiel/crosstable package to provide
advanced cross-tabulation capabilities beyond standard contingency
tables. This module offers sophisticated data analysis features
specifically designed for clinical research, including automated
statistical test selection, comprehensive effect size calculations, and
publication-ready formatting.
What Makes Enhanced Cross Tables Special?
Advanced Functionality
- Tidyselect Syntax: Modern R syntax for variable selection and manipulation
- Formula Interface: Transform variables on-the-fly during analysis
- Automated Testing: Intelligent statistical test selection based on data characteristics
- Effect Sizes: Comprehensive effect size calculations with confidence intervals
- Professional Output: Publication-ready formatting for medical journals
Clinical Research Focus
- Medical Terminology: Clinically appropriate variable labels and descriptions
- Regulatory Compliance: Output formats suitable for regulatory submissions
- Flexible Reporting: Multiple export options including Word, HTML, and CSV
- Statistical Rigor: Appropriate tests for medical research standards
When to Use Enhanced Cross Tables
Perfect for: - Clinical trial baseline characteristic tables (Table 1) - Case-control studies examining risk factor associations - Diagnostic test evaluation and validation studies - Epidemiological research with categorical exposures and outcomes - Multi-center studies requiring standardized reporting - Regulatory submissions requiring detailed statistical analysis
Key Advantages: - Superior functionality compared to basic cross-tabulation - Automated statistical test selection reduces analysis errors - Professional formatting saves time in manuscript preparation - Comprehensive documentation supports reproducible research
Getting Started
Installation Requirements
The enhanced cross-table functionality requires the
crosstable
package:
# Install from CRAN
install.packages("crosstable")
# For enhanced export features
install.packages("officer")
install.packages("flextable")
# Supporting packages (automatically handled)
install.packages(c("janitor", "labelled", "stringr", "dplyr"))
Basic Syntax Comparison
Standard crosstable vs Enhanced crosstable
# Standard ClinicoPath crosstable
crosstable(
data = histopathology,
row_var = "Grade_Level",
col_var = "Group",
add_margins = TRUE
)
# Enhanced crosstable with danchaltiel integration
enhancedcrosstable(
data = histopathology,
vars = c("Grade_Level", "LVI", "PNI"),
by_var = "Group",
test_auto = TRUE,
effect_size = TRUE,
export_format = "html"
)
Clinical Research Applications
Example 1: Clinical Trial Baseline Characteristics
This example demonstrates creating a comprehensive Table 1 for a clinical trial using the histopathology dataset:
library(ClinicoPath)
# Load the histopathology dataset
data("histopathology")
# Basic enhanced cross-table for baseline characteristics
enhancedcrosstable(
data = histopathology,
vars = c("Age", "Sex", "Race", "Grade_Level", "LVI", "PNI"),
by_var = "Group",
percent_pattern = "col_percent",
test_auto = TRUE,
effect_size = FALSE,
funs_arg = "mean_sd",
show_total = TRUE,
show_total_row = TRUE,
use_labels = TRUE,
export_format = "html"
)
Clinical Interpretation: - Automatically selects appropriate tests (t-test for Age, chi-square for categorical variables) - Column percentages show distribution within each treatment group - Professional formatting suitable for publication - Variable labels enhance readability for clinical audiences
Example 2: Risk Factor Analysis with Effect Sizes
Examining associations between pathological features and outcomes:
# Risk factor analysis with comprehensive effect sizes
enhancedcrosstable(
data = histopathology,
vars = c("LVI", "PNI", "LymphNodeMetastasis", "Grade_Level"),
by_var = "Mortality5yr",
percent_pattern = "row_percent",
test_auto = TRUE,
effect_size = TRUE,
margin = "row",
show_interpretation = TRUE,
export_format = "flextable"
)
Advanced Features: - Row percentages show risk within each exposure category - Effect sizes (Cramer’s V, odds ratios) quantify association strength - Statistical interpretation provides clinical context - Flextable output ready for Word documents
Example 3: Diagnostic Test Evaluation
Analyzing diagnostic test performance across different populations:
# Diagnostic test evaluation
enhancedcrosstable(
data = histopathology,
vars = c("New Test", "Golden Standart"),
by_var = "Disease Status",
percent_pattern = "col_percent",
test_auto = TRUE,
effect_size = TRUE,
cor_method = "spearman",
showNA = "ifany",
compact = FALSE,
summary_statistics = TRUE
)
Key Features: - Evaluates test performance across disease status - Spearman correlation for ordinal diagnostic scores - Missing data handling with transparency - Summary statistics provide additional context
Advanced Features and Customization
Percentage Pattern Options
The enhanced cross-table offers sophisticated percentage display patterns:
Pattern Types
-
col_percent
:{n} ({p_col}%)
- Percentages within columns -
row_percent
:{n} ({p_row}%)
- Percentages within rows
-
total_percent
:{n} ({p_tot}%)
- Percentages of total -
count_only
:{n} only
- Raw counts without percentages -
percent_only
:{p_col} only
- Percentages without counts
Clinical Applications
# For prevalence studies (column percentages)
enhancedcrosstable(
data = histopathology,
vars = "LVI",
by_var = "Sex",
percent_pattern = "col_percent" # Shows prevalence within each sex
)
# For risk analysis (row percentages)
enhancedcrosstable(
data = histopathology,
vars = "Grade_Level",
by_var = "Mortality5yr",
percent_pattern = "row_percent" # Shows mortality risk within each grade
)
Statistical Test Automation
The enhanced cross-table automatically selects appropriate statistical tests:
Test Selection Logic
- Chi-square test: For expected frequencies ≥ 5
- Fisher’s exact test: For small expected frequencies (< 5)
- t-test: For continuous variables with 2 groups
- ANOVA: For continuous variables with >2 groups
- Correlation tests: For continuous-continuous associations
Custom Test Selection
# Force specific statistical approaches
enhancedcrosstable(
data = histopathology,
vars = c("Age", "MeasurementA"),
by_var = "Group",
cor_method = "spearman", # Non-parametric correlations
funs_arg = "median_q1q3" # Robust summary statistics
)
Effect Size Calculations
Comprehensive effect size analysis provides clinical significance context:
Available Effect Sizes
- Cramer’s V: For categorical associations (small: 0.1, medium: 0.3, large: 0.5)
- Odds Ratios: For 2×2 tables with confidence intervals
- Phi Coefficient: For binary associations
- Cohen’s d: For continuous variable comparisons
Clinical Interpretation
# Comprehensive effect size analysis
enhancedcrosstable(
data = histopathology,
vars = c("LVI", "PNI", "Grade_Level"),
by_var = "Outcome",
effect_size = TRUE,
show_interpretation = TRUE
)
Interpretation Guidelines: - Effect sizes quantify practical significance beyond p-values - Confidence intervals provide precision estimates - Clinical guidelines help interpret magnitude of effects - Essential for evidence-based medicine applications
Export and Reporting Options
Publication-Ready Outputs
HTML Tables
# Professional HTML output for web reports
enhancedcrosstable(
data = histopathology,
vars = c("Sex", "Age", "Grade_Level"),
by_var = "Group",
export_format = "html",
compact = FALSE,
use_labels = TRUE
)
Word Document Integration
# Flextable output for Word manuscripts
enhancedcrosstable(
data = histopathology,
vars = c("LVI", "PNI", "LymphNodeMetastasis"),
by_var = "Mortality5yr",
export_format = "flextable",
digits = 2,
show_n_col = TRUE
)
CSV Data Export
# CSV export for further analysis
enhancedcrosstable(
data = histopathology,
vars = c("Grade_Level", "TStage"),
by_var = "Group",
export_format = "csv",
exclude_missing = TRUE
)
Customization Options
Display Formatting
# Customized display options
enhancedcrosstable(
data = histopathology,
vars = c("Age", "OverallTime"),
by_var = "Mortality5yr",
digits = 3, # Decimal precision
compact = TRUE, # Space-efficient layout
show_total = TRUE, # Include totals
show_total_row = TRUE, # Include row totals
show_n_col = TRUE # Display column sample sizes
)
Missing Data Handling
# Comprehensive missing data options
enhancedcrosstable(
data = histopathology,
vars = c("Race", "Smoker"),
by_var = "Group",
exclude_missing = FALSE, # Include missing in analysis
showNA = "ifany", # Show missing if present
use_labels = TRUE # Use descriptive labels
)
Best Practices for Clinical Research
Design Recommendations
Variable Selection
- Primary Variables: Focus on clinically meaningful associations
- Confounding Variables: Include known confounders for stratified analysis
- Sample Size: Ensure adequate power for subgroup analyses
Quality Assurance
Data Validation
# Pre-analysis data quality check
summary(histopathology[c("Group", "Mortality5yr", "LVI")])
# Enhanced cross-table with validation
enhancedcrosstable(
data = histopathology,
vars = c("LVI", "PNI"),
by_var = "Group",
exclude_missing = TRUE, # Remove incomplete cases
show_interpretation = TRUE, # Include statistical guidance
summary_statistics = TRUE # Display sample size information
)
Reproducibility Standards
# Document analysis parameters for reproducibility
analysis_params <- list(
dataset = "histopathology v1.0",
variables = c("LVI", "PNI", "Grade_Level"),
grouping = "Group",
test_method = "automatic",
missing_data = "excluded",
effect_sizes = TRUE,
alpha_level = 0.05
)
# Include in analysis output
enhancedcrosstable(
data = histopathology,
vars = analysis_params$variables,
by_var = analysis_params$grouping,
exclude_missing = TRUE,
test_auto = TRUE,
effect_size = TRUE
)
Comparison with Standard Cross Tables
Functionality Comparison
Feature | Standard crosstable | Enhanced crosstable |
---|---|---|
Variable Selection | Single pair | Multiple variables |
Statistical Tests | Manual selection | Automated selection |
Effect Sizes | Manual calculation | Automated calculation |
Export Options | Basic HTML | HTML, Word, CSV |
Missing Data | Basic handling | Comprehensive options |
Tidyselect Syntax | Not supported | Full support |
Formula Interface | Limited | Advanced transformations |
Troubleshooting and Support
Common Issues
Package Dependencies
# Check required packages
required_packages <- c("crosstable", "officer", "janitor", "labelled")
for (pkg in required_packages) {
if (!requireNamespace(pkg, quietly = TRUE)) {
install.packages(pkg)
}
}
Export Problems
# Fallback to HTML if flextable fails
enhancedcrosstable(
data = histopathology,
vars = "LVI",
by_var = "Group",
export_format = "html" # Reliable fallback option
)
Getting Help
Documentation Resources
-
Function Help:
?enhancedcrosstable
for parameter details -
Package Documentation:
help(package = "crosstable")
for advanced features - Clinical Examples: Additional vignettes for specific research contexts
Advanced Topics
Integration with Clinical Workflows
EMR Data Integration
# Example with electronic medical record data
emr_data <- data.frame(
patient_id = 1:200,
age_group = factor(sample(c("Young", "Middle", "Old"), 200, replace = TRUE)),
comorbidity = factor(sample(c("Yes", "No"), 200, replace = TRUE)),
treatment = factor(sample(c("Standard", "Experimental"), 200, replace = TRUE)),
outcome = factor(sample(c("Success", "Failure"), 200, replace = TRUE))
)
# Clinical research analysis
enhancedcrosstable(
data = emr_data,
vars = c("age_group", "comorbidity"),
by_var = "outcome",
test_auto = TRUE,
effect_size = TRUE,
show_interpretation = TRUE
)
Multi-Center Studies
# Stratified analysis by site
multi_center_analysis <- function(data, site_var) {
sites <- unique(data[[site_var]])
for (site in sites) {
cat("\n=== Analysis for", site, "===\n")
site_data <- data[data[[site_var]] == site, ]
result <- enhancedcrosstable(
data = site_data,
vars = c("treatment", "severity"),
by_var = "outcome",
test_auto = TRUE,
compact = TRUE
)
print(result)
}
}
Future Enhancements
Conclusion
The enhanced cross-table functionality represents a significant advancement in clinical research analysis capabilities. By integrating the sophisticated danchaltiel/crosstable package with ClinicoPath’s clinical focus, researchers gain access to:
- Professional Quality: Publication-ready outputs that meet journal standards
- Statistical Rigor: Automated test selection and comprehensive effect sizes
- Clinical Relevance: Features designed specifically for medical research
- Efficiency: Streamlined workflow from analysis to manuscript
- Reproducibility: Comprehensive documentation and parameter tracking
Key Benefits
- Enhanced Productivity: Automated statistical analysis reduces manual work
- Improved Quality: Professional formatting and statistical rigor
- Clinical Focus: Designed for medical research applications
- Flexibility: Multiple output formats and customization options
- Integration: Seamless workflow with ClinicoPath ecosystem
Recommendations
- Start Simple: Begin with basic cross-tabulations before exploring advanced features
- Validate Results: Compare with standard methods during initial implementation
- Document Parameters: Maintain detailed analysis logs for reproducibility
- Seek Training: Consider statistical consulting for complex research designs
- Stay Updated: Monitor package updates for new features and improvements
For optimal results in clinical research, combine the enhanced cross-table functionality with appropriate study design, adequate sample sizes, and careful interpretation of statistical results in clinical context.