Skip to contents

A comprehensive test dataset specifically designed for testing the chisqposttest function. Contains multiple categorical variables with known associations of different strengths, edge cases, and missing data patterns.

Usage

data("chisqposttest_test_data")

Format

A data frame with 300 observations and 14 variables:

PatientID

Patient identifier (1-300)

Treatment

Treatment group: "Standard", "Experimental"

Response

Treatment response: "No Response", "Response" (strongly associated with Treatment)

Sex

Patient sex: "Male", "Female" (balanced)

TumorGrade

Tumor grade: "Grade 1", "Grade 2", "Grade 3"

TumorStage

Tumor stage: "Stage I", "Stage II", "Stage III" (moderately associated with TumorGrade)

Institution

Hospital: "Hospital A", "Hospital B", "Hospital C", "Hospital D"

QualityScore

Quality rating: "High", "Low" (weakly associated with Institution)

RandomVar1

Random variable: "Group A", "Group B", "Group C" (no associations)

RandomVar2

Random variable: "Type X", "Type Y" (no associations)

RareCategory

Frequency category: "Common", "Uncommon", "Rare" (unbalanced)

BinaryOutcome

Binary outcome: "Negative", "Positive" (associated with RareCategory)

AgeGroup

Age category: "Young", "Middle", "Elderly"

BiomarkerStatus

Biomarker status: "Negative", "Positive" (moderately associated with AgeGroup)

Details

This dataset contains several types of associations designed to test different aspects of chi-square post-hoc analysis:

Strong Associations:

  • Treatment -> Response: Clear treatment effect with odds ratio ~5

Moderate Associations:

  • TumorGrade -> TumorStage: Higher grades associated with advanced stages

  • AgeGroup -> BiomarkerStatus: Age-related biomarker expression pattern

Weak Associations:

  • Institution -> QualityScore: Institutional quality differences

  • RareCategory -> BinaryOutcome: Effect in rare category with small cell counts

No Associations:

  • RandomVar1 ⊥ RandomVar2: Independent random variables for null hypothesis testing

The dataset includes approximately 5% missing data in Treatment, Sex, and TumorGrade variables to test missing data handling options.

Source

Simulated data created for testing purposes. Associations are based on realistic clinical scenarios but data is artificially generated.

See also

chisqposttest, histopathology

Examples

# Load the dataset
data(chisqposttest_test_data)

# Examine structure
str(chisqposttest_test_data)
#> 'data.frame':	300 obs. of  14 variables:
#>  $ PatientID      : int  1 2 3 4 5 6 7 8 9 10 ...
#>  $ Treatment      : Factor w/ 2 levels "Standard","Experimental": 1 1 2 1 1 2 1 2 1 1 ...
#>  $ Response       : Factor w/ 2 levels "No Response",..: 1 1 1 1 1 2 1 2 1 1 ...
#>  $ Sex            : Factor w/ 2 levels "Male","Female": 2 2 2 2 1 1 2 2 2 2 ...
#>  $ TumorGrade     : Factor w/ 3 levels "Grade 1","Grade 2",..: 3 2 1 2 3 3 1 2 NA 3 ...
#>  $ TumorStage     : Factor w/ 3 levels "Stage I","Stage II",..: 3 3 1 2 2 3 2 2 3 3 ...
#>  $ Institution    : Factor w/ 4 levels "Hospital A","Hospital B",..: 1 1 3 1 3 4 1 1 3 1 ...
#>  $ QualityScore   : Factor w/ 2 levels "High","Low": 1 2 2 1 1 1 1 1 2 1 ...
#>  $ RandomVar1     : Factor w/ 3 levels "Group A","Group B",..: 2 2 2 2 1 2 1 1 1 2 ...
#>  $ RandomVar2     : Factor w/ 2 levels "Type X","Type Y": 1 2 2 1 2 2 1 2 1 1 ...
#>  $ RareCategory   : Factor w/ 3 levels "Common","Uncommon",..: 2 2 1 1 1 1 2 1 1 1 ...
#>  $ BinaryOutcome  : Factor w/ 2 levels "Negative","Positive": 1 2 1 1 2 1 2 1 1 1 ...
#>  $ AgeGroup       : Factor w/ 3 levels "Young","Middle",..: 3 2 1 1 3 2 2 2 1 2 ...
#>  $ BiomarkerStatus: Factor w/ 2 levels "Negative","Positive": 2 1 2 1 2 2 2 1 1 1 ...
#>  - attr(*, "description")= chr "Test dataset for chisqposttest function with known associations"
#>  - attr(*, "associations")=List of 4
#>   ..$ strong  : chr "Treatment -> Response"
#>   ..$ moderate: chr [1:2] "TumorGrade -> TumorStage" "AgeGroup -> BiomarkerStatus"
#>   ..$ weak    : chr [1:2] "Institution -> QualityScore" "RareCategory -> BinaryOutcome"
#>   ..$ none    : chr "RandomVar1 ⊥ RandomVar2"
#>  - attr(*, "created")= Date[1:1], format: "2025-07-02"
#>  - attr(*, "sample_size")= num 300

