Last updated: 2025-03-09
Checks: 6 1
Knit directory: SAPPHIRE/
This reproducible R Markdown analysis was created with workflowr (version 1.7.1). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.
The R Markdown file has unstaged changes. To know which version of
the R Markdown file created these results, you’ll want to first commit
it to the Git repo. If you’re still working on the analysis, you can
ignore this warning. When you’re finished, you can run
wflow_publish to commit the R Markdown file and build the
HTML.
Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.
The command set.seed(20240923) was run prior to running
the code in the R Markdown file. Setting a seed ensures that any results
that rely on randomness, e.g. subsampling or permutations, are
reproducible.
Great job! Recording the operating system, R version, and package versions is critical for reproducibility.
Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.
Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.
Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.
The results in this page were generated with repository version 9217a86. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.
Note that you need to be careful to ensure that all relevant files for
the analysis have been committed to Git prior to generating the results
(you can use wflow_publish or
wflow_git_commit). workflowr only checks the R Markdown
file, but you know if there are other scripts or data files that it
depends on. Below is the status of the Git repository when the results
were generated:
Ignored files:
Ignored: .DS_Store
Ignored: .Rhistory
Ignored: .Rproj.user/
Ignored: data/.DS_Store
Ignored: output/.DS_Store
Unstaged changes:
Modified: analysis/pigmentationdata.Rmd
Modified: analysis/summer-winter.Rmd
Modified: output/coeff_plots_summer-winter/coeff_Forehead_B.png
Modified: output/coeff_plots_summer-winter/coeff_Forehead_CIE_L.png
Modified: output/coeff_plots_summer-winter/coeff_Forehead_CIE_a.png
Modified: output/coeff_plots_summer-winter/coeff_Forehead_CIE_b.png
Modified: output/coeff_plots_summer-winter/coeff_Forehead_E.png
Modified: output/coeff_plots_summer-winter/coeff_Forehead_G.png
Modified: output/coeff_plots_summer-winter/coeff_Forehead_M.png
Modified: output/coeff_plots_summer-winter/coeff_Forehead_R.png
Modified: output/coeff_plots_summer-winter/coeff_LeftUpperInnerArm_B.png
Modified: output/coeff_plots_summer-winter/coeff_LeftUpperInnerArm_CIE_L.png
Modified: output/coeff_plots_summer-winter/coeff_LeftUpperInnerArm_CIE_a.png
Modified: output/coeff_plots_summer-winter/coeff_LeftUpperInnerArm_CIE_b.png
Modified: output/coeff_plots_summer-winter/coeff_LeftUpperInnerArm_E.png
Modified: output/coeff_plots_summer-winter/coeff_LeftUpperInnerArm_G.png
Modified: output/coeff_plots_summer-winter/coeff_LeftUpperInnerArm_M.png
Modified: output/coeff_plots_summer-winter/coeff_LeftUpperInnerArm_R.png
Modified: output/coeff_plots_summer-winter/coeff_RightUpperInnerArm_B.png
Modified: output/coeff_plots_summer-winter/coeff_RightUpperInnerArm_CIE_L.png
Modified: output/coeff_plots_summer-winter/coeff_RightUpperInnerArm_CIE_a.png
Modified: output/coeff_plots_summer-winter/coeff_RightUpperInnerArm_CIE_b.png
Modified: output/coeff_plots_summer-winter/coeff_RightUpperInnerArm_E.png
Modified: output/coeff_plots_summer-winter/coeff_RightUpperInnerArm_G.png
Modified: output/coeff_plots_summer-winter/coeff_RightUpperInnerArm_M.png
Modified: output/coeff_plots_summer-winter/coeff_RightUpperInnerArm_R.png
Modified: output/withinstats_filtered/Forehead_B.png
Modified: output/withinstats_filtered/Forehead_CIE_L.png
Modified: output/withinstats_filtered/Forehead_CIE_a.png
Modified: output/withinstats_filtered/Forehead_CIE_b.png
Modified: output/withinstats_filtered/Forehead_E.png
Modified: output/withinstats_filtered/Forehead_G.png
Modified: output/withinstats_filtered/Forehead_M.png
Modified: output/withinstats_filtered/Forehead_R.png
Modified: output/withinstats_filtered/LeftUpperInnerArm_B.png
Modified: output/withinstats_filtered/LeftUpperInnerArm_CIE_L.png
Modified: output/withinstats_filtered/LeftUpperInnerArm_CIE_a.png
Modified: output/withinstats_filtered/LeftUpperInnerArm_CIE_b.png
Modified: output/withinstats_filtered/LeftUpperInnerArm_E.png
Modified: output/withinstats_filtered/LeftUpperInnerArm_G.png
Modified: output/withinstats_filtered/LeftUpperInnerArm_M.png
Modified: output/withinstats_filtered/LeftUpperInnerArm_R.png
Modified: output/withinstats_filtered/RightUpperInnerArm_B.png
Modified: output/withinstats_filtered/RightUpperInnerArm_CIE_L.png
Modified: output/withinstats_filtered/RightUpperInnerArm_CIE_a.png
Modified: output/withinstats_filtered/RightUpperInnerArm_CIE_b.png
Modified: output/withinstats_filtered/RightUpperInnerArm_E.png
Modified: output/withinstats_filtered/RightUpperInnerArm_G.png
Modified: output/withinstats_filtered/RightUpperInnerArm_M.png
Modified: output/withinstats_filtered/RightUpperInnerArm_R.png
Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.
These are the previous versions of the repository in which changes were
made to the R Markdown (analysis/summer-winter.Rmd) and
HTML (docs/summer-winter.html) files. If you’ve configured
a remote Git repository (see ?wflow_git_remote), click on
the hyperlinks in the table below to view the files as they were in that
past version.
| File | Version | Author | Date | Message |
|---|---|---|---|---|
| Rmd | 9217a86 | Tina Lasisi | 2025-03-07 | Cleaned data and redid analyses |
library(ggstatsplot)
You can cite this package as:
Patil, I. (2021). Visualizations with statistical details: The 'ggstatsplot' approach.
Journal of Open Source Software, 6(61), 3167, doi:10.21105/joss.03167
## ---- ensure-both-seasons-and-then-ggwithinstats ----
## This code explicitly filters your data so that each participant has BOTH Summer and Winter
## for each (body_site, measurement_type). We then run grouped_ggwithinstats on that subset.
## This ensures a truly paired analysis with no partial data.
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.5.1 ✔ tibble 3.2.1
✔ lubridate 1.9.4 ✔ tidyr 1.3.1
✔ purrr 1.0.4
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggstatsplot)
# 1) Read your final medians data, with columns like:
# ParticipantCentreID, Season, Ethnicity, final_median, body_site, measurement_type
df <- read_csv("data/ScreeningDataCollection_medians.csv") %>%
filter(Season %in% c("Summer","Winter")) %>%
mutate(Season = factor(Season, levels = c("Summer","Winter")))
Rows: 5280 Columns: 8
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (5): ParticipantCentreID, Ethnicity, Season, body_site, measurement_type
dbl (3): Gender, final_median, n_valid
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# 2) Keep only those participants who have BOTH Summer and Winter for each site+metric.
# i.e., for each (ParticipantCentreID, body_site, metric), n_distinct(Season) == 2
df_valid <- df %>%
group_by(ParticipantCentreID, body_site, measurement_type) %>%
filter(n_distinct(Season) == 2) %>%
ungroup()
# 3) Now for each (body_site, measurement_type) combo, we do a grouped_ggwithinstats
# that splits the data by Ethnicity, and does a repeated-measures (paired) test
# comparing Summer vs. Winter.
combos <- df_valid %>%
distinct(body_site, measurement_type)
# 4) We'll loop over combos and produce a plot for each
# (If you want a single big combined plot, you'd do a different approach.)
plots <- combos %>%
mutate(
plotobj = map2(body_site, measurement_type, ~ {
sub_data <- df_valid %>%
filter(body_site == .x, measurement_type == .y)
# If we have no data, skip
if (nrow(sub_data) < 2) return(NULL)
grouped_ggwithinstats(
data = sub_data,
x = Season, # 2-level repeated measure
y = final_median, # numeric outcome
subject.id = ParticipantCentreID,# repeated ID column
grouping.var = Ethnicity, # one subplot per Ethnicity
paired = TRUE, # do a paired test
type = "parametric", # or "nonparametric", etc.
title.text = paste("Body site:", .x, "| Metric:", .y),
caption.text = "Paired test (Summer vs. Winter) within each Ethnicity",
plotgrid.args= list(ncol = 2) # if you have 2 Ethnicities, side-by-side
)
})
)
# 5) Save each figure
dir.create("output/withinstats_filtered", showWarnings = FALSE, recursive = TRUE)
plots %>%
rowwise() %>%
mutate(
outfile = paste0("output/withinstats_filtered/", body_site, "_", measurement_type, ".png"),
saved = {
if (!is.null(plotobj)) {
ggsave(outfile, plotobj, width=10, height=6)
TRUE
} else FALSE
}
)
# A tibble: 24 × 5
# Rowwise:
body_site measurement_type plotobj outfile saved
<chr> <chr> <list> <chr> <lgl>
1 Forehead B <patchwrk> output/withinstats_filte… TRUE
2 Forehead CIE_L <patchwrk> output/withinstats_filte… TRUE
3 Forehead CIE_a <patchwrk> output/withinstats_filte… TRUE
4 Forehead CIE_b <patchwrk> output/withinstats_filte… TRUE
5 Forehead E <patchwrk> output/withinstats_filte… TRUE
6 Forehead G <patchwrk> output/withinstats_filte… TRUE
7 Forehead M <patchwrk> output/withinstats_filte… TRUE
8 Forehead R <patchwrk> output/withinstats_filte… TRUE
9 LeftUpperInnerArm B <patchwrk> output/withinstats_filte… TRUE
10 LeftUpperInnerArm CIE_L <patchwrk> output/withinstats_filte… TRUE
# ℹ 14 more rows
stage2_data <- read_csv("data/ScreeningDataCollection_stage2.csv") %>%
group_by(ParticipantCentreID, body_site, measurement_type, Season) %>%
mutate(
# Subtract however many NA values are in 'value' from n_valid
n_valid = n_valid - sum(is.na(value))
) %>%
ungroup() %>%
# Then remove rows with NA in 'value'
filter(!is.na(value))
Rows: 15840 Columns: 24
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (7): ParticipantCentreID, Season, body_site, measurement_type, Ethnicit...
dbl (16): Gender, value, replicate, mean_val, sd_val, n_valid, cv, n, min_va...
lgl (1): is_outlier_allSites
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
write.csv(stage2_data, "data/ScreeningDataCollection_stage2_clean.csv", row.names = FALSE)
stage2_data <- stage2_data %>%
filter(Season %in% c("Summer","Winter")) %>%
mutate(Season = factor(Season, levels=c("Summer","Winter")))
# Suppose your replicate-level data is in 'stage2_data' with columns:
# ParticipantCentreID, body_site, metric, Ethnicity, Season, value, etc.
# We have 2 levels in Season: "Summer","Winter".
library(lmerTest)
Loading required package: lme4
Loading required package: Matrix
Attaching package: 'Matrix'
The following objects are masked from 'package:tidyr':
expand, pack, unpack
Attaching package: 'lmerTest'
The following object is masked from 'package:lme4':
lmer
The following object is masked from 'package:stats':
step
library(tidyverse)
# 2) Identify combos of body_site, metric, and Ethnicity
combos <- stage2_data %>%
distinct(body_site, measurement_type, Ethnicity)
results_list <- list()
# 3) Loop over each combination
for (i in seq_len(nrow(combos))) {
bs <- combos$body_site[i]
mt <- combos$measurement_type[i]
eth <- combos$Ethnicity[i]
# subset
sub_data <- stage2_data %>%
filter(body_site == bs, measurement_type == mt, Ethnicity == eth)
# skip if not enough data
if (nrow(sub_data) < 4) {
next
}
# 4) Fit random intercept model
# value ~ Season + (1|ParticipantCentreID)
mod <- tryCatch({
lmer(
value ~ Season + (1|ParticipantCentreID),
data = sub_data
)
}, error=function(e) NULL)
if (!is.null(mod)) {
# store summary or any results
sum_mod <- summary(mod)
results_list[[paste(bs,mt,eth,sep="_")]] <- sum_mod
}
}
# Then you can examine 'results_list', each containing the model summary for
# that site–metric–ethnicity combination. You see if 'Season' is significant
# in each subset.
## ---- coeff-plots-summer-winter ----
## This script creates a single side-by-side (ncol=2) coefficient plot
## for each (body_site, measurement_type), comparing the two ethnic groups
## (e.g., "CapeMixed" vs. "Xhosa"). Each subplot shows a random-intercept
## model's coefficients for value ~ Season + (1|ParticipantCentreID).
library(lmerTest)
library(tidyverse)
library(ggstatsplot)
# 1) Load and clean your stage2_data
stage2_data <- read_csv("data/ScreeningDataCollection_stage2.csv") %>%
group_by(ParticipantCentreID, body_site, measurement_type, Season) %>%
mutate(
# Subtract however many NA values are in 'value' from n_valid
n_valid = n_valid - sum(is.na(value))
) %>%
ungroup() %>%
# Then remove rows with NA in 'value'
filter(!is.na(value)) %>%
# Keep only Summer/Winter
filter(Season %in% c("Summer","Winter")) %>%
mutate(
Season = factor(Season, levels=c("Summer","Winter")),
body_site = factor(body_site),
measurement_type = factor(measurement_type),
Ethnicity = factor(Ethnicity),
ParticipantCentreID= factor(ParticipantCentreID)
)
Rows: 15840 Columns: 24
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (7): ParticipantCentreID, Season, body_site, measurement_type, Ethnicit...
dbl (16): Gender, value, replicate, mean_val, sd_val, n_valid, cv, n, min_va...
lgl (1): is_outlier_allSites
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# 2) Identify combos of body_site + measurement_type
combos <- stage2_data %>%
distinct(body_site, measurement_type) %>%
arrange(body_site, measurement_type)
# 3) Create a directory "output/coeff_plots_summer-winter"
dir.create("output/coeff_plots_summer-winter", showWarnings = FALSE, recursive = TRUE)
# 4) We'll handle two ethnic groups side-by-side: "CapeMixed" & "Xhosa"
ethnic_groups <- c("CapeMixed", "Xhosa")
for (i in seq_len(nrow(combos))) {
bs <- combos$body_site[i]
mt <- combos$measurement_type[i]
# We'll store the two coefficient plots in a list
plot_list <- list()
valid_plots <- 0
for (eth in ethnic_groups) {
# Subset data
sub_data <- stage2_data %>%
filter(body_site == bs, measurement_type == mt, Ethnicity == eth)
if (nrow(sub_data) < 4) {
message("Skipping: ", bs, " - ", mt, " - ", eth, " => not enough data.")
plot_list[[eth]] <- NULL
next
}
# Fit a random intercept model
mod <- tryCatch({
lmer(value ~ Season + (1|ParticipantCentreID), data=sub_data)
}, error = function(e) NULL)
if (is.null(mod)) {
message("Model failed for: ", bs, " - ", mt, " - ", eth)
plot_list[[eth]] <- NULL
next
}
# Create a coefficient plot
plot_title <- paste0(bs, " - ", as.character(mt), " - ", eth)
p <- ggcoefstats(
x = mod,
title = plot_title,
xlab = "Coefficient (beta)",
ylab = "Fixed Effects"
)
plot_list[[eth]] <- p
valid_plots <- valid_plots + 1
}
# If neither ethnicity had enough data, skip saving
if (valid_plots == 0) {
next
}
# 5) Combine the two coefficient plots side by side (ncol=2)
# If one ethnicity was missing data, combine_plots can handle a NULL
final_plot <- combine_plots(
plotlist = plot_list,
plotgrid.args = list(ncol = 2),
annotation.args= list(
title = paste0("Coefficients: ", bs, " - ", mt),
caption = "Random intercept model: value ~ Season + (1|ParticipantCentreID)"
)
)
# 6) Save
outfile <- paste0("output/coeff_plots_summer-winter/coeff_", bs, "_", mt, ".png")
ggsave(outfile, final_plot, width=10, height=6)
message("Saved: ", outfile)
}
Saved: output/coeff_plots_summer-winter/coeff_Forehead_B.png
Saved: output/coeff_plots_summer-winter/coeff_Forehead_CIE_a.png
Saved: output/coeff_plots_summer-winter/coeff_Forehead_CIE_b.png
Saved: output/coeff_plots_summer-winter/coeff_Forehead_CIE_L.png
Saved: output/coeff_plots_summer-winter/coeff_Forehead_E.png
Saved: output/coeff_plots_summer-winter/coeff_Forehead_G.png
Saved: output/coeff_plots_summer-winter/coeff_Forehead_M.png
Saved: output/coeff_plots_summer-winter/coeff_Forehead_R.png
Saved: output/coeff_plots_summer-winter/coeff_LeftUpperInnerArm_B.png
Saved: output/coeff_plots_summer-winter/coeff_LeftUpperInnerArm_CIE_a.png
Saved: output/coeff_plots_summer-winter/coeff_LeftUpperInnerArm_CIE_b.png
Saved: output/coeff_plots_summer-winter/coeff_LeftUpperInnerArm_CIE_L.png
Saved: output/coeff_plots_summer-winter/coeff_LeftUpperInnerArm_E.png
Saved: output/coeff_plots_summer-winter/coeff_LeftUpperInnerArm_G.png
Saved: output/coeff_plots_summer-winter/coeff_LeftUpperInnerArm_M.png
Saved: output/coeff_plots_summer-winter/coeff_LeftUpperInnerArm_R.png
Saved: output/coeff_plots_summer-winter/coeff_RightUpperInnerArm_B.png
Saved: output/coeff_plots_summer-winter/coeff_RightUpperInnerArm_CIE_a.png
Saved: output/coeff_plots_summer-winter/coeff_RightUpperInnerArm_CIE_b.png
Saved: output/coeff_plots_summer-winter/coeff_RightUpperInnerArm_CIE_L.png
Saved: output/coeff_plots_summer-winter/coeff_RightUpperInnerArm_E.png
Saved: output/coeff_plots_summer-winter/coeff_RightUpperInnerArm_G.png
Saved: output/coeff_plots_summer-winter/coeff_RightUpperInnerArm_M.png
Saved: output/coeff_plots_summer-winter/coeff_RightUpperInnerArm_R.png
sessionInfo()
R version 4.4.3 (2025-02-28)
Platform: aarch64-apple-darwin20
Running under: macOS Sequoia 15.3.1
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: America/Detroit
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] lmerTest_3.1-3 lme4_1.1-36 Matrix_1.7-2 lubridate_1.9.4
[5] forcats_1.0.0 stringr_1.5.1 dplyr_1.1.4 purrr_1.0.4
[9] readr_2.1.5 tidyr_1.3.1 tibble_3.2.1 ggplot2_3.5.1
[13] tidyverse_2.0.0 ggstatsplot_0.13.0 workflowr_1.7.1
loaded via a namespace (and not attached):
[1] Rdpack_2.6.2 pbapply_1.7-2 rematch2_2.1.2
[4] rlang_1.1.5 magrittr_2.0.3 git2r_0.35.0
[7] compiler_4.4.3 statsExpressions_1.6.2 getPass_0.2-4
[10] systemfonts_1.2.1 callr_3.7.6 vctrs_0.6.5
[13] pkgconfig_2.0.3 crayon_1.5.3 fastmap_1.2.0
[16] labeling_0.4.3 effectsize_1.0.0 utf8_1.2.4
[19] promises_1.3.2 rmarkdown_2.29 tzdb_0.4.0
[22] ps_1.8.1 nloptr_2.1.1 ragg_1.3.3
[25] MatrixModels_0.5-3 bit_4.5.0.1 xfun_0.50
[28] cachem_1.1.0 jsonlite_1.8.9 later_1.4.1
[31] parallel_4.4.3 R6_2.5.1 bslib_0.9.0
[34] stringi_1.8.4 boot_1.3-31 numDeriv_2016.8-1.1
[37] jquerylib_0.1.4 estimability_1.5.1 Rcpp_1.0.14
[40] knitr_1.49 parameters_0.24.2 correlation_0.8.7
[43] httpuv_1.6.15 splines_4.4.3 timechange_0.3.0
[46] tidyselect_1.2.1 rstudioapi_0.17.1 yaml_2.3.10
[49] processx_3.8.5 lattice_0.22-6 withr_3.0.2
[52] bayestestR_0.15.2 coda_0.19-4.1 evaluate_1.0.3
[55] RcppParallel_5.1.10 pillar_1.10.1 whisker_0.4.1
[58] reformulas_0.4.0 insight_1.1.0 generics_0.1.3
[61] vroom_1.6.5 rprojroot_2.0.4 paletteer_1.6.0
[64] hms_1.1.3 rstantools_2.4.0 munsell_0.5.1
[67] scales_1.3.0 minqa_1.2.8 glue_1.8.0
[70] emmeans_1.10.7 tools_4.4.3 fs_1.6.5
[73] mvtnorm_1.3-3 grid_4.4.3 rbibutils_2.3
[76] datawizard_1.0.1 colorspace_2.1-1 nlme_3.1-167
[79] patchwork_1.3.0 performance_0.13.0 cli_3.6.3
[82] textshaping_1.0.0 gtable_0.3.6 zeallot_0.1.0
[85] sass_0.4.9 digest_0.6.37 prismatic_1.1.2
[88] ggrepel_0.9.6 farver_2.1.2 htmltools_0.5.8.1
[91] lifecycle_1.0.4 httr_1.4.7 bit64_4.6.0-1
[94] BayesFactor_0.9.12-4.7 MASS_7.3-64