|
| 1 | +--- |
| 2 | +title: "bsyncr Functionality Demonstration" |
| 3 | +output: html_notebook |
| 4 | +--- |
| 5 | + |
| 6 | +Create a .Renviron file in the project folder and set your NOAA_TOKEN |
| 7 | + |
| 8 | +e.g., |
| 9 | + |
| 10 | +NOAA_TOKEN=xyz123 |
| 11 | + |
| 12 | + |
| 13 | +```{r setup, include=FALSE} |
| 14 | +# set the working directory to the project folder, not the location of this file. |
| 15 | +setwd("..") |
| 16 | +
|
| 17 | +# install the dependencies |
| 18 | +source("./setup_environment.R") |
| 19 | +
|
| 20 | +# Load required libraries |
| 21 | +library(xml2) |
| 22 | +library(rnoaa) |
| 23 | +library(lubridate) |
| 24 | +library(dplyr) |
| 25 | +library(ggplot2) |
| 26 | +
|
| 27 | +
|
| 28 | +# Source the utility functions |
| 29 | +source("./R/bsync_utils.R") |
| 30 | +
|
| 31 | +# get root path |
| 32 | +root_path <- getwd() |
| 33 | +
|
| 34 | +# Ensure NOAA token is set |
| 35 | +NOAA_TOKEN <- Sys.getenv('NOAA_TOKEN') |
| 36 | +if (NOAA_TOKEN == "") { |
| 37 | + stop("Missing NOAA token env var: NOAA_TOKEN") |
| 38 | +} |
| 39 | +options(noaakey = NOAA_TOKEN) |
| 40 | +``` |
| 41 | + |
| 42 | +```{r} |
| 43 | +
|
| 44 | +# Path to the test file |
| 45 | +# bsync path from root path |
| 46 | +bsync_filepath <- file.path(root_path, "tests", "data", "ex_bsync.xml") |
| 47 | +
|
| 48 | +
|
| 49 | +baseline_scenario_id <- "Scenario-bsyncr" |
| 50 | +bsync_doc <- xml2::read_xml(bsync_filepath) %>% |
| 51 | + bsyncr::bs_stub_scenarios(linked_building_id = "My-Fav-Building", baseline_id = baseline_scenario_id) |
| 52 | +
|
| 53 | +baseline_xpath <- sprintf("//auc:Scenario[@ID = '%s']", baseline_scenario_id) |
| 54 | +sc_baseline <- xml2::xml_find_first(bsync_doc, baseline_xpath) |
| 55 | +not_used <- sc_baseline %>% bsyncr::bs_stub_derived_model(dm_id = "DerivedModel-bsyncr", |
| 56 | + dm_period = "Baseline") |
| 57 | +
|
| 58 | +b_df <- bsyncr::bs_parse_nmecr_df(bsync_doc, insert_weather_data = TRUE) |
| 59 | +``` |
| 60 | + |
| 61 | +```{r} |
| 62 | +# Create an SLR model |
| 63 | +model <- nmecr::model_with_SLR(b_df, nmecr::assign_model_inputs(regression_type = "SLR")) |
| 64 | +
|
| 65 | +model_df <- model$training_data %>% |
| 66 | + tidyr::gather(key = "variable", value = "value", c("eload", "model_fit")) |
| 67 | +
|
| 68 | +print(model_df) |
| 69 | +
|
| 70 | +# add in the linear regression line from the model results, need to |
| 71 | +# confirm, but it looks like model is in BTU and °C |
| 72 | +intercept = model$model$coefficients[["(Intercept)"]] / 3.41214 # btu to kwh |
| 73 | +# Model is in °C, so convert to F. |
| 74 | +slope = model$model$coefficients[["temp"]] * 9/5 # °C to °F |
| 75 | +
|
| 76 | +ggplot2::ggplot(model_df, aes(x = temp, y = value)) + |
| 77 | + geom_point(aes(color = variable), data=model_df[model_df$variable == "eload",]) + |
| 78 | + geom_line(aes(color = variable), data=model_df[model_df$variable == "model_fit",]) + |
| 79 | + geom_abline(intercept = intercept, slope = slope, color = "red", linetype = "dashed") + |
| 80 | + xlab("Temperature") + |
| 81 | + scale_y_continuous(name = "Energy Data & Model Fit (kWh)", labels = scales::comma) + |
| 82 | + theme_minimal() + |
| 83 | + theme(legend.position = "bottom") + |
| 84 | + theme(legend.title = element_blank()) |
| 85 | +``` |
0 commit comments