# Example 1: Strong association (should be highly significant)
chisqposttest(
  data = chisqposttest_test_data,
  rows = "Treatment",
  cols = "Response",
  posthoc = "bonferroni"
)
#> Warning: Chi-squared approximation may be incorrect
#> Warning: Chi-squared approximation may be incorrect
#> Warning: Chi-squared approximation may be incorrect
#> 
#>  CHI-SQUARE POST-HOC TESTS
#> 
#> character(0)
#> 
#>  Chi-Square Test Results                        
#>  ────────────────────────────────────────────── 
#>    Statistic     Value       df    p-value      
#>  ────────────────────────────────────────────── 
#>    Chi-Square    66.63312     2    < .0000001   
#>  ────────────────────────────────────────────── 
#> 
#> 
#>  <div style="padding: 15px; background-color: #f8f9fa; border-left: 4px
#>  solid #1976d2; margin: 10px 0;">
#>  <h4 style="color: #1976d2; margin-top: 0;">Chi-Square Post-Hoc
#>  Analysis Guide
#> 
#>  Three-Step Comprehensive Analysis:
#> 
#> 
#> 
#>  1.
#>  Overall Chi-Square Test:
#>  Tests if there's any association between variables
#>  <br/>
#>  2.
#>  Residuals Analysis:
#>  Identifies which specific cells contribute to significance
#>  <br/>
#>  3.
#>  Pairwise Comparisons:
#>  Formal hypothesis testing between group pairs
#> 
#> 
#> 
#>  *Recommended approach: Start with residuals analysis for pattern
#>  identification, then use pairwise tests for formal hypothesis
#>  testing.*
#> 
#> 
#>  <table border='1' style='border-collapse: collapse;'>No
#>  ResponseResponseStandard8828Experimental49130NA23
#> 
#>  <div style='margin: 15px 0;'><div style="padding: 15px;
#>  background-color: #e8f5e8; border-left: 4px solid #4caf50; margin:
#>  10px 0;">
#>  <h4 style="color: #2e7d32; margin-top: 0;">Standardized Residuals
#>  Interpretation
#> 
#>  What are standardized residuals?
#> 
#> 
#>  Standardized residuals measure how much each cell deviates from what
#>  we'd expect if there was no association. They're calculated as
#>  (Observed - Expected) / √Expected, then adjusted for multiple
#>  comparisons.
#> 
#>  <div style="background-color: #f1f8e9; padding: 10px; border-radius:
#>  3px;">
#>  Interpretation Guidelines:
#>  <br/>
#>  • |Residual| > 2.0: Suggests meaningful deviation
#>  <br/>
#>  • |Residual| > 3.0: Strong evidence of deviation
#>  <br/>
#>  • Positive values: Over-represented (more than expected)
#>  <br/>
#>  • Negative values: Under-represented (fewer than expected)
#> 
#>  <table style='border-collapse: collapse; width: 100%; margin: 15px
#>  0;'><tr style='background-color: #e8f5e8;'><th style='border: 1px
#>  solid #666; padding: 8px;'><th style='border: 1px solid #666; padding:
#>  8px; text-align: center;'>No Response<th style='border: 1px solid
#>  #666; padding: 8px; text-align: center;'>Response<td style='border:
#>  1px solid #666; padding: 8px; font-weight: bold; background-color:
#>  #f5f5f5;'>Standard<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center; background-color: #ffebee; color: #c62828;
#>  font-weight: bold;'>8.144
#>  Over-represented<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center; background-color: #e3f2fd; color: #1565c0;
#>  font-weight: bold;'>-8.144
#>  Under-represented<td style='border: 1px solid #666; padding: 8px;
#>  font-weight: bold; background-color: #f5f5f5;'>Experimental<td
#>  style='border: 1px solid #666; padding: 8px; text-align: center;
#>  background-color: #e3f2fd; color: #1565c0; font-weight: bold;'>-8.01
#>  Under-represented<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center; background-color: #ffebee; color: #c62828;
#>  font-weight: bold;'>8.01
#>  Over-represented<td style='border: 1px solid #666; padding: 8px;
#>  font-weight: bold; background-color: #f5f5f5;'>NA<td style='border:
#>  1px solid #666; padding: 8px; text-align: center; background-color:
#>  #f9f9f9;'>-0.286
#>  As expected<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center; background-color: #f9f9f9;'>0.286
#>  As expected
#> 
#>  Critical value for significance: ±2.638
#> 
#>  <div style="padding: 15px; background-color: #fff3e0; border-left: 4px
#>  solid #ff9800; margin: 10px 0;">
#>  <h4 style="color: #e65100; margin-top: 0;">Multiple Testing Correction
#> 
#>  Why correction is needed:
#> 
#> 
#>  When performing multiple tests, the probability of finding at least
#>  one false positive increases. With
#>  3
#>  comparisons, there's a
#>  14.3
#>  % chance of a false positive without correction.
#> 
#> 
#>  <div style="background-color: #fce4ec; padding: 10px; border-radius:
#>  3px;">
#>  Correction Applied:
#>  <br/>
#>  • Number of comparisons:
#>  3
#>  <br/>
#>  • Adjusted significance level: α =
#>  0.0167
#>  <br/>
#>  • Use adjusted p-values for significance decisions
#> 
#> 
#>  Pairwise Comparison Results                                                                                                    
#>  ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
#>    Comparison                  Test Method       Chi-Square    p-value       Adj. p-value    Effect Size (Phi)    Significant   
#>  ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
#>    Standard vs Experimental    Chi-square        66.5303419    < .0000001    < .0000001           0.47500000    Yes           
#>    Standard vs NA              Fisher's exact     3.2350414     0.1056590       0.2113181           0.16400000    No            
#>    Experimental vs NA          Fisher's exact     0.3870168     0.6181533       1.0000000           0.04600000    No            
#>  ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
#> 
#> 
#>  <div style='margin: 20px 0;'><h3 style='color: #1976d2;'>Detailed
#>  Pairwise Comparison Tables
#> 
#>  <div style='margin: 20px 0; padding: 15px; border: 1px solid #ddd;
#>  border-radius: 5px;'><h4 style='margin-top: 0; color:
#>  #1976d2;'>Standard vs Experimental
#> 
#>  <div style='margin-bottom: 15px; padding: 10px; background-color:
#>  #f5f5f5; border-radius: 3px;'>Test Results:
#>  Method: Chi-square test (standard approach)
#>  Chi-square = 66.53, p = 3.45e-16, Adjusted p = 1.03e-15
#>  Effect size (Phi) = 0.475
#>  <span style='color: #d32f2f; font-weight: bold;'>✓ Significant<table
#>  style='border-collapse: collapse; width: 100%; margin: 10px 0;'><tr
#>  style='background-color: #e3f2fd;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'>No Response<th style='border: 1px
#>  solid #666; padding: 8px; text-align: center;'>Response<th
#>  style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>Total<td style='border: 1px solid #666; padding: 8px;
#>  background-color: #e3f2fd; font-weight: bold;'>Standard<td
#>  style='border: 1px solid #666; padding: 8px; text-align: center;'>88
#>  (75.9%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>28
#>  (24.1%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>116<td style='border: 1px solid #666;
#>  padding: 8px; background-color: #e3f2fd; font-weight:
#>  bold;'>Experimental<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center;'>49
#>  (27.4%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>130
#>  (72.6%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>179<tr style='background-color: #f5f5f5;
#>  font-weight: bold;'><td style='border: 1px solid #666; padding:
#>  8px;'>Total<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center;'>137
#>  (46.4%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>158
#>  (53.6%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>295<div style='margin: 20px 0; padding: 15px; border: 1px
#>  solid #ddd; border-radius: 5px;'><h4 style='margin-top: 0; color:
#>  #1976d2;'>Standard vs NA
#> 
#>  <div style='margin-bottom: 15px; padding: 10px; background-color:
#>  #f5f5f5; border-radius: 3px;'>Test Results:
#>  Method: Fisher's exact test (used due to low expected frequencies)
#>  Chi-square = 3.235, p = 0.106, Adjusted p = 0.211
#>  Effect size (Phi) = 0.164
#>  <span style='color: #666;'>Not significant<table
#>  style='border-collapse: collapse; width: 100%; margin: 10px 0;'><tr
#>  style='background-color: #e3f2fd;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'>No Response<th style='border: 1px
#>  solid #666; padding: 8px; text-align: center;'>Response<th
#>  style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>Total<td style='border: 1px solid #666; padding: 8px;
#>  background-color: #e3f2fd; font-weight: bold;'>Standard<td
#>  style='border: 1px solid #666; padding: 8px; text-align: center;'>88
#>  (75.9%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>28
#>  (24.1%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>116<td style='border: 1px solid #666;
#>  padding: 8px; background-color: #e3f2fd; font-weight: bold;'>NA<td
#>  style='border: 1px solid #666; padding: 8px; text-align: center;'>2
#>  (40%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>3
#>  (60%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>5<tr style='background-color: #f5f5f5;
#>  font-weight: bold;'><td style='border: 1px solid #666; padding:
#>  8px;'>Total<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center;'>90
#>  (74.4%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>31
#>  (25.6%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>121<div style='margin: 20px 0; padding: 15px; border: 1px
#>  solid #ddd; border-radius: 5px;'><h4 style='margin-top: 0; color:
#>  #1976d2;'>Experimental vs NA
#> 
#>  <div style='margin-bottom: 15px; padding: 10px; background-color:
#>  #f5f5f5; border-radius: 3px;'>Test Results:
#>  Method: Fisher's exact test (used due to low expected frequencies)
#>  Chi-square = 0.387, p = 0.618, Adjusted p = 1
#>  Effect size (Phi) = 0.046
#>  <span style='color: #666;'>Not significant<table
#>  style='border-collapse: collapse; width: 100%; margin: 10px 0;'><tr
#>  style='background-color: #e3f2fd;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'>No Response<th style='border: 1px
#>  solid #666; padding: 8px; text-align: center;'>Response<th
#>  style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>Total<td style='border: 1px solid #666; padding: 8px;
#>  background-color: #e3f2fd; font-weight: bold;'>Experimental<td
#>  style='border: 1px solid #666; padding: 8px; text-align: center;'>49
#>  (27.4%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>130
#>  (72.6%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>179<td style='border: 1px solid #666;
#>  padding: 8px; background-color: #e3f2fd; font-weight: bold;'>NA<td
#>  style='border: 1px solid #666; padding: 8px; text-align: center;'>2
#>  (40%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>3
#>  (60%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>5<tr style='background-color: #f5f5f5;
#>  font-weight: bold;'><td style='border: 1px solid #666; padding:
#>  8px;'>Total<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center;'>51
#>  (27.7%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>133
#>  (72.3%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>184

