Code
library(tidyverse)
library(pins)
library(nanoparquet)
library(googledrive)
ds <- board_gdrive(path = as_id("1OZlphhu6vYm1A2Bm2-zD7a4luS5nGWgS")) %>%
pin_read("imu_raw_samples")library(tidyverse)
library(pins)
library(nanoparquet)
library(googledrive)
ds <- board_gdrive(path = as_id("1OZlphhu6vYm1A2Bm2-zD7a4luS5nGWgS")) %>%
pin_read("imu_raw_samples")usable_data <- ds %>% count(id, session) %>% mutate(usable_hours = n/3600)
recording_duration <- ds %>% group_by(id, session) %>%
mutate(recording_hours = as.numeric(time_leg_off - time_leg_on)) %>%
slice_head(n = 1) %>% ungroup
usable_data %>% left_join(recording_duration) %>%
get_summary_stats(usable_hours, recording_hours, show = c("mean", "sd","min","max")) %>%
flextable() %>% autofit()variable |
n |
mean |
sd |
min |
max |
|---|---|---|---|---|---|
usable_hours |
23 |
6.605 |
1.526 |
4.432 |
9.466 |
recording_hours |
23 |
10.605 |
1.426 |
7.533 |
12.400 |
usable_data %>%
ggplot(aes(x = usable_hours)) +
geom_histogram() +
scale_x_binned(name = "Hours", limits = c(2,14))
recording_duration %>%
ggplot(aes(x = recording_hours)) +
geom_histogram() +
scale_x_binned(name = "Hours", limits = c(2,14))ds_sum <- ds %>% group_by(id, session) %>%
mutate(total_samples = n()) %>% group_by(id, session, pos) %>%
summarize(pos_n = n(), total_samples = mean(total_samples)) %>% ungroup
ds_sum <- ds_sum %>% complete(nesting(id, session), pos, fill = list(pos_n = 0, total_samples = 1))
ds_sum$pos_prop = ds_sum$pos_n/ds_sum$total_samples*100
ds_sum$session = as.numeric(ds_sum$session)
ds_sum %>% group_by(pos, session) %>%
get_summary_stats(pos_prop, show = c("mean", "sd","min","max")) %>%
select(-variable) %>% flextable() %>% autofit()session |
pos |
n |
mean |
sd |
min |
max |
|---|---|---|---|---|---|---|
1 |
Supine |
12 |
24.645 |
16.808 |
8.322 |
58.046 |
2 |
Supine |
7 |
12.872 |
8.251 |
3.956 |
26.262 |
3 |
Supine |
4 |
12.305 |
11.304 |
1.922 |
25.343 |
1 |
Prone |
12 |
21.262 |
13.823 |
2.168 |
53.195 |
2 |
Prone |
7 |
9.581 |
4.239 |
3.257 |
15.002 |
3 |
Prone |
4 |
12.236 |
2.590 |
10.127 |
15.904 |
1 |
Sitting |
12 |
32.451 |
13.768 |
12.819 |
53.703 |
2 |
Sitting |
7 |
45.813 |
11.220 |
32.400 |
60.394 |
3 |
Sitting |
4 |
35.892 |
8.042 |
25.139 |
44.118 |
1 |
Standing |
12 |
10.361 |
8.775 |
1.326 |
33.114 |
2 |
Standing |
7 |
20.350 |
7.713 |
8.784 |
31.995 |
3 |
Standing |
4 |
30.361 |
7.806 |
20.757 |
38.755 |
1 |
Held |
12 |
11.282 |
4.832 |
3.704 |
20.746 |
2 |
Held |
7 |
11.383 |
5.555 |
3.105 |
19.508 |
3 |
Held |
4 |
9.206 |
5.645 |
3.273 |
15.978 |
ggplot(ds_sum, aes(x = session, y = pos_prop, color = pos)) +
stat_summary(position = position_dodge(.1)) +
stat_summary(geom = "line", position = position_dodge(.1)) +
ylim(0,101) + ylab("% of time") + xlab("Session") +
scale_x_continuous(breaks = 1:3, labels = c("7 mo","9 mo","11 mo"), limits = c(0.5,3.5)) +
scale_color_manual(values = pal, name ="") +
theme(legend.position = "bottom")
ggplot(ds_sum, aes(x = session, y = pos_prop, fill = pos)) +
stat_summary(geom = "bar", position = "stack") +
ylim(0,101) + ylab("% of time") + xlab("Session") +
scale_x_continuous(breaks = 1:3, labels = c("7 mo","9 mo","11 mo"), limits = c(0.5,3.5)) +
scale_fill_manual(values = pal, name ="") +
theme(legend.position = "bottom")ggplot(ds_sum, aes(x = session, y = pos_prop, color = pos, group = id)) +
facet_wrap(~ pos) +
geom_point() + geom_line() +
ylim(0,100) + ylab("% of time") + xlab("Session") +
scale_x_continuous(breaks = 1:3, labels = c("7 mo","9 mo","11 mo"), limits = c(0.5,3.5)) +
scale_color_manual(values = pal, name ="") +
theme(legend.position = "none")---
title: "Summaries"
fig-dpi: 300
lightbox: auto
execute:
cache: false
format:
html:
toc: true
code-fold: true
code-tools: true
code-link: true
knitr:
opts_chunk:
out.width: "100%"
---
```{r}
#| echo: false
#| warning: false
#| error: false
library(tidyverse)
library(hms)
library(rstatix)
library(flextable)
library(pins)
pal <- c("#F0E442","#009E73","#56B4E9", "#E69F00","#0072B2") %>% set_names(c("Standing", "Sitting", "Prone", "Supine", "Held"))
theme_update(text = element_text(size = 12),
axis.text.x = element_text(size = 12, color = "black"),
axis.title.x = element_text(size = 14),
axis.text.y = element_text(size = 12, color = "black"),
axis.title.y = element_text(size = 14),
panel.background = element_blank(),panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_blank(),
axis.ticks.length=unit(.25, "cm"),
legend.key = element_rect(fill = "white"))
board <- board_folder("/Volumes/padlab/study_sensorsinperson/data_processed/datasets/")
data_version <- board %>% pin_meta("imu_raw_samples")
ds <- board %>% pin_read("imu_raw_samples")
```
| Last updated: `r round(now())` using pinned data version from: `r round(data_version$created)`
| Users can load data by running (requires one-time google drive authentication):
```{r}
#| eval: false
library(tidyverse)
library(pins)
library(nanoparquet)
library(googledrive)
ds <- board_gdrive(path = as_id("1OZlphhu6vYm1A2Bm2-zD7a4luS5nGWgS")) %>%
pin_read("imu_raw_samples")
```
### Usable data
```{r}
#| warning: false
#| label: Usable-data
#| tbl-cap: Usable data and total recording length in hours
usable_data <- ds %>% count(id, session) %>% mutate(usable_hours = n/3600)
recording_duration <- ds %>% group_by(id, session) %>%
mutate(recording_hours = as.numeric(time_leg_off - time_leg_on)) %>%
slice_head(n = 1) %>% ungroup
usable_data %>% left_join(recording_duration) %>%
get_summary_stats(usable_hours, recording_hours, show = c("mean", "sd","min","max")) %>%
flextable() %>% autofit()
```
```{r}
#| warning: false
#| label: Usable-data-histograms
#| layout-ncol: 2
#| fig-cap:
#| - "Usable hours after exclusions"
#| - "Recording length in hours"
usable_data %>%
ggplot(aes(x = usable_hours)) +
geom_histogram() +
scale_x_binned(name = "Hours", limits = c(2,14))
recording_duration %>%
ggplot(aes(x = recording_hours)) +
geom_histogram() +
scale_x_binned(name = "Hours", limits = c(2,14))
```
### Infant Position Frequency by Age (Overall)
```{r}
#| warning: false
#| label: position-table-age
#| tbl-cap: Position summary statistics
ds_sum <- ds %>% group_by(id, session) %>%
mutate(total_samples = n()) %>% group_by(id, session, pos) %>%
summarize(pos_n = n(), total_samples = mean(total_samples)) %>% ungroup
ds_sum <- ds_sum %>% complete(nesting(id, session), pos, fill = list(pos_n = 0, total_samples = 1))
ds_sum$pos_prop = ds_sum$pos_n/ds_sum$total_samples*100
ds_sum$session = as.numeric(ds_sum$session)
ds_sum %>% group_by(pos, session) %>%
get_summary_stats(pos_prop, show = c("mean", "sd","min","max")) %>%
select(-variable) %>% flextable() %>% autofit()
```
```{r}
#| warning: false
#| label: "frequency-by-age-overall"
#| layout-ncol: 2
#| fig-cap:
#| - "Position means and SE"
#| - "Position distribution"
ggplot(ds_sum, aes(x = session, y = pos_prop, color = pos)) +
stat_summary(position = position_dodge(.1)) +
stat_summary(geom = "line", position = position_dodge(.1)) +
ylim(0,101) + ylab("% of time") + xlab("Session") +
scale_x_continuous(breaks = 1:3, labels = c("7 mo","9 mo","11 mo"), limits = c(0.5,3.5)) +
scale_color_manual(values = pal, name ="") +
theme(legend.position = "bottom")
ggplot(ds_sum, aes(x = session, y = pos_prop, fill = pos)) +
stat_summary(geom = "bar", position = "stack") +
ylim(0,101) + ylab("% of time") + xlab("Session") +
scale_x_continuous(breaks = 1:3, labels = c("7 mo","9 mo","11 mo"), limits = c(0.5,3.5)) +
scale_fill_manual(values = pal, name ="") +
theme(legend.position = "bottom")
```
### Infant Position Frequency by Age (Individual)
```{r}
#| out-width: "90%"
#| warning: false
#| label: "frequency-by-age-individual"
ggplot(ds_sum, aes(x = session, y = pos_prop, color = pos, group = id)) +
facet_wrap(~ pos) +
geom_point() + geom_line() +
ylim(0,100) + ylab("% of time") + xlab("Session") +
scale_x_continuous(breaks = 1:3, labels = c("7 mo","9 mo","11 mo"), limits = c(0.5,3.5)) +
scale_color_manual(values = pal, name ="") +
theme(legend.position = "none")
```