-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresidual-models.R
71 lines (62 loc) · 2 KB
/
residual-models.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Taking residual from holdout CNN,
# modeling them based on demography variables
library(dplyr)
library(tidyr)
library(acceleep)+
library(kableExtra)
# Gather holdout results, CNN only
holdout_results <- readRDS(here::here("output", "results", "holdout.rds")) %>%
filter(model_kind == "CNN")
cnn_results <- holdout_results %>%
unnest(data) %>%
unnest(predicted_obs) %>%
select(accel, model, placement, outcome_unit, ID, interval, outcome, predicted) %>%
filter(outcome != 0) %>%
mutate(
residual = outcome - predicted,
residual_rel = (outcome - predicted) / outcome
)
# Merge with demo data (mind the numeric ID)
cnn_results <- get_demo() %>%
select(-date) %>%
mutate(ID = sprintf("%03i", ID)) %>%
inner_join(cnn_results, by = "ID")
# Group by outcome unit, make linear models using both absolute and relative residuals
residual_models <- cnn_results %>%
mutate(outcome_unit = as.character(outcome_unit)) %>%
group_by(outcome_unit) %>%
group_map(~{
xmd <- lm(residual ~ sex + height + weight + hand + age, data = .x)
xmd_rel <- lm(residual_rel ~ sex + height + weight + hand + age, data = .x)
bind_rows(
broom::tidy(xmd) %>%
mutate(response = "residual"),
broom::tidy(xmd_rel) %>%
mutate(response = "residual/outcome")
) %>%
mutate(
outcome_unit = .y[[1]],
p.value = ifelse(p.value < 0.05, "< 0.05", as.character(round(p.value, 2)))
)
}) %>%
bind_rows()
# Save for later
save_output_data(residual_models, "results")
# Tabularize?
residual_models %>%
select(outcome_unit, response, everything()) %>%
mutate(across(where(is.numeric), round, 2)) %>%
mutate(
estimate = glue::glue("{estimate} ({std.error}), t={statistic} (p = {p.value})")
) %>%
pivot_wider(
id_cols = c(outcome_unit, response, term),
names_from = response,
values_from = estimate
) %>%
kable() %>%
kable_styling() %>%
#collapse_rows(1) %>%
pack_rows("J/min/kg", 1, 6) %>%
pack_rows("kJ", 7, 12) %>%
pack_rows("MET", 13, 18)