Skip to content

Latest commit

 

History

History
464 lines (355 loc) · 12.1 KB

sample_data.md

File metadata and controls

464 lines (355 loc) · 12.1 KB
title author date output
Analysis of 24Hr Movement Guideline Fibit Data
Daniel Fuller
26/07/2021
html_document
keep_md
true

24 Hour Movement Guidelines Data Analysis

Required packages

Data Description

There are a number of data types and frequencies. Below is a summary:

  1. Minute Frequency Data

    • heartrate_1min_merged
    • minuteCaloriesNarrow_merged
    • minuteIntensitiesNarrow_merged
    • minuteMETsNarrow_merged
    • minuteSleep_merged
    • minuteStepsNarrow_merged
  2. Daily Frequency Data

    • dailyCalories_merged
    • dailyIntensities_merged
    • dailySteps_merged
    • sleepDay_merged
    • heartRateZones_merged
      • Daily time in 4 heart rate zones (Out of Range, Fat Burn, Cardio, Peak)
  3. Other Frequency data

    • activitylogs_merged
      • An activity is recorded whenever the user inputs it
    • battery_merged
      • Battery level is recorded whenever there is a sync event
    • syncEvents_merged
      • All Sync events
    • 30secondSleepStages_merged
      • Sleep stages every 30 seconds.

To do

  1. Combine the minute level data
  2. Combine day level data
  3. Aggregate minute level data to the day and confirm it makes sense with day level
  4. Join battery and sync events to the minute level data
  5. Aggregate everything up to the day level

1. Combine the minute level data

Heart rate per minute

hr_data <- read_csv("Data/heartrate_1min_merged.csv")
## Rows: 1773649 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Id, Time
## dbl (1): Value
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
hr_data$time <- mdy_hms(hr_data$Time)
hr_data$Time <- NULL
colnames(hr_data) <- c("id", "heart_rate_bmp", "time")
summary(hr_data$heart_rate_bmp)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   36.00   65.00   76.00   77.73   88.00  204.00
ggplot(hr_data, aes(heart_rate_bmp)) + 
        geom_histogram() 
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Calories per minute

ee_data <- read_csv("Data/minuteCaloriesNarrow_merged.csv")
## Rows: 2039924 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Id, ActivityMinute
## dbl (1): Calories
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
ee_data$time <- mdy_hms(ee_data$ActivityMinute)
ee_data$ActivityMinute <- NULL
colnames(ee_data) <- c("id", "calories", "time")
summary(ee_data$calories)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.0000  0.9598  1.0586  1.5370  1.2920 16.8428
ggplot(ee_data, aes(calories)) + 
        geom_histogram() 
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

See fitbit description for calories https://dev.fitbit.com/build/reference/web-api/intraday/get-activity-intraday-by-date/

## Using BMR/EER Algorithms

