## Load in required packages import_package = function(package_name){ ## function to suppress messages and warnings during package load suppressWarnings(suppressMessages(require(package_name, character.only = TRUE))) } import_package("nmecr") import_package("ggplot2") eload_data <- readRDS("Data/Processed Energy Consumption - multiyear.rds") temp_data <- readRDS("Data/Processed Temperature - multiyear.rds") # Align energy and temperature data at daily intervals energy_temp_df <- nmecr::create_dataframe(eload_data, temp_data, start_date = "2020-03-31", end_date = "2021-04-01", convert_to_data_interval = "Daily") # Plot energy use against time options(repr.plot.width=16, repr.plot.height=8) temperatureColor <- "#69b3a2" priceColor <- rgb(0.2, 0.6, 0.9, 1) energy_temp_df %>% ggplot2::ggplot(aes(x = time)) + geom_line(aes(y=eload), size=1.5, color=temperatureColor) + geom_line( aes(y=temp*200), size=1.5, color=priceColor) + scale_y_continuous( name = "Energy Consumption (kWh)", sec.axis = sec_axis(~.*1/200, name="Temperature (deg F)")) + theme( axis.title.y = element_text(color = temperatureColor, size=20), axis.title.y.right = element_text(color = priceColor, size=20), text = element_text(size = 20)) + xlab("") # Plot energy use against temperature options(repr.plot.width=16, repr.plot.height=8) energy_temp_df[complete.cases(energy_temp_df), ] %>% ggplot2::ggplot(aes(x = temp, y = eload)) + geom_point() + scale_x_continuous(limits = c(0, NA)) + scale_y_continuous(limits = c(0, NA)) + theme(text = element_text(size = 20)) + labs(x = "Temperature (deg F)", y = "Energy Consumption (kWh)") # Plot energy use against temperature - zoomed in options(repr.plot.width=16, repr.plot.height=8) energy_temp_df[complete.cases(energy_temp_df), ] %>% ggplot2::ggplot(aes(x = temp, y = eload)) + geom_point() + theme(text = element_text(size = 20)) + labs(x = "Temperature (deg F)", y = "Energy Consumption (kWh)") # create the two models and the dataframe for plotting four_parameter_model <- nmecr::model_with_CP(training_data = energy_temp_df, model_input_options = nmecr::assign_model_inputs(regression_type = "4P")) TOWT_model <- nmecr::model_with_TOWT(training_data = energy_temp_df, model_input_options = nmecr::assign_model_inputs(regression_type = "TOWT")) actual_modeled_df <- four_parameter_model$training_data %>% select(-c("model_fit")) %>% mutate('4P_fit' = four_parameter_model$training_data$model_fit) %>% mutate('TOWT_fit' = TOWT_model$training_data$model_fit) baseline_scatter_df <- tidyr::pivot_longer(actual_modeled_df, cols = c("eload", "4P_fit", "TOWT_fit")) generate_scatter_plot <- function(data) { # to generate scatter plot data %>% ggplot2::ggplot(aes(x = temp, y = value, color = name)) + geom_point() + scale_x_continuous(limits = c(0, NA)) + scale_y_continuous(limits = c(0, NA)) + theme(text = element_text(size = 20), legend.title = element_blank()) + labs(x = "Temperature (deg F)", y = "Energy Consumption (kWh)") } options(repr.plot.width=16, repr.plot.height=8) generate_scatter_plot(baseline_scatter_df) ## Model Summary Stats generate_summary_stats <- function(model1, model2) { model1_stats <- nmecr::calculate_summary_statistics(model1) model1_unc <- nmecr::calculate_savings_and_uncertainty(modeled_object = model1, model_summary_statistics = model1_stats, confidence_level = 90) model1_metrics <- dplyr::bind_cols(model1_stats, model1_unc) model2_stats <- nmecr::calculate_summary_statistics(model2) model2_unc <- nmecr::calculate_savings_and_uncertainty(modeled_object = model2, model_summary_statistics = model2_stats, confidence_level = 90) model2_metrics <- dplyr::bind_cols(model2_stats, model2_unc) metrics <- dplyr::bind_rows(model1_metrics, model2_metrics) metrics <- metrics[, c(1:5,9:10)] names(metrics) <- c("R2", "Adj. R2", "CVRMSE%", "NDBE%", "NMBE%", "Savings Uncertainty @ 10% Savings", "Savings Fraction for 50% Uncertainty") metrics$`NDBE%` <- as.numeric(metrics$`NDBE%`) metrics$`NMBE%` <- as.numeric(metrics$`NMBE%`) metrics <- metrics %>% mutate("Algorithm" = c("4P", "TOWT")) metrics <- metrics %>% select(c("Algorithm", everything())) return(metrics) } generate_summary_stats(four_parameter_model, TOWT_model) energy_temp_df <- nmecr::create_dataframe(eload_data, temp_data, start_date = "2020-03-31", end_date = "2021-04-01", convert_to_data_interval = "Daily") prediction_energy_temp_df <- nmecr::create_dataframe(eload_data, temp_data, start_date = "2019-03-31", end_date = "2020-04-01", convert_to_data_interval = "Daily") CP_prediction <- nmecr::calculate_model_predictions(training_data = energy_temp_df, prediction_data = prediction_energy_temp_df, modeled_object = four_parameter_model) TOWT_prediction <- nmecr::calculate_model_predictions(training_data = energy_temp_df, prediction_data = prediction_energy_temp_df, modeled_object = TOWT_model) all_predictions_df <- CP_prediction %>% select(-c("predictions")) %>% mutate('4P_predictions' = CP_prediction$predictions) %>% mutate('TOWT_predictions' = TOWT_prediction$predictions) prediction_scatter_df <- tidyr::pivot_longer(all_predictions_df, cols = c("eload", "4P_predictions", "TOWT_predictions")) options(repr.plot.width=16, repr.plot.height=8) generate_scatter_plot(prediction_scatter_df) calculate_prediction_errors <- function(data) { CP_prediction_error <- sum(data$eload - data$`4P_predictions`, na.rm = T)/ sum(data$eload, na.rm = T) TOWT_prediction_error <- sum(data$eload - data$TOWT_predictions, na.rm = T)/ sum(data$eload, na.rm = T) CP_prediction_error <- paste0(round(100*CP_prediction_error, 2), "%") TOWT_prediction_error <- paste0(round(100*TOWT_prediction_error, 2), "%") return(list(CP_prediction_error, TOWT_prediction_error)) } prediction_errors <- calculate_prediction_errors(all_predictions_df) message("Prediction Error using the Four Parameter model is ", prediction_errors[1], " while the error for the TOWT model is ", prediction_errors[2]) occupancy_data <- readRDS("Data/Processed Occupancy Data - multiyear.rds") energy_temp_df_upd <- nmecr::create_dataframe(eload_data, temp_data, additional_independent_variables = occupancy_data, additional_variable_aggregation = c(median), start_date = "2020-03-31", end_date = "2021-04-01", convert_to_data_interval = "Daily") prediction_energy_temp_df_upd <- nmecr::create_dataframe(eload_data, temp_data, additional_independent_variables = occupancy_data, additional_variable_aggregation = c(median), start_date = "2019-03-31", end_date = "2020-04-01", convert_to_data_interval = "Daily") four_parameter_model_upd <- nmecr::model_with_CP(training_data = energy_temp_df_upd, model_input_options = nmecr::assign_model_inputs(regression_type = "4P")) TOWT_model_upd <- nmecr::model_with_TOWT(training_data = energy_temp_df_upd, model_input_options = nmecr::assign_model_inputs(regression_type = "TOWT")) actual_modeled_df_upd <- four_parameter_model_upd$training_data %>% select(-c("model_fit")) %>% mutate('4P_fit' = four_parameter_model_upd$training_data$model_fit) %>% mutate('TOWT_fit' = TOWT_model_upd$training_data$model_fit) baseline_scatter_df_upd <- tidyr::pivot_longer(actual_modeled_df_upd, cols = c("eload", "4P_fit", "TOWT_fit")) options(repr.plot.width=16, repr.plot.height=8) generate_scatter_plot(baseline_scatter_df_upd) CP_prediction_upd <- nmecr::calculate_model_predictions(training_data = energy_temp_df_upd, prediction_data = prediction_energy_temp_df_upd, modeled_object = four_parameter_model_upd) TOWT_prediction_upd <- nmecr::calculate_model_predictions(training_data = energy_temp_df_upd, prediction_data = prediction_energy_temp_df_upd, modeled_object = TOWT_model_upd) all_predictions_df_upd <- CP_prediction_upd %>% select(-c("predictions")) %>% mutate('4P_predictions' = CP_prediction_upd$predictions) %>% mutate('TOWT_predictions' = TOWT_prediction_upd$predictions) prediction_scatter_df_upd <- tidyr::pivot_longer(all_predictions_df_upd, cols = c("eload", "4P_predictions", "TOWT_predictions")) prediction_errors <- calculate_prediction_errors(all_predictions_df_upd) message("Prediction Error using the Four Parameter model is ", prediction_errors[1], " while the error for the TOWT model is ", prediction_errors[2]) generate_summary_stats(four_parameter_model_upd, TOWT_model_upd)