Skip to content

Commit fa15243

Browse files
committed
add in the lineal regression for SLR
1 parent 48695bd commit fa15243

File tree

5 files changed

+104
-3
lines changed

5 files changed

+104
-3
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
.DS_Store
22
.python-version
3+
.Renviron
4+
.Rhistory
5+
.RData
36

47
node_modules
58

9+
tests/*.zip
10+
tests/*.nb.html
11+
tests/*_results/
12+
613
vignettes/*.html
714
vignettes/*.R
815
vignettes/output
16+
.Rproj.user

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ Rscript -e "testthat::test_dir('tests')"
3636

3737
## Releasing new version
3838

39+
- Open `bysync.Rproj` in RStudio
40+
- In RStudio, format all the R files by running the following commands in RStudio
41+
42+
```R
43+
install.packages("styler")
44+
styler::style_dir()
45+
```
46+
3947
- Create a branch with the prepared release change log.
4048
- Make sure the rnoaa and nmecr versions in `setup_environment.R` and ` are correct.
4149
- Update version in bsync.RProj and DESCRIPTION to the next correct semantic version

bsyncr.Rproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
Version: 0.1
1+
Version: 1.0
2+
ProjectId: b4573bbf-3288-46a6-89f8-fe2dc7103016
23

34
RestoreWorkspace: Default
45
SaveWorkspace: Default

setup_environment.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ cat(getwd(), "\n\n")
77

88
# Install required packages if not already installed
99
required_packages <- c(
10-
"remotes", "crayon", "dplyr", "tidyr", "crul", "xml2", "testthat", "anytime", "lubridate", "segmented", "xts", "zoo",
11-
"ggplot2", "scales", "XML", "rappdirs", "gridExtra", "isdparser", "geonames", "hoardr", "data.table"
10+
"remotes", "crayon", "dplyr", "tidyr", "crul", "xml2", "testthat", "anytime", "lubridate", "segmented", "xts", "zoo", "ggplot2", "scales", "XML", "rappdirs", "gridExtra", "isdparser", "geonames", "hoardr", "data.table"
1211
)
1312

1413
cat("Checking and installing required packages...\n")

tests/bsyncr_example.Rmd

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)