Skip to content

Instantly share code, notes, and snippets.

@baogorek
Last active March 5, 2019 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baogorek/8d148e498c286df3aabffbc25c2f0d60 to your computer and use it in GitHub Desktop.
Save baogorek/8d148e498c286df3aabffbc25c2f0d60 to your computer and use it in GitHub Desktop.
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))
}
cumulative_impact_eta <- sapply(1:nrow(train_df),
function(n) convolve_with_fn(train_df$w, n,
get_eta_hat))
cumulative_impact_phi <- sapply(1:nrow(train_df),
function(n) convolve_with_fn(train_df$w, n,
get_true_phi))
train_aug_df$perf_hat_convo_eta <- coef(spline_reg)[1] + cumulative_impact_eta
train_aug_df$perf_hat_fitness_fatigue <- 496 + cumulative_impact_phi
# Removing end effects, convolving with spline is same as regression fitted vals
all(abs(train_aug_df$perf_hat_convo_eta - train_aug_df$perf_hat)[-1] < .001)
days_grid <- 1:259
eta_hat <- get_eta_hat(days_grid)
phi <- get_true_phi(days_grid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment