Last active
May 18, 2020 03:04
-
-
Save jroberayalas/c1ef42ad08ae8d98f482c07d0111f304 to your computer and use it in GitHub Desktop.
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
results <- vector(mode = 'numeric', length = nrow(prophetGrid)) | |
# Search best parameters | |
for (i in seq_len(nrow(prophetGrid))) { | |
# Select the parameters to try | |
parameters <- prophetGrid[i, ] | |
if (parameters$growth == 'logistic') { | |
train$cap <- parameters$capacity | |
} | |
# Initialize Prophet model | |
m <- prophet(growth = parameters$growth, | |
holidays = holidays, | |
seasonality.prior.scale = parameters$seasonality_prior_scale, | |
changepoint.prior.scale = parameters$changepoint_prior_scale, | |
holidays.prior.scale = parameters$holidays_prior_scale, | |
yearly.seasonality = TRUE, | |
fit = FALSE) | |
# Add exogenous features | |
m <- add_regressor(m, 'weather', prior.scale = parameters$regressor_prior_scale) | |
m <- add_regressor(m, 'atemp', prior.scale = parameters$regressor_prior_scale) | |
m <- add_regressor(m, 'humidity', prior.scale = parameters$regressor_prior_scale) | |
m <- add_regressor(m, 'windspeed', prior.scale = parameters$regressor_prior_scale) | |
# Fit the model | |
m <- fit.prophet(m, train) | |
if (parameters$growth == 'logistic') { | |
valid$cap <- parameters$capacity | |
} | |
# NOTE: There's a problem in function names with library(caret) | |
forecast <- predict(m, bind_rows(train, valid)) | |
# Compute MAE and save it | |
results[i] <- forecast::accuracy(forecast[ymd(forecast$ds) %in% valid$ds, ]$yhat, valid$y)[ , 'MAE'] | |
} | |
prophetGrid <- cbind(prophetGrid, results) | |
# Choose the hyperparameters that produced the best (minimum) MAE | |
best_params <- prophetGrid[prophetGrid$results == min(results), ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment