What

Some extra plots from:

Init

options(digits = 2)
library(pacman)
p_load(kirkegaard, readxl, rms, broom)

d = readxl::read_xlsx("data.xlsx") %>% df_legalize_names()

#subset by outcome
subsets = df_to_ldf(d, "metric", remove_by = F)

#simple
d2 = imap(subsets, function(x, y) {
  # browser()
  select(x, `_50th`) %>% 
    set_colnames(y)
})
d2 = do.call(cbind, d2)
d2$race = subsets$`Abdominal Circumference (mm)`$race
d2$GA = subsets$`Abdominal Circumference (mm)`$GA
d2 %<>% df_legalize_names()

Models

#plot
bpd1 = d2 %>% 
  ggplot(aes(GA, Bi_parietal_Diameter_mm, color = race)) +
  geom_path() +
  theme_bw() +
  ggtitle("Bi-parietal Diameter")
bpd1

#control GA, replot
d2$bpd_resid = ols(Bi_parietal_Diameter_mm ~ rcs(GA), data = d2) %>% resid()
bpd2 = d2 %>% 
  ggplot(aes(GA, bpd_resid, color = race)) +
  geom_path() +
  theme_bw() +
  ggtitle("Bi-parietal Diameter, age controlled")
bpd2

#save together
GG_save("bpd.png", gridExtra::grid.arrange(bpd1, bpd2))

#head circumference
#plot
hc1 = d2 %>% 
  ggplot(aes(GA, Head_Circumference_mm, color = race)) +
  geom_path() +
  theme_bw() +
  ggtitle("Head circumference")
hc1

#control GA, replot
d2$hc_resid = ols(Head_Circumference_mm ~ rcs(GA), data = d2) %>% resid()
hc2 = d2 %>% 
  ggplot(aes(GA, hc_resid, color = race)) +
  geom_path() +
  theme_bw() +
  ggtitle("Head circumference, age controlled")
hc2

#save together
GG_save("hc.png", gridExtra::grid.arrange(hc1, hc2))

#body size
#plot
fe1 = d2 %>% 
  ggplot(aes(GA, Femur_Length_mm, color = race)) +
  geom_path() +
  theme_bw() +
  ggtitle("Femur (thigh) length")
fe1

#control GA, replot
d2$fe_resid = ols(Femur_Length_mm ~ rcs(GA), data = d2) %>% resid()
fe2 = d2 %>% 
  ggplot(aes(GA, fe_resid, color = race)) +
  geom_path() +
  theme_bw() +
  ggtitle("Femur (thigh) length, age controlled")
fe2

#save together
GG_save("fe.png", gridExtra::grid.arrange(fe1, fe2))

#head controlled for femur and age
d2$head_body = d2$Head_Circumference_mm / d2$Abdominal_Circumference_mm
hc3 = ggplot(d2, aes(GA, head_body, color = race)) +
  geom_path() +
  theme_bw() +
  ggtitle("Head to body size")
hc3

GG_save("head_body.png")