diff --git a/reports/correlation-3/correlation-3-pc.R b/reports/correlation-3/correlation-3-pc.R index 941b1387..e8386de1 100644 --- a/reports/correlation-3/correlation-3-pc.R +++ b/reports/correlation-3/correlation-3-pc.R @@ -4,7 +4,7 @@ rm(list=ls(all=TRUE)) #Clear the memory of variables from previous run. This is #Load any source files that contain/define functions, but that don't load any other types of variables # into memory. Avoid side effects and don't pollute the global environment. source("./scripts/mplus/model-components.R") # organizes variable names -source("./reports/correlation-3/functions-support.R") +source("./reports/correlation-3/support-functions.R") # ---- load-packages ----------------------------------------------------------- library(magrittr) #Pipes library(ggplot2) @@ -27,7 +27,7 @@ catalog <- readr::read_csv(path_input) rm(path_input) # ---- tweak-data -------------------- -catalog_pretty <- catalog %>% prettify_catalog(model_type_ = "aehplus",model_number_ = "b1") +# catalog_pretty <- catalog %>% prettify_catalog(model_type_ = "aehplus",model_number_ = "b1") # ---- save-data-for-tables -------------------------- # for(outcome in c("gait","grip","pulmonary")){ @@ -43,10 +43,6 @@ catalog_pretty <- catalog %>% prettify_catalog(model_type_ = "aehplus",model_num # } # } -# ---- print-forestplot ------------- - - - # ---- table-dynamic ----------------------------------------------------------- @@ -109,6 +105,9 @@ d %>% # ---- table-static-full ------------------------------------------------------------ +# outcome values is defined in Rmd +# outcome = "pulmonary" +# gender = "male" cat("\n#Group by domain\n") for(gender in c("male","female")){ cat("\n##",gender) @@ -186,6 +185,46 @@ for(gender in c("male","female")){ } +# ---- print-forestplot ------------- +track = "pulmonary" +domain = "memory" + +# browser() +d1 <- catalog %>% + prettify_catalog(model_type_ = c("aehplus"), model_number_ = "b1") %>% + dplyr::select( + process_b_domain, study_name, + model_number, subgroup, model_type, process_a, process_b, subject_count, + er_tau_11_est, er_tau_11_ci95lo, er_tau_11_ci95hi, er_slopes + ) %>% + dplyr::mutate( + subject_count = scales::comma(subject_count) + ) %>% + plyr::rename( c( + "process_b_domain" ="domain", + "study_name" ="study", + "process_a" ="physical", + "process_b" ="cognitive", + "subject_count" ="n", + "er_tau_11_est" ="mean", + "er_tau_11_ci95lo" ="lower", + "er_tau_11_ci95hi" ="upper", + "er_slopes" ="dense" + )) %>% + dplyr::mutate( + dense = gsub("[$]p[$]","p",dense) + ) %>% + dplyr::filter(physical %in% c("pef","pek","fev"))# %>% +d <- d1 %>% rename_domains(track) + +for(dom in unique(d$domain)){ + for(gender in (unique(d$subgroup))){ + d <- d %>% dplyr::filter(subgroup == gender) + d %>% print_forest_plot(dom) + } +} +# d %>% print_forest_plot("memory") + # ---- publish -------------- path_pulmonary_full <- "./reports/correlation-3/correlation-3-pulmonary-full.Rmd" path_pulmonary_focus <- "./reports/correlation-3/correlation-3-pulmonary-focus.Rmd" diff --git a/reports/correlation-3/correlation-3-pulmonary-focus.Rmd b/reports/correlation-3/correlation-3-pulmonary-focus.Rmd index 43ade015..f66a3ffa 100644 --- a/reports/correlation-3/correlation-3-pulmonary-focus.Rmd +++ b/reports/correlation-3/correlation-3-pulmonary-focus.Rmd @@ -101,6 +101,11 @@ where ``` +# Plots + +```{r print-forest, echo=echoChunks, message=TRUE, results='asis', eval=TRUE} +``` + # Session Information diff --git a/reports/correlation-3/support-functions.R b/reports/correlation-3/support-functions.R index d2e6af2c..3903bd62 100644 --- a/reports/correlation-3/support-functions.R +++ b/reports/correlation-3/support-functions.R @@ -189,7 +189,7 @@ select_for_table <- function( outcome, # gait, grip, pulmonary : selects process a / PHYSICAL MEASURE gender = "andro", # andro, male, female : selects subgroup format = "full", # full, focus, brief : selects columns to display - pretty_name = TRUE + pretty_name = TRUE # columns names are pretty, ready for table ){ # browser() d1 <- catalog_pretty @@ -275,6 +275,10 @@ select_for_table <- function( er_sigma_00_est, er_sigma_00_se, er_sigma_00_pval, er_sigma_00_ci95lo, er_sigma_00_ci95hi ) } + d4 <- d4 %>% + dplyr::mutate( + subject_count = scales::comma(subject_count) + ) return(d4) } @@ -302,13 +306,15 @@ rename_domains <- function( catalog_pretty_selected, track_name ){ + # browser() + # track_name = "pulmonary" path_stencil = "./reports/correlation-3/rename-domains-" path = paste0(path_stencil,track_name,".csv") d_rules <- readr::read_csv(path) %>% dplyr::select(domain, domain_new, study, cognitive, physical) - t <- table(d$domain, d$study); t[t==0]<-"."; t + # t <- table(d$domain, d$study); t[t==0]<-"."; t # join the model data frame to the conversion data frame. d <- catalog_pretty_selected %>% dplyr::left_join(d_rules ) @@ -324,3 +330,61 @@ rename_domains <- function( } +# --- forest ----------------- +print_forest_plot <- function( + catalog, + domain_ +){ + + d <- catalog %>% + dplyr::select(domain,study, physical, cognitive,n,mean,lower, upper,dense ) %>% + dplyr::filter(domain == domain_) + + col_1 <- c(NA,"Study") + col_2 <- c("Physical","process") + col_3 <- c("Cognitive","process") + col_4 <- c("Sample","size") + text_top <- data.frame( + "study" = col_1, + "physical" = col_2, + "cognitive" = col_3, + "n" = col_4, + "dense" = as.character(c(NA,NA)) + ) + + d_text <- d %>% + dplyr::select(study, physical, cognitive, n, dense) %>% + as.data.frame() + d_text <- dplyr::bind_rows(text_top, d_text) + n_rows <- nrow(d_text) + d_text[(n_rows+1):(n_rows+2),] <- NA + d_value <- data.frame( + "mean" = c(NA,NA,d$mean, NA, mean(d$mean, na.rm=T)), + "lower"= c(NA,NA,d$lower,NA, mean(d$lower, na.rm=T)), + "upper"= c(NA,NA,d$upper,NA, mean(d$upper, na.rm=T)) + ) + + + g <- forestplot::forestplot( + d_text, + d_value, + # mean = d$mean, + # lower = d$lower, + # upper = d$upper, + align = c("r","r","l","l","l","l"), + new_page = TRUE, + is.summary = c(TRUE,TRUE,rep(FALSE,n_rows-1),TRUE), + clip = c(-2,2), + # xlog = TRUE, + col = fpColors(box = "royalblue", + line = "darkblue", + summary = "royalblue"), + hrzl_lines = gpar(col="#444444"), + graph.pos = 5 + ) + return(g) +} +# Usage: +# print_forest_plot(catalog,"pulmonary","memory") + +