If a user had no Fitbit tracker data for the specific day then the greater of Logged Activities + BMR (for minutes when there is no activity) and the calories calculated from the EER for that day (if EER enabled for this user's profile) are taken. In case, there was some data from the tracker for the specific day, that data where available is used and for time where data is unavailable, the BMR is used. If the total is less than 20% greater than BMR then the EER (cals < EER * 0.8) is used. EER never used to calculate calories for today.
Using BMR Formula

Fitbit uses the standard MD Mifflin-St Jeor equation:

9.99 * weightKg + 6.25*heightCm - 4.92*ageYears + s, where s is +5 for 
males
and -161 for female

EER Formula (TEE total energy expenditure)

The EER Formula is based on http://www.cdc.gov/pcd/issues/2006/oct/pdf/06_0034.pdf, which in turn is based on "Food and Nutrition Board. Dietary reference intakes for energy, carbohydrate, fiber, fat, fatty acids, cholesterol, protein, and amino acids (macronutrients). Washington (DC): National Academy Press; 2005." http://www.nap.edu/openbook.php?isbn=0309085373&page=204
MALE-based EER Formula:

TEE = 864 - 9.72 x age (years) + 1.0 x (14.2 x weight(kg) + 503 x height
(meters))

FEMALE-based EER Formula:

TEE = 387 - 7.31 x age (years) + 1.0 x (10.9 x weight(kg) + 660.7 x height
(meters))

Intensity per minute

intense_data <- read_csv("Data/minuteIntensitiesNarrow_merged.csv")
## Rows: 2039924 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Id, ActivityMinute
## dbl (1): Intensity
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
intense_data$time <- mdy_hms(intense_data$ActivityMinute)
intense_data$ActivityMinute <- NULL
colnames(intense_data) <- c("id", "intensity", "time")
intense_data$intensity <- as.factor(intense_data$intensity)
table(intense_data$intensity)
## 
##       0       1       2       3 
## 1705018  274378   28023   32505
ggplot(intense_data, aes(intensity)) + 
        geom_bar() 

This is a four category variable. Looks like it will match up with the heart rate zone data but not sure yet.

METS per minute

met_data <- read_csv("Data/minuteMETsNarrow_merged.csv")
## Rows: 2039924 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Id, ActivityMinute
## dbl (1): METs
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
met_data$time <- mdy_hms(met_data$ActivityMinute)
met_data$ActivityMinute <- NULL
colnames(met_data) <- c("id", "mets", "time")
summary(met_data$mets)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.00   10.00   10.00   15.31   13.00  137.00
ggplot(met_data, aes(mets)) + 
        geom_histogram() 
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Interesting. Will need to look into how this is calculated. 10 METS as the median and 15 METS as the mean is not right.

Sleep per minute

sleep_data <- read_csv("Data/minuteSleep_merged.csv")
## Rows: 578422 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Id, date
## dbl (2): value, logId
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
sleep_data$time <- mdy_hms(sleep_data$date)
sleep_data$date <- NULL
colnames(sleep_data) <- c("id", "sleep", "log_id", "time")
sleep_data$sleep <- as.factor(sleep_data$sleep)
table(sleep_data$sleep)
## 
##      1      2      3 
## 538051  36612   3759
ggplot(sleep_data, aes(sleep)) + 
        geom_bar() 

This is a three category variable. There is a lot of missing here so I'm guessing that these represent sleep stages or something with a null value meaning not sleeping.

Steps per minute

step_data <- read_csv("Data/minuteStepsNarrow_merged.csv")
## Rows: 2039924 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Id, ActivityMinute
## dbl (1): Steps
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
step_data$time <- mdy_hms(step_data$ActivityMinute)
step_data$ActivityMinute <- NULL
colnames(step_data) <- c("id", "steps", "time")
summary(step_data$steps)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   0.000   0.000   5.998   0.000 199.000
ggplot(step_data, aes(steps)) + 
        geom_histogram() 
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Joining the datasets

ee_intense <- full_join(ee_data, intense_data, by = c("id", "time"))
ee_intense_met <- full_join(ee_intense, met_data, by = c("id", "time"))
ee_intense_met_step <- full_join(ee_intense_met, step_data, by = c("id", "time"))
ee_intense_met_step_hr <- full_join(ee_intense_met_step, hr_data, by = c("id", "time"))
data <- full_join(ee_intense_met_step_hr, sleep_data, by = c("id", "time"))

## Filter out coordinator data
data <- filter(data, id != "Study Coordinator QU")

### Create a location variable
data$location <- substr(data$id, 1, 1)

### Create an intervention 
data <- data %>%
              mutate(intervention = case_when(
                          time < "2021-10-25 00:00:00" ~ "pre",
                          time >= "2021-10-25 00:00:00" & time < "2021-12-05 00:00:00" ~ "intervention",
                          time > "2021-12-05 00:00:00" ~ "post"
                        ))
table(data$intervention)
## 
## intervention          pre 
##      1498466       751117
### Recoding Sleep 
data <- data %>%
              mutate(sleep_yn = case_when(
                          sleep == 1 ~ "sleep",
                          sleep == 2 ~ "sleep",
                          sleep == 3 ~ "sleep",
                          TRUE ~ "awake"
                        ))
table(data$sleep_yn)
## 
##   awake   sleep 
## 1681054  568529
rm(ee_data, ee_intense, ee_intense_met, ee_intense_met_step, ee_intense_met_step_hr, hr_data, intense_data, met_data, sleep_data, step_data)

Quick descriptive stats

data$day <- day(data$time)
data$hour <- hour(data$time)

hourly_summary <- data %>%
          group_by(id, hour) %>%
            get_summary_stats(calories, heart_rate_bmp, steps)

daily_summary <- data %>%
      group_by(id, day) %>%
        get_summary_stats(calories, heart_rate_bmp, steps)

Heat Map over the study period per hour

ggplot(hourly_summary, aes(x = hour, y = variable, fill = mean)) +
  geom_tile() + 
  facet_wrap(~ id) +
  scale_fill_gradient(low = "grey20", high = "red") +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "none") 

Heat Map over the study period per day

ggplot(daily_summary, aes(x = day, y = variable, fill = mean)) +
  geom_tile() + 
  facet_wrap(~ id) +
  scale_fill_gradient(low = "grey20", high = "red") +
  labs(x = "", y = "") +
  theme_classic() +
  theme(legend.position = "none") 

2. Combine day level data