# Example 2: Moderate association (should be significant with post-hoc differences)
chisqposttest(
  data = chisqposttest_test_data,
  rows = "TumorGrade", 
  cols = "TumorStage",
  posthoc = "fdr"
)
#> Warning: Chi-squared approximation may be incorrect
#> Warning: Chi-squared approximation may be incorrect
#> Warning: Chi-squared approximation may be incorrect
#> Warning: Chi-squared approximation may be incorrect
#> 
#>  CHI-SQUARE POST-HOC TESTS
#> 
#> character(0)
#> 
#>  Chi-Square Test Results                        
#>  ────────────────────────────────────────────── 
#>    Statistic     Value       df    p-value      
#>  ────────────────────────────────────────────── 
#>    Chi-Square    109.4691     6    < .0000001   
#>  ────────────────────────────────────────────── 
#> 
#> 
#>  <div style="padding: 15px; background-color: #f8f9fa; border-left: 4px
#>  solid #1976d2; margin: 10px 0;">
#>  <h4 style="color: #1976d2; margin-top: 0;">Chi-Square Post-Hoc
#>  Analysis Guide
#> 
#>  Three-Step Comprehensive Analysis:
#> 
#> 
#> 
#>  1.
#>  Overall Chi-Square Test:
#>  Tests if there's any association between variables
#>  <br/>
#>  2.
#>  Residuals Analysis:
#>  Identifies which specific cells contribute to significance
#>  <br/>
#>  3.
#>  Pairwise Comparisons:
#>  Formal hypothesis testing between group pairs
#> 
#> 
#> 
#>  *Recommended approach: Start with residuals analysis for pattern
#>  identification, then use pairwise tests for formal hypothesis
#>  testing.*
#> 
#> 
#>  <table border='1' style='border-collapse: collapse;'>Stage IStage
#>  IIStage IIIGrade 156247Grade 2287122Grade 363249NA032
#> 
#>  <div style='margin: 15px 0;'><div style="padding: 15px;
#>  background-color: #e8f5e8; border-left: 4px solid #4caf50; margin:
#>  10px 0;">
#>  <h4 style="color: #2e7d32; margin-top: 0;">Standardized Residuals
#>  Interpretation
#> 
#>  What are standardized residuals?
#> 
#> 
#>  Standardized residuals measure how much each cell deviates from what
#>  we'd expect if there was no association. They're calculated as
#>  (Observed - Expected) / √Expected, then adjusted for multiple
#>  comparisons.
#> 
#>  <div style="background-color: #f1f8e9; padding: 10px; border-radius:
#>  3px;">
#>  Interpretation Guidelines:
#>  <br/>
#>  • |Residual| > 2.0: Suggests meaningful deviation
#>  <br/>
#>  • |Residual| > 3.0: Strong evidence of deviation
#>  <br/>
#>  • Positive values: Over-represented (more than expected)
#>  <br/>
#>  • Negative values: Under-represented (fewer than expected)
#> 
#>  <table style='border-collapse: collapse; width: 100%; margin: 15px
#>  0;'><tr style='background-color: #e8f5e8;'><th style='border: 1px
#>  solid #666; padding: 8px;'><th style='border: 1px solid #666; padding:
#>  8px; text-align: center;'>Stage I<th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'>Stage II<th style='border: 1px
#>  solid #666; padding: 8px; text-align: center;'>Stage III<td
#>  style='border: 1px solid #666; padding: 8px; font-weight: bold;
#>  background-color: #f5f5f5;'>Grade 1<td style='border: 1px solid #666;
#>  padding: 8px; text-align: center; background-color: #ffebee; color:
#>  #c62828; font-weight: bold;'>8.302
#>  Over-represented<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center; background-color: #e3f2fd; color: #1565c0;
#>  font-weight: bold;'>-3.518
#>  Under-represented<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center; background-color: #e3f2fd; color: #1565c0;
#>  font-weight: bold;'>-4.661
#>  Under-represented<td style='border: 1px solid #666; padding: 8px;
#>  font-weight: bold; background-color: #f5f5f5;'>Grade 2<td
#>  style='border: 1px solid #666; padding: 8px; text-align: center;
#>  background-color: #f9f9f9;'>-2.132
#>  As expected<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center; background-color: #ffebee; color: #c62828;
#>  font-weight: bold;'>4.41
#>  Over-represented<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center; background-color: #f9f9f9;'>-2.732
#>  As expected<td style='border: 1px solid #666; padding: 8px;
#>  font-weight: bold; background-color: #f5f5f5;'>Grade 3<td
#>  style='border: 1px solid #666; padding: 8px; text-align: center;
#>  background-color: #e3f2fd; color: #1565c0; font-weight: bold;'>-5.581
#>  Under-represented<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center; background-color: #f9f9f9;'>-1.464
#>  As expected<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center; background-color: #ffebee; color: #c62828;
#>  font-weight: bold;'>7.423
#>  Over-represented<td style='border: 1px solid #666; padding: 8px;
#>  font-weight: bold; background-color: #f5f5f5;'>NA<td style='border:
#>  1px solid #666; padding: 8px; text-align: center; background-color:
#>  #f9f9f9;'>-1.476
#>  As expected<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center; background-color: #f9f9f9;'>0.758
#>  As expected<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center; background-color: #f9f9f9;'>0.68
#>  As expected
#> 
#>  Critical value for significance: ±2.865
#> 
#>  <div style="padding: 15px; background-color: #fff3e0; border-left: 4px
#>  solid #ff9800; margin: 10px 0;">
#>  <h4 style="color: #e65100; margin-top: 0;">Multiple Testing Correction
#> 
#>  Why correction is needed:
#> 
#> 
#>  When performing multiple tests, the probability of finding at least
#>  one false positive increases. With
#>  6
#>  comparisons, there's a
#>  26.5
#>  % chance of a false positive without correction.
#> 
#> 
#>  <div style="background-color: #fce4ec; padding: 10px; border-radius:
#>  3px;">
#>  Correction Applied:
#>  <br/>
#>  • Number of comparisons:
#>  6
#>  <br/>
#>  • Adjusted significance level: α =
#>  0.0083
#>  <br/>
#>  • Use adjusted p-values for significance decisions
#> 
#> 
#>  Pairwise Comparison Results                                                                                              
#>  ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
#>    Comparison            Test Method       Chi-Square    p-value       Adj. p-value    Effect Size (Phi)    Significant   
#>  ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
#>    Grade 1 vs Grade 2    Chi-square         35.741905    < .0000001    < .0000001            0.4150000    Yes           
#>    Grade 1 vs Grade 3    Chi-square         72.965438    < .0000001    < .0000001            0.6480000    Yes           
#>    Grade 1 vs NA         Fisher's exact      9.846232     0.0045976       0.0137929            0.3270000    Yes           
#>    Grade 2 vs Grade 3    Chi-square         34.637706    < .0000001    < .0000001            0.4080000    Yes           
#>    Grade 2 vs NA         Fisher's exact      2.358454     0.2905058       0.4357587            0.1370000    No            
#>    Grade 3 vs NA         Fisher's exact      1.242070     0.5701787       0.5701787            0.1160000    No            
#>  ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
#> 
#> 
#>  <div style='margin: 20px 0;'><h3 style='color: #1976d2;'>Detailed
#>  Pairwise Comparison Tables
#> 
#>  <div style='margin: 20px 0; padding: 15px; border: 1px solid #ddd;
#>  border-radius: 5px;'><h4 style='margin-top: 0; color: #1976d2;'>Grade
#>  1 vs Grade 2
#> 
#>  <div style='margin-bottom: 15px; padding: 10px; background-color:
#>  #f5f5f5; border-radius: 3px;'>Test Results:
#>  Method: Chi-square test (standard approach)
#>  Chi-square = 35.742, p = 1.73e-08, Adjusted p = 5.2e-08
#>  Effect size (Phi) = 0.415
#>  <span style='color: #d32f2f; font-weight: bold;'>✓ Significant<table
#>  style='border-collapse: collapse; width: 100%; margin: 10px 0;'><tr
#>  style='background-color: #e3f2fd;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'>Stage I<th style='border: 1px solid
#>  #666; padding: 8px; text-align: center;'>Stage II<th style='border:
#>  1px solid #666; padding: 8px; text-align: center;'>Total<td
#>  style='border: 1px solid #666; padding: 8px; background-color:
#>  #e3f2fd; font-weight: bold;'>Grade 1<td style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'>56
#>  (64.4%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>24
#>  (27.6%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>87<td style='border: 1px solid #666;
#>  padding: 8px; background-color: #e3f2fd; font-weight: bold;'>Grade
#>  2<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>28
#>  (23.1%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>71
#>  (58.7%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>121<tr style='background-color: #f5f5f5;
#>  font-weight: bold;'><td style='border: 1px solid #666; padding:
#>  8px;'>Total<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center;'>84
#>  (40.4%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>95
#>  (45.7%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>208<div style='margin: 20px 0; padding: 15px; border: 1px
#>  solid #ddd; border-radius: 5px;'><h4 style='margin-top: 0; color:
#>  #1976d2;'>Grade 1 vs Grade 3
#> 
#>  <div style='margin-bottom: 15px; padding: 10px; background-color:
#>  #f5f5f5; border-radius: 3px;'>Test Results:
#>  Method: Chi-square test (standard approach)
#>  Chi-square = 72.965, p = <2e-16, Adjusted p = 8.59e-16
#>  Effect size (Phi) = 0.648
#>  <span style='color: #d32f2f; font-weight: bold;'>✓ Significant<table
#>  style='border-collapse: collapse; width: 100%; margin: 10px 0;'><tr
#>  style='background-color: #e3f2fd;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'>Stage I<th style='border: 1px solid
#>  #666; padding: 8px; text-align: center;'>Stage II<th style='border:
#>  1px solid #666; padding: 8px; text-align: center;'>Total<td
#>  style='border: 1px solid #666; padding: 8px; background-color:
#>  #e3f2fd; font-weight: bold;'>Grade 1<td style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'>56
#>  (64.4%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>24
#>  (27.6%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>87<td style='border: 1px solid #666;
#>  padding: 8px; background-color: #e3f2fd; font-weight: bold;'>Grade
#>  3<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>6
#>  (6.9%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>32
#>  (36.8%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>87<tr style='background-color: #f5f5f5;
#>  font-weight: bold;'><td style='border: 1px solid #666; padding:
#>  8px;'>Total<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center;'>62
#>  (35.6%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>56
#>  (32.2%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>174<div style='margin: 20px 0; padding: 15px; border: 1px
#>  solid #ddd; border-radius: 5px;'><h4 style='margin-top: 0; color:
#>  #1976d2;'>Grade 1 vs NA
#> 
#>  <div style='margin-bottom: 15px; padding: 10px; background-color:
#>  #f5f5f5; border-radius: 3px;'>Test Results:
#>  Method: Fisher's exact test (used due to low expected frequencies)
#>  Chi-square = 9.846, p = 0.0046, Adjusted p = 0.0138
#>  Effect size (Phi) = 0.327
#>  <span style='color: #d32f2f; font-weight: bold;'>✓ Significant<table
#>  style='border-collapse: collapse; width: 100%; margin: 10px 0;'><tr
#>  style='background-color: #e3f2fd;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'>Stage I<th style='border: 1px solid
#>  #666; padding: 8px; text-align: center;'>Stage II<th style='border:
#>  1px solid #666; padding: 8px; text-align: center;'>Total<td
#>  style='border: 1px solid #666; padding: 8px; background-color:
#>  #e3f2fd; font-weight: bold;'>Grade 1<td style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'>56
#>  (64.4%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>24
#>  (27.6%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>87<td style='border: 1px solid #666;
#>  padding: 8px; background-color: #e3f2fd; font-weight: bold;'>NA<td
#>  style='border: 1px solid #666; padding: 8px; text-align: center;'>0
#>  (0%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>3
#>  (60%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>5<tr style='background-color: #f5f5f5;
#>  font-weight: bold;'><td style='border: 1px solid #666; padding:
#>  8px;'>Total<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center;'>56
#>  (60.9%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>27
#>  (29.3%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>92<div style='margin: 20px 0; padding: 15px; border: 1px
#>  solid #ddd; border-radius: 5px;'><h4 style='margin-top: 0; color:
#>  #1976d2;'>Grade 2 vs Grade 3
#> 
#>  <div style='margin-bottom: 15px; padding: 10px; background-color:
#>  #f5f5f5; border-radius: 3px;'>Test Results:
#>  Method: Chi-square test (standard approach)
#>  Chi-square = 34.638, p = 3.01e-08, Adjusted p = 6.02e-08
#>  Effect size (Phi) = 0.408
#>  <span style='color: #d32f2f; font-weight: bold;'>✓ Significant<table
#>  style='border-collapse: collapse; width: 100%; margin: 10px 0;'><tr
#>  style='background-color: #e3f2fd;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'>Stage I<th style='border: 1px solid
#>  #666; padding: 8px; text-align: center;'>Stage II<th style='border:
#>  1px solid #666; padding: 8px; text-align: center;'>Total<td
#>  style='border: 1px solid #666; padding: 8px; background-color:
#>  #e3f2fd; font-weight: bold;'>Grade 2<td style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'>28
#>  (23.1%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>71
#>  (58.7%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>121<td style='border: 1px solid #666;
#>  padding: 8px; background-color: #e3f2fd; font-weight: bold;'>Grade
#>  3<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>6
#>  (6.9%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>32
#>  (36.8%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>87<tr style='background-color: #f5f5f5;
#>  font-weight: bold;'><td style='border: 1px solid #666; padding:
#>  8px;'>Total<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center;'>34
#>  (16.3%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>103
#>  (49.5%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>208<div style='margin: 20px 0; padding: 15px; border: 1px
#>  solid #ddd; border-radius: 5px;'><h4 style='margin-top: 0; color:
#>  #1976d2;'>Grade 2 vs NA
#> 
#>  <div style='margin-bottom: 15px; padding: 10px; background-color:
#>  #f5f5f5; border-radius: 3px;'>Test Results:
#>  Method: Fisher's exact test (used due to low expected frequencies)
#>  Chi-square = 2.358, p = 0.291, Adjusted p = 0.436
#>  Effect size (Phi) = 0.137
#>  <span style='color: #666;'>Not significant<table
#>  style='border-collapse: collapse; width: 100%; margin: 10px 0;'><tr
#>  style='background-color: #e3f2fd;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'>Stage I<th style='border: 1px solid
#>  #666; padding: 8px; text-align: center;'>Stage II<th style='border:
#>  1px solid #666; padding: 8px; text-align: center;'>Total<td
#>  style='border: 1px solid #666; padding: 8px; background-color:
#>  #e3f2fd; font-weight: bold;'>Grade 2<td style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'>28
#>  (23.1%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>71
#>  (58.7%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>121<td style='border: 1px solid #666;
#>  padding: 8px; background-color: #e3f2fd; font-weight: bold;'>NA<td
#>  style='border: 1px solid #666; padding: 8px; text-align: center;'>0
#>  (0%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>3
#>  (60%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>5<tr style='background-color: #f5f5f5;
#>  font-weight: bold;'><td style='border: 1px solid #666; padding:
#>  8px;'>Total<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center;'>28
#>  (22.2%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>74
#>  (58.7%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>126<div style='margin: 20px 0; padding: 15px; border: 1px
#>  solid #ddd; border-radius: 5px;'><h4 style='margin-top: 0; color:
#>  #1976d2;'>Grade 3 vs NA
#> 
#>  <div style='margin-bottom: 15px; padding: 10px; background-color:
#>  #f5f5f5; border-radius: 3px;'>Test Results:
#>  Method: Fisher's exact test (used due to low expected frequencies)
#>  Chi-square = 1.242, p = 0.57, Adjusted p = 0.57
#>  Effect size (Phi) = 0.116
#>  <span style='color: #666;'>Not significant<table
#>  style='border-collapse: collapse; width: 100%; margin: 10px 0;'><tr
#>  style='background-color: #e3f2fd;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'>Stage I<th style='border: 1px solid
#>  #666; padding: 8px; text-align: center;'>Stage II<th style='border:
#>  1px solid #666; padding: 8px; text-align: center;'>Total<td
#>  style='border: 1px solid #666; padding: 8px; background-color:
#>  #e3f2fd; font-weight: bold;'>Grade 3<td style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'>6
#>  (6.9%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>32
#>  (36.8%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>87<td style='border: 1px solid #666;
#>  padding: 8px; background-color: #e3f2fd; font-weight: bold;'>NA<td
#>  style='border: 1px solid #666; padding: 8px; text-align: center;'>0
#>  (0%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>3
#>  (60%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>5<tr style='background-color: #f5f5f5;
#>  font-weight: bold;'><td style='border: 1px solid #666; padding:
#>  8px;'>Total<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center;'>6
#>  (6.5%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>35
#>  (38%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>92

