-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiet.rmd
157 lines (132 loc) · 4.61 KB
/
diet.rmd
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
---
title: "Diet covariates"
output: html_notebook
---
```{r}
library(arivale.data.interface)
list_snapshot_contents()
qs <- get_snapshot("assessments", clean=T)
diet_variables <- names(qs)[grepl("diet_freq", names(qs))]
lifestyle_variables <- c(
"assessment_lifestyle_cruciferous_vegetables_enum",
"assessment_lifestyle_fruits_enum",
"assessment_lifestyle_vegetables_enum",
"assessment_lifestyle_sugary_drinks_enum",
"assessment_lifestyle_water_enum",
"assessment_lifestyle_alcohol_drinks_a_day_enum",
"assessment_lifestyle_grains_enum"
)
variables <- c(diet_variables, lifestyle_variables)
qs <- qs[, c("public_client_id", variables), with=FALSE]
qs[, "complete" := apply(qs, 1, function(x) sum(!is.na(x))/length(x))]
qs <- qs[order(-complete)][!duplicated(public_client_id)]
scales <- apply(qs[, variables, with=F], 2, function(x) unique(x, na.rm=T) %>%
sort() %>% paste(collapse = ", ")) %>% data.table()
scales <- data.table(quantity=variables, scale=scales)
fwrite(scales, "data/diet_scales.csv")
```
All of those should be on a ordinal scale so we will conver them to the corresponding rank values.
```{r}
library(stringr)
library(magrittr)
for (v in variables) {
qs[[v]] <- str_match(qs[[v]], "^\\((\\d)\\)")[, 2] %>% as.numeric()
}
qs
```
Now let's merge in our selected cohort.
```{r}
cohort <- rbind(
fread("no_weight_loss.csv", colClasses=c(public_client_id="character")),
fread("successful_weight_loss.csv", colClasses=c(public_client_id="character"))
)[since_baseline == 0]
cohort[, "subset" := "controls"]
cohort[weight_change_relative < 0, "subset" := "weight loss"]
cohort[, subset := factor(subset)]
data <- qs[cohort, on="public_client_id"]
```
Let's visualize what we have.
```{r, fig.width=16, fig.height=6}
library(pheatmap)
library(viridisLite)
df <- as.data.frame(data[, variables, with=F])
rownames(df) <- data$public_client_id
anns <- data.frame(BMI=data$bmi, subset=data$subset, row.names=data$public_client_id)
pheatmap(t(df), color=viridis(256), annotation_col = anns, show_colnames=FALSE)
# Save image
pheatmap(t(df), color=viridis(256), annotation_col = anns, show_colnames=FALSE,
filename="figures/diets.png", width=16, height=6, dpi=300)
```
Finally we can run the models for BMI.
```{r}
models <- lapply(variables, function(v){
model <- reformulate(c("age", "sex", "bmi"), v) %>% glm(data=data)
attr(model, "feature") <- v
model
})
bmi_tests <- lapply(models, function(m) {
coefs <- summary(m)$coefficients
data.table(
feature=attr(m, "feature"),
coef=coefs[4, 1],
se=coefs[4, 2],
t=coefs[4, 3],
p=coefs[4, 4]
)
}) %>% rbindlist()
bmi_tests[, "padj" := p.adjust(p, method="fdr")]
bmi_tests[, "variable" := "bmi"]
bmi_tests[order(p)]
```
And for weight loss.
```{r}
models <- lapply(variables, function(v){
model <- reformulate(c("age", "sex", "bmi", "subset"), v) %>%
glm(data=data)
attr(model, "feature") <- v
model
})
wl_tests <- lapply(models, function(m) {
coefs <- summary(m)$coefficients
data.table(
feature=attr(m, "feature"),
coef=coefs[5, 1],
se=coefs[5, 2],
t=coefs[5, 3],
p=coefs[5, 4]
)
}) %>% rbindlist()
wl_tests[, "padj" := p.adjust(p, method="fdr")]
wl_tests[, "variable" := "subset"]
wl_tests[order(p)]
```
```{r}
tests <- rbind(bmi_tests, wl_tests)
fwrite(tests[order(padj)], "data/tests_diet.csv")
```
```{r, fig.width=5, fig.height=3}
library(ggplot2)
theme_minimal() %>% theme_set()
wide <- dcast(tests, feature ~ variable, value.var=c("t", "padj"), fill=0)
wide[, "sig" := "none"]
wide[padj_bmi < 0.05, "sig" := "BMI"]
wide[padj_subset < 0.05, "sig" := "weight loss"]
wide[padj_bmi < 0.05 & padj_subset < 0.05, "sig" := "both"]
wide[, "sig" := factor(sig, levels=c("none", "BMI", "weight loss", "both"))]
ggplot(wide, aes(x=t_bmi, y=t_subset, color=sig)) +
geom_vline(xintercept = 0, color="gray30", lty="dashed") +
geom_hline(yintercept = 0, color="gray30", lty="dashed") +
geom_point() + stat_smooth(method="glm", aes(group = 1)) +
labs(x = "t statistic BMI", y = "t statistic weight loss") +
scale_color_manual(values=c(none = "black", BMI = "royalblue", `weight loss`="orange", both = "red"))
ggsave("figures/diet_t.png", dpi=300, width=5, height=3)
cor.test(~ t_bmi + t_subset, data=wide)
```
## Overall explained variance
```{r}
library(vegan)
m <- as(data[, variables, with=F], "matrix")
m[is.na(m)] <- min(as.numeric(m), na.rm=T)
perm <- adonis(m ~ age + sex + bmi + subset, data=data, method="euclidean")
perm
```