|
| 1 | +## ----setup, include = FALSE--------------------------------------------------- |
| 2 | +knitr::opts_chunk$set( |
| 3 | + collapse = TRUE, |
| 4 | + comment = "#>", |
| 5 | + fig.width = 7, |
| 6 | + fig.height = 5 |
| 7 | +) |
| 8 | + |
| 9 | +## ----eval=FALSE--------------------------------------------------------------- |
| 10 | +# library(Athlytics) |
| 11 | +# library(dplyr) |
| 12 | +# |
| 13 | +# # Load a single activity with stream data |
| 14 | +# activities <- load_local_activities("export_12345678.zip") |
| 15 | +# |
| 16 | +# # Parse stream data from a FIT/TCX file |
| 17 | +# # (Assuming you have stream data - this is a conceptual example) |
| 18 | +# stream_data <- parse_activity_file("path/to/activity.fit") |
| 19 | +# |
| 20 | +# # Flag quality issues |
| 21 | +# flagged_data <- flag_quality( |
| 22 | +# stream_data, |
| 23 | +# sport = "Run", |
| 24 | +# hr_range = c(30, 220), # Valid HR range |
| 25 | +# pw_range = c(0, 1500), # Valid power range |
| 26 | +# max_run_speed = 7.0, # Max speed (m/s) ≈ 2:23/km pace |
| 27 | +# max_hr_jump = 10, # Max HR change per second |
| 28 | +# max_pw_jump = 300, # Max power change per second |
| 29 | +# min_steady_minutes = 20, # Min duration for steady-state |
| 30 | +# steady_cv_threshold = 8 # CV% threshold for steady-state |
| 31 | +# ) |
| 32 | +# |
| 33 | +# # View quality summary |
| 34 | +# summary_stats <- quality_summary(flagged_data) |
| 35 | +# print(summary_stats) |
| 36 | + |
| 37 | +## ----eval=FALSE--------------------------------------------------------------- |
| 38 | +# # Load activities |
| 39 | +# activities <- load_local_activities("export_12345678.zip") |
| 40 | +# |
| 41 | +# # Calculate ACWR using Rolling Average (traditional) |
| 42 | +# acwr_ra <- calculate_acwr_ewma( |
| 43 | +# activities, |
| 44 | +# activity_type = "Run", |
| 45 | +# method = "ra", |
| 46 | +# acute_period = 7, |
| 47 | +# chronic_period = 28, |
| 48 | +# load_metric = "duration_mins" |
| 49 | +# ) |
| 50 | +# |
| 51 | +# # Calculate ACWR using EWMA |
| 52 | +# acwr_ewma <- calculate_acwr_ewma( |
| 53 | +# activities, |
| 54 | +# activity_type = "Run", |
| 55 | +# method = "ewma", |
| 56 | +# half_life_acute = 3.5, # Acute load half-life (days) |
| 57 | +# half_life_chronic = 14, # Chronic load half-life (days) |
| 58 | +# load_metric = "duration_mins" |
| 59 | +# ) |
| 60 | +# |
| 61 | +# # Compare the two methods |
| 62 | +# library(ggplot2) |
| 63 | +# ggplot() + |
| 64 | +# geom_line(data = acwr_ra, aes(x = date, y = acwr_smooth), |
| 65 | +# color = "blue", size = 1, linetype = "solid") + |
| 66 | +# geom_line(data = acwr_ewma, aes(x = date, y = acwr_smooth), |
| 67 | +# color = "red", size = 1, linetype = "dashed") + |
| 68 | +# labs(title = "ACWR: Rolling Average vs EWMA", |
| 69 | +# subtitle = "Blue = RA (7:28d) | Red = EWMA (3.5:14d half-life)", |
| 70 | +# x = "Date", y = "ACWR") + |
| 71 | +# theme_minimal() |
| 72 | + |
| 73 | +## ----eval=FALSE--------------------------------------------------------------- |
| 74 | +# # Calculate EWMA with 95% confidence bands |
| 75 | +# acwr_ewma_ci <- calculate_acwr_ewma( |
| 76 | +# activities, |
| 77 | +# activity_type = "Run", |
| 78 | +# method = "ewma", |
| 79 | +# half_life_acute = 3.5, |
| 80 | +# half_life_chronic = 14, |
| 81 | +# load_metric = "duration_mins", |
| 82 | +# ci = TRUE, # Enable confidence intervals |
| 83 | +# B = 200, # Bootstrap iterations |
| 84 | +# block_len = 7, # Block length (days) for bootstrap |
| 85 | +# conf_level = 0.95 # 95% CI |
| 86 | +# ) |
| 87 | +# |
| 88 | +# # Plot with confidence bands |
| 89 | +# ggplot(acwr_ewma_ci, aes(x = date)) + |
| 90 | +# geom_ribbon(aes(ymin = acwr_lower, ymax = acwr_upper), |
| 91 | +# fill = "gray70", alpha = 0.5) + |
| 92 | +# geom_line(aes(y = acwr_smooth), color = "black", size = 1) + |
| 93 | +# geom_hline(yintercept = c(0.8, 1.3), linetype = "dotted", color = "green") + |
| 94 | +# geom_hline(yintercept = 1.5, linetype = "dotted", color = "red") + |
| 95 | +# labs(title = "ACWR with 95% Confidence Bands", |
| 96 | +# subtitle = "Gray band = bootstrap confidence interval", |
| 97 | +# x = "Date", y = "ACWR") + |
| 98 | +# theme_minimal() |
| 99 | + |
| 100 | +## ----eval=FALSE--------------------------------------------------------------- |
| 101 | +# # Load data for multiple athletes |
| 102 | +# athlete1 <- load_local_activities("athlete1_export.zip") %>% |
| 103 | +# mutate(athlete_id = "athlete1", sex = "M", age_band = "25-35") |
| 104 | +# |
| 105 | +# athlete2 <- load_local_activities("athlete2_export.zip") %>% |
| 106 | +# mutate(athlete_id = "athlete2", sex = "F", age_band = "25-35") |
| 107 | +# |
| 108 | +# athlete3 <- load_local_activities("athlete3_export.zip") %>% |
| 109 | +# mutate(athlete_id = "athlete3", sex = "M", age_band = "35-45") |
| 110 | +# |
| 111 | +# # Combine |
| 112 | +# cohort_data <- bind_rows(athlete1, athlete2, athlete3) |
| 113 | +# |
| 114 | +# # Calculate ACWR for each athlete |
| 115 | +# cohort_acwr <- cohort_data %>% |
| 116 | +# group_by(athlete_id) %>% |
| 117 | +# group_modify(~ calculate_acwr_ewma(.x, method = "ewma")) %>% |
| 118 | +# ungroup() %>% |
| 119 | +# left_join( |
| 120 | +# cohort_data %>% select(athlete_id, sex, age_band) %>% distinct(), |
| 121 | +# by = "athlete_id" |
| 122 | +# ) |
| 123 | + |
| 124 | +## ----eval=FALSE--------------------------------------------------------------- |
| 125 | +# # Calculate cohort reference by sport and sex |
| 126 | +# reference <- cohort_reference( |
| 127 | +# cohort_acwr, |
| 128 | +# metric = "acwr_smooth", |
| 129 | +# by = c("sport", "sex"), # Group by sport and sex |
| 130 | +# probs = c(0.05, 0.25, 0.5, 0.75, 0.95), # Percentiles |
| 131 | +# min_athletes = 3 # Minimum athletes per group |
| 132 | +# ) |
| 133 | +# |
| 134 | +# # View reference structure |
| 135 | +# head(reference) |
| 136 | + |
| 137 | +## ----eval=FALSE--------------------------------------------------------------- |
| 138 | +# # Extract one athlete's data |
| 139 | +# individual <- cohort_acwr %>% |
| 140 | +# filter(athlete_id == "athlete1") |
| 141 | +# |
| 142 | +# # Plot with cohort reference |
| 143 | +# p <- plot_with_reference( |
| 144 | +# individual = individual, |
| 145 | +# reference = reference %>% filter(sex == "M"), # Match athlete's group |
| 146 | +# metric = "acwr_smooth", |
| 147 | +# bands = c("p25_p75", "p05_p95", "p50") |
| 148 | +# ) |
| 149 | +# |
| 150 | +# print(p) |
| 151 | + |
| 152 | +## ----eval=FALSE--------------------------------------------------------------- |
| 153 | +# library(Athlytics) |
| 154 | +# library(dplyr) |
| 155 | +# library(ggplot2) |
| 156 | +# |
| 157 | +# # --- Step 1: Load multi-athlete cohort --- |
| 158 | +# cohort_files <- c("athlete1_export.zip", "athlete2_export.zip", "athlete3_export.zip") |
| 159 | +# cohort_data <- lapply(cohort_files, function(file) { |
| 160 | +# load_local_activities(file) %>% |
| 161 | +# mutate(athlete_id = tools::file_path_sans_ext(basename(file))) |
| 162 | +# }) %>% bind_rows() |
| 163 | +# |
| 164 | +# # --- Step 2: Quality control (conceptual - for stream data) --- |
| 165 | +# # If you have stream data, flag quality before calculating metrics |
| 166 | +# # cohort_data <- cohort_data %>% |
| 167 | +# # group_by(athlete_id) %>% |
| 168 | +# # mutate(quality_score = calculate_quality_score(streams)) |
| 169 | +# |
| 170 | +# # --- Step 3: Calculate ACWR with EWMA + CI --- |
| 171 | +# cohort_acwr <- cohort_data %>% |
| 172 | +# group_by(athlete_id) %>% |
| 173 | +# group_modify(~ calculate_acwr_ewma( |
| 174 | +# .x, |
| 175 | +# method = "ewma", |
| 176 | +# half_life_acute = 3.5, |
| 177 | +# half_life_chronic = 14, |
| 178 | +# load_metric = "duration_mins", |
| 179 | +# ci = TRUE, |
| 180 | +# B = 100 # Reduced for speed in example |
| 181 | +# )) %>% |
| 182 | +# ungroup() |
| 183 | +# |
| 184 | +# # --- Step 4: Calculate cohort reference --- |
| 185 | +# reference <- cohort_reference( |
| 186 | +# cohort_acwr, |
| 187 | +# metric = "acwr_smooth", |
| 188 | +# by = c("sport"), |
| 189 | +# probs = c(0.05, 0.25, 0.5, 0.75, 0.95), |
| 190 | +# min_athletes = 3 |
| 191 | +# ) |
| 192 | +# |
| 193 | +# # --- Step 5: Visualize individual with confidence + cohort --- |
| 194 | +# individual <- cohort_acwr %>% filter(athlete_id == "athlete1") |
| 195 | +# |
| 196 | +# p <- plot_with_reference(individual, reference, metric = "acwr_smooth") + |
| 197 | +# # Add individual's confidence bands |
| 198 | +# geom_ribbon(data = individual, |
| 199 | +# aes(x = date, ymin = acwr_lower, ymax = acwr_upper), |
| 200 | +# fill = "blue", alpha = 0.2) |
| 201 | +# |
| 202 | +# print(p) |
| 203 | +# |
| 204 | +# # --- Step 6: Export for statistical analysis --- |
| 205 | +# write.csv(cohort_acwr, "cohort_acwr_with_ci.csv", row.names = FALSE) |
| 206 | +# write.csv(reference, "cohort_reference.csv", row.names = FALSE) |
| 207 | + |
| 208 | +## ----------------------------------------------------------------------------- |
| 209 | +sessionInfo() |
| 210 | + |
0 commit comments