# Example 3: No association (should be non-significant)
chisqposttest(
  data = chisqposttest_test_data,
  rows = "RandomVar1",
  cols = "RandomVar2",
  posthoc = "bonferroni"
)
#> 
#>  CHI-SQUARE POST-HOC TESTS
#> 
#> character(0)
#> 
#>  Chi-Square Test Results                       
#>  ───────────────────────────────────────────── 
#>    Statistic     Value       df    p-value     
#>  ───────────────────────────────────────────── 
#>    Chi-Square    6.359561     2    0.0415948   
#>  ───────────────────────────────────────────── 
#> 
#> 
#>  <div style="padding: 15px; background-color: #f8f9fa; border-left: 4px
#>  solid #1976d2; margin: 10px 0;">
#>  <h4 style="color: #1976d2; margin-top: 0;">Chi-Square Post-Hoc
#>  Analysis Guide
#> 
#>  Three-Step Comprehensive Analysis:
#> 
#> 
#> 
#>  1.
#>  Overall Chi-Square Test:
#>  Tests if there's any association between variables
#>  <br/>
#>  2.
#>  Residuals Analysis:
#>  Identifies which specific cells contribute to significance
#>  <br/>
#>  3.
#>  Pairwise Comparisons:
#>  Formal hypothesis testing between group pairs
#> 
#> 
#> 
#>  *Recommended approach: Start with residuals analysis for pattern
#>  identification, then use pairwise tests for formal hypothesis
#>  testing.*
#> 
#> 
#>  <table border='1' style='border-collapse: collapse;'>Type XType YGroup
#>  A4749Group B5157Group C6135
#> 
#>  <div style='margin: 15px 0;'><div style="padding: 15px;
#>  background-color: #e8f5e8; border-left: 4px solid #4caf50; margin:
#>  10px 0;">
#>  <h4 style="color: #2e7d32; margin-top: 0;">Standardized Residuals
#>  Interpretation
#> 
#>  What are standardized residuals?
#> 
#> 
#>  Standardized residuals measure how much each cell deviates from what
#>  we'd expect if there was no association. They're calculated as
#>  (Observed - Expected) / √Expected, then adjusted for multiple
#>  comparisons.
#> 
#>  <div style="background-color: #f1f8e9; padding: 10px; border-radius:
#>  3px;">
#>  Interpretation Guidelines:
#>  <br/>
#>  • |Residual| > 2.0: Suggests meaningful deviation
#>  <br/>
#>  • |Residual| > 3.0: Strong evidence of deviation
#>  <br/>
#>  • Positive values: Over-represented (more than expected)
#>  <br/>
#>  • Negative values: Under-represented (fewer than expected)
#> 
#>  <table style='border-collapse: collapse; width: 100%; margin: 15px
#>  0;'><tr style='background-color: #e8f5e8;'><th style='border: 1px
#>  solid #666; padding: 8px;'><th style='border: 1px solid #666; padding:
#>  8px; text-align: center;'>Type X<th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'>Type Y<td style='border: 1px solid
#>  #666; padding: 8px; font-weight: bold; background-color:
#>  #f5f5f5;'>Group A<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center; background-color: #f9f9f9;'>-0.962
#>  As expected<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center; background-color: #f9f9f9;'>0.962
#>  As expected<td style='border: 1px solid #666; padding: 8px;
#>  font-weight: bold; background-color: #f5f5f5;'>Group B<td
#>  style='border: 1px solid #666; padding: 8px; text-align: center;
#>  background-color: #f9f9f9;'>-1.504
#>  As expected<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center; background-color: #f9f9f9;'>1.504
#>  As expected<td style='border: 1px solid #666; padding: 8px;
#>  font-weight: bold; background-color: #f5f5f5;'>Group C<td
#>  style='border: 1px solid #666; padding: 8px; text-align: center;
#>  background-color: #f9f9f9;'>2.51
#>  As expected<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center; background-color: #f9f9f9;'>-2.51
#>  As expected
#> 
#>  Critical value for significance: ±2.638
#> 
#>  <div style="padding: 15px; background-color: #fff3e0; border-left: 4px
#>  solid #ff9800; margin: 10px 0;">
#>  <h4 style="color: #e65100; margin-top: 0;">Multiple Testing Correction
#> 
#>  Why correction is needed:
#> 
#> 
#>  When performing multiple tests, the probability of finding at least
#>  one false positive increases. With
#>  3
#>  comparisons, there's a
#>  14.3
#>  % chance of a false positive without correction.
#> 
#> 
#>  <div style="background-color: #fce4ec; padding: 10px; border-radius:
#>  3px;">
#>  Correction Applied:
#>  <br/>
#>  • Number of comparisons:
#>  3
#>  <br/>
#>  • Adjusted significance level: α =
#>  0.0167
#>  <br/>
#>  • Use adjusted p-values for significance decisions
#> 
#> 
#>  Pairwise Comparison Results                                                                                          
#>  ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
#>    Comparison            Test Method    Chi-Square    p-value      Adj. p-value    Effect Size (Phi)    Significant   
#>  ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
#>    Group A vs Group B    Chi-square     0.06136889    0.8043453       1.0000000           0.01700000    No            
#>    Group A vs Group C    Chi-square     4.14814815    0.0416801       0.1250403           0.14700000    No            
#>    Group B vs Group C    Chi-square     5.46676048    0.0193815       0.0581446           0.16400000    No            
#>  ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
#> 
#> 
#>  <div style='margin: 20px 0;'><h3 style='color: #1976d2;'>Detailed
#>  Pairwise Comparison Tables
#> 
#>  <div style='margin: 20px 0; padding: 15px; border: 1px solid #ddd;
#>  border-radius: 5px;'><h4 style='margin-top: 0; color: #1976d2;'>Group
#>  A vs Group B
#> 
#>  <div style='margin-bottom: 15px; padding: 10px; background-color:
#>  #f5f5f5; border-radius: 3px;'>Test Results:
#>  Method: Chi-square test (standard approach)
#>  Chi-square = 0.061, p = 0.804, Adjusted p = 1
#>  Effect size (Phi) = 0.017
#>  <span style='color: #666;'>Not significant<table
#>  style='border-collapse: collapse; width: 100%; margin: 10px 0;'><tr
#>  style='background-color: #e3f2fd;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'>Type X<th style='border: 1px solid
#>  #666; padding: 8px; text-align: center;'>Type Y<th style='border: 1px
#>  solid #666; padding: 8px; text-align: center;'>Total<td style='border:
#>  1px solid #666; padding: 8px; background-color: #e3f2fd; font-weight:
#>  bold;'>Group A<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center;'>47
#>  (49%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>49
#>  (51%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>96<td style='border: 1px solid #666;
#>  padding: 8px; background-color: #e3f2fd; font-weight: bold;'>Group
#>  B<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>51
#>  (47.2%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>57
#>  (52.8%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>108<tr style='background-color: #f5f5f5;
#>  font-weight: bold;'><td style='border: 1px solid #666; padding:
#>  8px;'>Total<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center;'>98
#>  (48%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>106
#>  (52%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>204<div style='margin: 20px 0; padding: 15px; border: 1px
#>  solid #ddd; border-radius: 5px;'><h4 style='margin-top: 0; color:
#>  #1976d2;'>Group A vs Group C
#> 
#>  <div style='margin-bottom: 15px; padding: 10px; background-color:
#>  #f5f5f5; border-radius: 3px;'>Test Results:
#>  Method: Chi-square test (standard approach)
#>  Chi-square = 4.148, p = 0.0417, Adjusted p = 0.125
#>  Effect size (Phi) = 0.147
#>  <span style='color: #666;'>Not significant<table
#>  style='border-collapse: collapse; width: 100%; margin: 10px 0;'><tr
#>  style='background-color: #e3f2fd;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'>Type X<th style='border: 1px solid
#>  #666; padding: 8px; text-align: center;'>Type Y<th style='border: 1px
#>  solid #666; padding: 8px; text-align: center;'>Total<td style='border:
#>  1px solid #666; padding: 8px; background-color: #e3f2fd; font-weight:
#>  bold;'>Group A<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center;'>47
#>  (49%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>49
#>  (51%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>96<td style='border: 1px solid #666;
#>  padding: 8px; background-color: #e3f2fd; font-weight: bold;'>Group
#>  C<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>61
#>  (63.5%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>35
#>  (36.5%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>96<tr style='background-color: #f5f5f5;
#>  font-weight: bold;'><td style='border: 1px solid #666; padding:
#>  8px;'>Total<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center;'>108
#>  (56.2%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>84
#>  (43.8%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>192<div style='margin: 20px 0; padding: 15px; border: 1px
#>  solid #ddd; border-radius: 5px;'><h4 style='margin-top: 0; color:
#>  #1976d2;'>Group B vs Group C
#> 
#>  <div style='margin-bottom: 15px; padding: 10px; background-color:
#>  #f5f5f5; border-radius: 3px;'>Test Results:
#>  Method: Chi-square test (standard approach)
#>  Chi-square = 5.467, p = 0.0194, Adjusted p = 0.0581
#>  Effect size (Phi) = 0.164
#>  <span style='color: #666;'>Not significant<table
#>  style='border-collapse: collapse; width: 100%; margin: 10px 0;'><tr
#>  style='background-color: #e3f2fd;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'>Type X<th style='border: 1px solid
#>  #666; padding: 8px; text-align: center;'>Type Y<th style='border: 1px
#>  solid #666; padding: 8px; text-align: center;'>Total<td style='border:
#>  1px solid #666; padding: 8px; background-color: #e3f2fd; font-weight:
#>  bold;'>Group B<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center;'>51
#>  (47.2%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>57
#>  (52.8%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>108<td style='border: 1px solid #666;
#>  padding: 8px; background-color: #e3f2fd; font-weight: bold;'>Group
#>  C<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>61
#>  (63.5%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>35
#>  (36.5%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>96<tr style='background-color: #f5f5f5;
#>  font-weight: bold;'><td style='border: 1px solid #666; padding:
#>  8px;'>Total<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center;'>112
#>  (54.9%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>92
#>  (45.1%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>204

