This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) + |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |