library(gt)grip_summary_ttest %>%mutate(p_value =ifelse(is.na(p_value), "—", sprintf("%.3f", p_value))) %>%gt() %>%tab_header(title ="Grip Strength by Cancer History within Activity Groups",subtitle ="Mean ± SD (n) with within-group Welch t-tests" )
Grip Strength by Cancer History within Activity Groups
Mean ± SD (n) with within-group Welch t-tests
activity_group
CancerFALSE
CancerTRUE
t
df
p_value
NHANES_Inactive
31.02 ± 10.26 (n=1772)
29.26 ± 10.49 (n=349)
2.8768630
488.02746
0.004
SG_Athletes
39.35 ± 12.34 (n=89)
37.32 ± 9.47 (n=26)
0.8928796
52.29828
0.376
Code
library(dplyr)library(broom)# make sure grouping variable and cancer labels are consistentdf <- combined %>%mutate(activity_group =case_when( dataset =="SG25"~"SG_Athletes", dataset =="NHANES"& meets_ACSM_guidelines ~"NHANES_Active", dataset =="NHANES"&!meets_ACSM_guidelines ~"NHANES_Inactive",TRUE~NA_character_ ),cancer =if_else(cancer %in%c(TRUE, 1, "Yes", "Y"), "CancerTRUE", "CancerFALSE") ) %>%filter(!is.na(activity_group), is.finite(grip_dom))# unique groupsgroups <-unique(df$activity_group)# loop through each and print t-test resultsfor (g in groups) {cat("\n-------------------------------\n")cat("Activity Group:", g, "\n")cat("-------------------------------\n") sub <- df %>%filter(activity_group == g)# only run test if both cancer levels are presentif (n_distinct(sub$cancer) ==2) { t_res <-t.test(grip_dom ~ cancer, data = sub, conf.level =0.90) #0.95print(t_res) } else {cat("⚠️ Skipping:", g, "– only one cancer level present.\n") }}
-------------------------------
Activity Group: NHANES_Inactive
-------------------------------
Welch Two Sample t-test
data: grip_dom by cancer
t = 2.8769, df = 488.03, p-value = 0.004192
alternative hypothesis: true difference in means between group CancerFALSE and group CancerTRUE is not equal to 0
90 percent confidence interval:
0.7520097 2.7689638
sample estimates:
mean in group CancerFALSE mean in group CancerTRUE
31.02410 29.26361
-------------------------------
Activity Group: NHANES_Active
-------------------------------
Welch Two Sample t-test
data: grip_dom by cancer
t = 1.7362, df = 449.51, p-value = 0.08322
alternative hypothesis: true difference in means between group CancerFALSE and group CancerTRUE is not equal to 0
90 percent confidence interval:
0.05610361 2.15971163
sample estimates:
mean in group CancerFALSE mean in group CancerTRUE
35.23328 34.12537
-------------------------------
Activity Group: SG_Athletes
-------------------------------
Welch Two Sample t-test
data: grip_dom by cancer
t = 0.89288, df = 52.298, p-value = 0.376
alternative hypothesis: true difference in means between group CancerFALSE and group CancerTRUE is not equal to 0
90 percent confidence interval:
-1.775298 5.831218
sample estimates:
mean in group CancerFALSE mean in group CancerTRUE
39.34719 37.31923
Bayesian ANOVA:
Code
library(brms)# ensure artifacts/models directory exists under analysis/model_dir <- here::here("analysis", "artifacts", "models")dir.create(model_dir, showWarnings =FALSE, recursive =TRUE)# define model file name within artifacts storagemodel_file <-file.path(model_dir, "anova_grip_activity_cancer.rds")# check and load or fitif (file.exists(model_file)) {message("✅ Model already exists — loading from file.") anova_fit <-readRDS(model_file)} else {message("🚀 Model not found — fitting new model now.") anova_fit <-brm(formula = grip_dom ~ activity_group * cancer,data = df,family =student(),backend ="cmdstanr",seed =42,chains =4,cores =4,iter =4000,prior =c(prior(normal(0, 5), class ="b"),prior(student_t(3, 0, 10), class ="Intercept"),prior(exponential(1), class ="sigma") ) )# save the fitted modelsaveRDS(anova_fit, file = model_file)message("💾 Model saved to ", model_file)}
Code
library(emmeans)# Marginal means (adjusted for the model structure)emm <-emmeans(anova_fit, ~ activity_group * cancer, , level =0.90) #0.95summary(emm)