# Example 4: Edge case with rare categories
chisqposttest(
  data = chisqposttest_test_data,
  rows = "RareCategory",
  cols = "BinaryOutcome",
  posthoc = "fdr"
)
#> 
#>  CHI-SQUARE POST-HOC TESTS
#> 
#> character(0)
#> 
#>  Chi-Square Test Results                       
#>  ───────────────────────────────────────────── 
#>    Statistic     Value       df    p-value     
#>  ───────────────────────────────────────────── 
#>    Chi-Square    24.74713     2    0.0000042   
#>  ───────────────────────────────────────────── 
#> 
#> 
#>  <div style="padding: 15px; background-color: #f8f9fa; border-left: 4px
#>  solid #1976d2; margin: 10px 0;">
#>  <h4 style="color: #1976d2; margin-top: 0;">Chi-Square Post-Hoc
#>  Analysis Guide
#> 
#>  Three-Step Comprehensive Analysis:
#> 
#> 
#> 
#>  1.
#>  Overall Chi-Square Test:
#>  Tests if there's any association between variables
#>  <br/>
#>  2.
#>  Residuals Analysis:
#>  Identifies which specific cells contribute to significance
#>  <br/>
#>  3.
#>  Pairwise Comparisons:
#>  Formal hypothesis testing between group pairs
#> 
#> 
#> 
#>  *Recommended approach: Start with residuals analysis for pattern
#>  identification, then use pairwise tests for formal hypothesis
#>  testing.*
#> 
#> 
#>  <table border='1' style='border-collapse:
#>  collapse;'>NegativePositiveCommon14456Uncommon5328Rare316
#> 
#>  <div style='margin: 15px 0;'><div style="padding: 15px;
#>  background-color: #e8f5e8; border-left: 4px solid #4caf50; margin:
#>  10px 0;">
#>  <h4 style="color: #2e7d32; margin-top: 0;">Standardized Residuals
#>  Interpretation
#> 
#>  What are standardized residuals?
#> 
#> 
#>  Standardized residuals measure how much each cell deviates from what
#>  we'd expect if there was no association. They're calculated as
#>  (Observed - Expected) / √Expected, then adjusted for multiple
#>  comparisons.
#> 
#>  <div style="background-color: #f1f8e9; padding: 10px; border-radius:
#>  3px;">
#>  Interpretation Guidelines:
#>  <br/>
#>  • |Residual| > 2.0: Suggests meaningful deviation
#>  <br/>
#>  • |Residual| > 3.0: Strong evidence of deviation
#>  <br/>
#>  • Positive values: Over-represented (more than expected)
#>  <br/>
#>  • Negative values: Under-represented (fewer than expected)
#> 
#>  <table style='border-collapse: collapse; width: 100%; margin: 15px
#>  0;'><tr style='background-color: #e8f5e8;'><th style='border: 1px
#>  solid #666; padding: 8px;'><th style='border: 1px solid #666; padding:
#>  8px; text-align: center;'>Negative<th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'>Positive<td style='border: 1px
#>  solid #666; padding: 8px; font-weight: bold; background-color:
#>  #f5f5f5;'>Common<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center; background-color: #ffebee; color: #c62828;
#>  font-weight: bold;'>2.771
#>  Over-represented<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center; background-color: #e3f2fd; color: #1565c0;
#>  font-weight: bold;'>-2.771
#>  Under-represented<td style='border: 1px solid #666; padding: 8px;
#>  font-weight: bold; background-color: #f5f5f5;'>Uncommon<td
#>  style='border: 1px solid #666; padding: 8px; text-align: center;
#>  background-color: #f9f9f9;'>-0.276
#>  As expected<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center; background-color: #f9f9f9;'>0.276
#>  As expected<td style='border: 1px solid #666; padding: 8px;
#>  font-weight: bold; background-color: #f5f5f5;'>Rare<td style='border:
#>  1px solid #666; padding: 8px; text-align: center; background-color:
#>  #e3f2fd; color: #1565c0; font-weight: bold;'>-4.861
#>  Under-represented<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center; background-color: #ffebee; color: #c62828;
#>  font-weight: bold;'>4.861
#>  Over-represented
#> 
#>  Critical value for significance: ±2.638
#> 
#>  <div style="padding: 15px; background-color: #fff3e0; border-left: 4px
#>  solid #ff9800; margin: 10px 0;">
#>  <h4 style="color: #e65100; margin-top: 0;">Multiple Testing Correction
#> 
#>  Why correction is needed:
#> 
#> 
#>  When performing multiple tests, the probability of finding at least
#>  one false positive increases. With
#>  3
#>  comparisons, there's a
#>  14.3
#>  % chance of a false positive without correction.
#> 
#> 
#>  <div style="background-color: #fce4ec; padding: 10px; border-radius:
#>  3px;">
#>  Correction Applied:
#>  <br/>
#>  • Number of comparisons:
#>  3
#>  <br/>
#>  • Adjusted significance level: α =
#>  0.0167
#>  <br/>
#>  • Use adjusted p-values for significance decisions
#> 
#> 
#>  Pairwise Comparison Results                                                                                             
#>  ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
#>    Comparison            Test Method       Chi-Square    p-value      Adj. p-value    Effect Size (Phi)    Significant   
#>  ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
#>    Common vs Uncommon    Chi-square          1.186668    0.2760028       0.2760028           0.06500000    No            
#>    Common vs Rare        Fisher's exact     24.843523    0.0000021       0.0000042           0.33700000    Yes           
#>    Uncommon vs Rare      Fisher's exact     15.392437    0.0001695       0.0001695           0.39200000    Yes           
#>  ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
#> 
#> 
#>  <div style='margin: 20px 0;'><h3 style='color: #1976d2;'>Detailed
#>  Pairwise Comparison Tables
#> 
#>  <div style='margin: 20px 0; padding: 15px; border: 1px solid #ddd;
#>  border-radius: 5px;'><h4 style='margin-top: 0; color: #1976d2;'>Common
#>  vs Uncommon
#> 
#>  <div style='margin-bottom: 15px; padding: 10px; background-color:
#>  #f5f5f5; border-radius: 3px;'>Test Results:
#>  Method: Chi-square test (standard approach)
#>  Chi-square = 1.187, p = 0.276, Adjusted p = 0.276
#>  Effect size (Phi) = 0.065
#>  <span style='color: #666;'>Not significant<table
#>  style='border-collapse: collapse; width: 100%; margin: 10px 0;'><tr
#>  style='background-color: #e3f2fd;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'>Negative<th style='border: 1px
#>  solid #666; padding: 8px; text-align: center;'>Positive<th
#>  style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>Total<td style='border: 1px solid #666; padding: 8px;
#>  background-color: #e3f2fd; font-weight: bold;'>Common<td
#>  style='border: 1px solid #666; padding: 8px; text-align: center;'>144
#>  (72%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>56
#>  (28%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>200<td style='border: 1px solid #666;
#>  padding: 8px; background-color: #e3f2fd; font-weight:
#>  bold;'>Uncommon<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center;'>53
#>  (65.4%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>28
#>  (34.6%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>81<tr style='background-color: #f5f5f5;
#>  font-weight: bold;'><td style='border: 1px solid #666; padding:
#>  8px;'>Total<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center;'>197
#>  (70.1%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>84
#>  (29.9%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>281<div style='margin: 20px 0; padding: 15px; border: 1px
#>  solid #ddd; border-radius: 5px;'><h4 style='margin-top: 0; color:
#>  #1976d2;'>Common vs Rare
#> 
#>  <div style='margin-bottom: 15px; padding: 10px; background-color:
#>  #f5f5f5; border-radius: 3px;'>Test Results:
#>  Method: Fisher's exact test (used due to low expected frequencies)
#>  Chi-square = 24.844, p = 2.11e-06, Adjusted p = 4.23e-06
#>  Effect size (Phi) = 0.337
#>  <span style='color: #d32f2f; font-weight: bold;'>✓ Significant<table
#>  style='border-collapse: collapse; width: 100%; margin: 10px 0;'><tr
#>  style='background-color: #e3f2fd;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'>Negative<th style='border: 1px
#>  solid #666; padding: 8px; text-align: center;'>Positive<th
#>  style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>Total<td style='border: 1px solid #666; padding: 8px;
#>  background-color: #e3f2fd; font-weight: bold;'>Common<td
#>  style='border: 1px solid #666; padding: 8px; text-align: center;'>144
#>  (72%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>56
#>  (28%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>200<td style='border: 1px solid #666;
#>  padding: 8px; background-color: #e3f2fd; font-weight: bold;'>Rare<td
#>  style='border: 1px solid #666; padding: 8px; text-align: center;'>3
#>  (15.8%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>16
#>  (84.2%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>19<tr style='background-color: #f5f5f5;
#>  font-weight: bold;'><td style='border: 1px solid #666; padding:
#>  8px;'>Total<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center;'>147
#>  (67.1%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>72
#>  (32.9%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>219<div style='margin: 20px 0; padding: 15px; border: 1px
#>  solid #ddd; border-radius: 5px;'><h4 style='margin-top: 0; color:
#>  #1976d2;'>Uncommon vs Rare
#> 
#>  <div style='margin-bottom: 15px; padding: 10px; background-color:
#>  #f5f5f5; border-radius: 3px;'>Test Results:
#>  Method: Fisher's exact test (used due to low expected frequencies)
#>  Chi-square = 15.392, p = 0.00017, Adjusted p = 0.00017
#>  Effect size (Phi) = 0.392
#>  <span style='color: #d32f2f; font-weight: bold;'>✓ Significant<table
#>  style='border-collapse: collapse; width: 100%; margin: 10px 0;'><tr
#>  style='background-color: #e3f2fd;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'><th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'>Negative<th style='border: 1px
#>  solid #666; padding: 8px; text-align: center;'>Positive<th
#>  style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>Total<td style='border: 1px solid #666; padding: 8px;
#>  background-color: #e3f2fd; font-weight: bold;'>Uncommon<td
#>  style='border: 1px solid #666; padding: 8px; text-align: center;'>53
#>  (65.4%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>28
#>  (34.6%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>81<td style='border: 1px solid #666;
#>  padding: 8px; background-color: #e3f2fd; font-weight: bold;'>Rare<td
#>  style='border: 1px solid #666; padding: 8px; text-align: center;'>3
#>  (15.8%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>16
#>  (84.2%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center; font-weight: bold;'>19<tr style='background-color: #f5f5f5;
#>  font-weight: bold;'><td style='border: 1px solid #666; padding:
#>  8px;'>Total<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center;'>56
#>  (56%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>44
#>  (44%)<td style='border: 1px solid #666; padding: 8px; text-align:
#>  center;'>100

