Skip to content

Instantly share code, notes, and snippets.

View baogorek's full-sized avatar

Ben Ogorek baogorek

  • spencer Health Solutions, Inc.
  • Raleigh, NC
  • X @benogorek
View GitHub Profile
library(ggplot2)
# Files are at ...
folder_path <- "c:/devl/swimming" # Update accordingly
perf_params <- get_perf_params(world_record_perf = 1.8,
able_bodied_perf = 0.5, limit = 2.0,
type = "jumping")
get_perf_in_cp <- function(perf, a, b, limit) {
get_perf_params <- function(world_record_perf, able_bodied_perf, limit,
type = "running") {
# sum of squares around fixed point
rss <- function(a, b, world_record_perf, able_bodied_perf, limit) {
(1000 - b * log(a / (world_record_perf - limit))) ** 2 +
(0 - b * log(a / (able_bodied_perf - limit))) ** 2
}
stopifnot(type %in% c("running", "jumping"))
a_starting <- ifelse(type == "running", 5, -5)
library(ggplot2)
# Figure 1
plot_df <- rbind(
data.frame(day = days_grid, level = phi, type = "true \u03D5(t)"),
data.frame(day = days_grid, level = eta_star, type = "best fit \u03B7*(t)")
)
ggplot(plot_df, aes(x = day, y = level, color = type, linetype = type)) +
geom_line(size = 2) +
@baogorek
baogorek / cumulative-impact-part-ii-close-the-loop.R
Last active March 5, 2019 14:17
Code Block 4. Closing the loop.
get_eta_hat <- function(t_seq) {
spline_vars_grid <- predict(my_spline, newx = t_seq)
spline_vars_grid <- cbind(1, spline_vars_grid)
eta_hat <- spline_vars_grid %*% coef(spline_reg)[-1]
as.numeric(eta_hat)
}
convolve_with_fn <- function(training, n, impulse_fn) {
sum(training[1:(n - 1)] * impulse_fn((n - 1):1))
}
@baogorek
baogorek / spline-convolution-estimation.R
Last active March 5, 2019 14:15
Code Block 3: Introducing a spline-based method for modeling cumulative impact.
library(splines)
my_spline <- ns(1:259, Boundary.knots = c(1, 259), knots = c(14, 40, 100))
z_vars <- list()
for (n in 1:nrow(train_df)) {
spline_pred <- predict(my_spline, newx = (n - 1):1)
spline_vars <- colSums(spline_pred * train_df$w[1:(n - 1)]) # convolution
spline_const <- sum(train_df$w[1:(n - 1)])
z_vars[[n]] <- c(spline_const, spline_vars)
}
@baogorek
baogorek / combined-impulse-response-ff.R
Last active March 5, 2019 14:15
Code Block 2. Comparing the true theoretical impulse response with B-spline approximation.
library(splines)
exp_decay <- function(t, tau) {
exp(-t / tau)
}
get_true_phi <- function(t) {
0.07 * exp_decay(t, 60) - 0.27 * exp_decay(t, 13)
}
days_grid <- 1:259
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@baogorek
baogorek / cumulative-impact-part-ii-catching-up.R
Last active March 5, 2019 14:16
Code Block 1. Simulating H.T.’s training and performance data using the fitness-fatigue model from Physiology.
train_df <- data.frame(day = 1:259, day_of_week = 0:258 %% 7)
train_df$period <- ifelse(train_df$day <= 147, "build-up", "competition")
train_df$w <- with(train_df, w <-
-24 * (day_of_week == 0) +
12 * (day_of_week == 1) +
8 * (day_of_week == 2) +
0 * (day_of_week == 3) +
6 * (day_of_week == 4) +
-8 * (day_of_week == 5) +
6 * (day_of_week == 6))
@baogorek
baogorek / fitness-fatigue-optim.R
Last active December 24, 2018 16:58
Nonlinear optimization to fit the fitness-fatigue model
# Recover parameters using non-linear regression
rss <- function(theta) {
int <- theta[1] # performance baseline
k1 <- theta[2] # fitness weight
k2 <- theta[3] # fatigue weight
tau1 <- theta[4] # fitness decay
tau2 <- theta[5] # fatigue decay
fitness <- sapply(1:nrow(train_df),
function(n) convolve_training(train_df$w, n, tau1))
@baogorek
baogorek / create-fitness-and-fatigue.R
Last active December 23, 2018 15:35
Fitness and fatigue variables created from intensities w in data frame train_df
convolve_training <- function(training, n, tau) {
sum(training[1:(n - 1)] * exp_decay((n - 1):1, tau))
}
fitness <- sapply(1:nrow(train_df),
function(n) convolve_training(train_df$w, n, 60))
fatigue <- sapply(1:nrow(train_df),
function(n) convolve_training(train_df$w, n, 13))