# Setup -------------------------------------------------------------------
rm(list = ls()) # Clear memory
library(magrittr) # Attach magrittr package (makes code more readable)
source("Demo_Manuscript/zz_merge_helpers.R") # Load code/variables
source("Demo_Manuscript/zz_summary_helpers.R") # Load code/variables
source("Demo_Manuscript/zz_load_d.R") # Load data into object called `d`
# Demographic Summaries ---------------------------------------------------
list(
F = subset(d, sex == "F"),
M = subset(d, sex == "M"),
Total = d
) %>%
mapply(
demo_summarize,
., names(.), SIMPLIFY = FALSE
) %>%
Reduce(merge, .) %>%
data.table::fwrite("Demo_Manuscript/output/demographics.csv")
# Means -------------------------------------------------------------------
paste(d$sex, d$age) %>%
split(d, .) %>%
c(
total = list(d), sex = split(d, d$sex),
age = split(d, d$age), .
) %>%
{mapply(
get_means, d = ., description = get_names(.), SIMPLIFY = FALSE
)} %>%
c(make.row.names = FALSE) %>%
do.call(rbind, .) %T>%
saveRDS("Demo_Manuscript/output/means.rds") %>%
.[.$variable == "sum_string", setdiff(names(.), "variable")] %>%
data.table::fwrite("Demo_Manuscript/output/bare_means.csv")
# Correlations ------------------------------------------------------------
names(d) %>%
.[grepl("kcal", .)] %>%
d[ ,.] %>%
cor(.) %T>%
print(.) %>%
as.vector(.) %>%
.[.!=1] %T>%
{cat("\n")} %>%
range(.)
# MAPE --------------------------------------------------------------------
paste(d$sex, d$age) %>%
split(d, .) %>%
c(
total = list(d), sex = split(d, d$sex),
age = split(d, d$age), .
) %>%
{mapply(
get_mape, d = ., description = get_names(.), SIMPLIFY = FALSE
)} %>%
c(make.row.names = FALSE) %>%
do.call(rbind, .) %>%
data.table::fwrite("Demo_Manuscript/output/mape.csv")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.