# Example 5: Missing data handling
chisqposttest(
  data = chisqposttest_test_data,
  rows = "Treatment",
  cols = "Sex",
  excl = TRUE  # Exclude missing values
)
#> 
#>  CHI-SQUARE POST-HOC TESTS
#> 
#> character(0)
#> 
#>  Chi-Square Test Results                         
#>  ─────────────────────────────────────────────── 
#>    Statistic     Value         df    p-value     
#>  ─────────────────────────────────────────────── 
#>    Chi-Square    0.02066086     1    0.8857067   
#>  ─────────────────────────────────────────────── 
#> 
#> 
#>  <div style="padding: 15px; background-color: #f8f9fa; border-left: 4px
#>  solid #1976d2; margin: 10px 0;">
#>  <h4 style="color: #1976d2; margin-top: 0;">Chi-Square Post-Hoc
#>  Analysis Guide
#> 
#>  Three-Step Comprehensive Analysis:
#> 
#> 
#> 
#>  1.
#>  Overall Chi-Square Test:
#>  Tests if there's any association between variables
#>  <br/>
#>  2.
#>  Residuals Analysis:
#>  Identifies which specific cells contribute to significance
#>  <br/>
#>  3.
#>  Pairwise Comparisons:
#>  Formal hypothesis testing between group pairs
#> 
#> 
#> 
#>  *Recommended approach: Start with residuals analysis for pattern
#>  identification, then use pairwise tests for formal hypothesis
#>  testing.*
#> 
#> 
#>  <table border='1' style='border-collapse:
#>  collapse;'>MaleFemaleStandard5062Experimental8197
#> 
#>  <div style='margin: 15px 0;'><div style="padding: 15px;
#>  background-color: #e8f5e8; border-left: 4px solid #4caf50; margin:
#>  10px 0;">
#>  <h4 style="color: #2e7d32; margin-top: 0;">Standardized Residuals
#>  Interpretation
#> 
#>  What are standardized residuals?
#> 
#> 
#>  Standardized residuals measure how much each cell deviates from what
#>  we'd expect if there was no association. They're calculated as
#>  (Observed - Expected) / √Expected, then adjusted for multiple
#>  comparisons.
#> 
#>  <div style="background-color: #f1f8e9; padding: 10px; border-radius:
#>  3px;">
#>  Interpretation Guidelines:
#>  <br/>
#>  • |Residual| > 2.0: Suggests meaningful deviation
#>  <br/>
#>  • |Residual| > 3.0: Strong evidence of deviation
#>  <br/>
#>  • Positive values: Over-represented (more than expected)
#>  <br/>
#>  • Negative values: Under-represented (fewer than expected)
#> 
#>  <table style='border-collapse: collapse; width: 100%; margin: 15px
#>  0;'><tr style='background-color: #e8f5e8;'><th style='border: 1px
#>  solid #666; padding: 8px;'><th style='border: 1px solid #666; padding:
#>  8px; text-align: center;'>Male<th style='border: 1px solid #666;
#>  padding: 8px; text-align: center;'>Female<td style='border: 1px solid
#>  #666; padding: 8px; font-weight: bold; background-color:
#>  #f5f5f5;'>Standard<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center; background-color: #f9f9f9;'>-0.144
#>  As expected<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center; background-color: #f9f9f9;'>0.144
#>  As expected<td style='border: 1px solid #666; padding: 8px;
#>  font-weight: bold; background-color: #f5f5f5;'>Experimental<td
#>  style='border: 1px solid #666; padding: 8px; text-align: center;
#>  background-color: #f9f9f9;'>0.144
#>  As expected<td style='border: 1px solid #666; padding: 8px;
#>  text-align: center; background-color: #f9f9f9;'>-0.144
#>  As expected
#> 
#>  Critical value for significance: ±2.498
#> 
#>  <div style='padding: 15px; background-color: #d4edda; border: 1px
#>  solid #c3e6cb;'>Note: Overall chi-square test is not significant (p ≥
#>  0.05). Post-hoc pairwise comparisons are not recommended when the
#>  overall test is non-significant.
#> 
#>  Pairwise Comparison Results                                                                                  
#>  ──────────────────────────────────────────────────────────────────────────────────────────────────────────── 
#>    Comparison    Test Method    Chi-Square    p-value      Adj. p-value    Effect Size (Phi)    Significant   
#>  ──────────────────────────────────────────────────────────────────────────────────────────────────────────── 
#>  ──────────────────────────────────────────────────────────────────────────────────────────────────────────── 
#> 
#> 
#> character(0)