-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep1_join_epw.Rmd
383 lines (261 loc) · 12.6 KB
/
step1_join_epw.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
---
title: "Cluster and Analyze"
author: "Noah Klammer"
date: "4/27/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```
## Clear global env and report
```{r include=FALSE}
rm(list = ls())
gc()
```
# Intro
In this instance, I am using Zone Ideal Air Loads extracted from an EnergyPlus `in.idf` with the weather file `USA_CA_Los.Angeles.Intl.AP.722950_TMY3.ddy`. The Zone Ideal Load Air System ("ILAS") numerical `colnames` were `str_replace`d to match the `Name` field of the `Zone` object in the predecesor file `0_RVESO.Rmd`. Loads were normalized by zone floor area. In this file we will import both Ideal Load Air System data and weather data via the connector package `eplusr`. We will visualize this data for exploration. We will create report figures. We will attempt clustering of the ideal air loads for a subset of 8760 observations. Note that the nth observation in the `hour` variable corresponds to the average of the period from (n-1)th hour to nth hour.
## Import EPW
The R package `eplusr` contains useful functions and an `Epw` object for easily reading and writing weather data in the .epw file format.
```{r include=FALSE}
library(eplusr)
epw <- eplusr::Epw$new("USA_CA_Los.Angeles.Intl.AP.722950_TMY3.epw")
# good info
epw$typical_extreme_period()
paste0("The number of time intervals per hour is ",epw$interval(),".")
paste(colnames(epw$data()), collapse = ", ") # get available variables here
epw$data() %>%
select(month, day, hour, dry_bulb_temperature)
```
## Import data in Rda form
The responsible model for this data resides in the project folder `MORmod_mf/osw_mf_thesis/*/run`.
```{r import, message=FALSE, warning=FALSE}
load(file = "ilas_nodhw.Rda") # ilas_nodhw
load(file = "ilas_dhw_novar.Rda") # ilas_dhw_novar
load(file = "hvac_dhw_novar.Rda") # hvac_dhw_novar
load(file = "hvac_dhw_var.Rda") # hvac_dhw_var
```
### Check that column names match
```{r}
uniq <- unique(
c(
colnames(ilas_nodhw),
colnames(ilas_dhw_novar),
colnames(hvac_dhw_novar),
colnames(hvac_dhw_var)
))
all(uniq %in% colnames(ilas_nodhw))
all(uniq %in% colnames(ilas_dhw_novar))
all(uniq %in% colnames(hvac_dhw_novar))
all(uniq %in% colnames(hvac_dhw_var))
```
# Visualizations
Under this section I have code that opens the `grDevices` connection and writes custom figures to bitmap files in the containing folder. The image outputs are shown below.
## Visualize regressive trends
```{r Type Function, include=FALSE}
# Define function for categorizing zones by "Apartment", "Stairs", and "Corridor".
type_f <- function (zone_string) {
if (grepl("BDRM",zone_string)) {
return("Apartment")
}
else if (grepl("STAIR",zone_string)) {
return("Stairs")
}
else { # is corridor
return("Corridor")
}
}
```
```{r include=FALSE}
db_temp <- epw$data()$dry_bulb_temperature # degC
# SET PLOT DATA HERE
df <- cbind(db_temp,ilas_nodhw)
```
```{r message=FALSE}
df_gg <- cbind(db_temp, ilas_nodhw)
ggplot(df_gg, aes(x=db_temp,y=`0_BDRM_1_3`)) + geom_jitter(width = 0.6, height = 0, alpha = 1/20) + geom_smooth(se=F) + theme_classic(base_size = 16) + scale_y_continuous("Ideal Air Loads Cooling (J)") + scale_x_continuous("Drybulb Temperature (°C)") + ggtitle("Studio Bedroom 1, Third Floor")
```
```{r message=FALSE}
df_gg <- cbind(db_temp, hvac_dhw_var)
ggplot(df_gg, aes(x=db_temp, y=`0_BDRM_1_3`)) + geom_jitter(width = 0.6, height = 0, alpha = 1/20) + geom_smooth(se=F) + theme_classic(base_size = 16) + scale_y_continuous("Energy Use (J)",labels = scales::scientific_format()) + scale_x_continuous("Drybulb Temperature (°C)") + ggtitle("Studio Bedroom 1, Third Floor")
```
### ggplot facet_wrap
#### ilas_nodhw
```{r include=FALSE}
# how many zone variables?
x <- df %>%
select(-c(db_temp, day, hour, month))# %>% select(c(1:20))
num_var <- x %>% length() # 54 zone variables
# create db_temp in length of 54 x 8760
db_list <- rep(df$db_temp, times = num_var)
db_list %>% length() == num_var*8760
db_list[1+8760*0] == db_list[1+8760*4]
# concatenate all variables except db_temp
# use rbind() instead of append()
values <- vector(mode = "numeric")
zones <- vector(mode = "character")
type <- vector(mode = "character")
for (col in 1:ncol(x)) {
values <- append(values, x[,col])
zones <- append(zones, rep(colnames(x)[col], times = 8760))
type <- append(type, rep(type_f(colnames(x)[col]), times = 8760))
}
df <- data.frame(db_temp = as.double(db_list), values = as.integer(values), zones = as.factor(zones), type = as.factor(type))
```
```{r message=FALSE, warning=FALSE}
#png(filename = "multi_smooth_ilas_nodhw.png", height = 480, width = 950, units = "px")
# have to tell aes that the variable is a discrete `factor`
p <- ggplot(df, aes(x = db_temp, y = values, color = type)) + geom_smooth(se = T, method = "gam", show.legend = T) + coord_cartesian(xlim = c(14,20))
p + facet_wrap(~zones) + scale_x_continuous(limits = quantile(df$db_temp,c(0.25,0.75))) + labs(x = "Drybulb Temperature (°C)", y = "Cooling Load per Area (J/m^2)") + theme(strip.background = element_blank(), strip.text.x = element_blank(), axis.title.y = element_text(margin = margin(r=12)))
#dev.off()
```
#### ilas_dhw_novar
```{r include=FALSE}
db_temp <- epw$data()$dry_bulb_temperature # degC
# SET PLOT DATA HERE
df <- cbind(db_temp,ilas_dhw_novar)
# how many zone variables?
x <- df %>%
select(-c(db_temp, day, hour, month))# %>% select(c(1:20))
num_var <- x %>% length() # 54 zone variables
# create db_temp in length of 54 x 8760
db_list <- rep(df$db_temp, times = num_var)
db_list %>% length() == num_var*8760
db_list[1+8760*0] == db_list[1+8760*4]
# concatenate all variables except db_temp
# use rbind() instead of append()
values <- vector(mode = "numeric")
zones <- vector(mode = "character")
type <- vector(mode = "character")
for (col in 1:ncol(x)) {
values <- append(values, x[,col])
zones <- append(zones, rep(colnames(x)[col], times = 8760))
type <- append(type, rep(type_f(colnames(x)[col]), times = 8760))
}
df <- data.frame(db_temp = as.double(db_list), values = as.integer(values), zones = as.factor(zones), type = as.factor(type))
```
```{r}
# have to tell aes that the variable is a discrete `factor`
p <- ggplot(df, aes(x = db_temp, y = values, color = type)) + geom_smooth(se = T, method = "glm", show.legend = T) + coord_cartesian(xlim = c(14,20))
p + facet_wrap(~zones) + scale_x_continuous(limits = quantile(df$db_temp,c(0.25,0.75))) + labs(x = "Drybulb Temperature (°C)", y = "Cooling Load per Area (J/m^2)") + theme(strip.background = element_blank(), strip.text.x = element_blank(), axis.title.y = element_text(margin = margin(r=12)))
```
#### hvac_dhw_novar
Try and jitter the data in Corridors and Stairs.
```{r}
hvac_dhw_novar$CORRIDOR_2 <- jitter(hvac_dhw_novar$CORRIDOR_2)
hvac_dhw_novar$CORRIDOR_3 <- jitter(hvac_dhw_novar$CORRIDOR_3)
hvac_dhw_novar$CORRIDOR_4 <- jitter(hvac_dhw_novar$CORRIDOR_4)
hvac_dhw_novar$STAIRWELL_1_2 <- jitter(hvac_dhw_novar$STAIRWELL_1_2)
hvac_dhw_novar$STAIRWELL_2_2 <- jitter(hvac_dhw_novar$STAIRWELL_2_2)
STAIRWELL_1_3 <- jitter(hvac_dhw_novar$STAIRWELL_1_3)
STAIRWELL_2_3 <- jitter(hvac_dhw_novar$STAIRWELL_2_3)
STAIRWELL_1_4 <- jitter(hvac_dhw_novar$STAIRWELL_1_4)
hvac_dhw_novar$STAIRWELL_2_4 <- jitter(hvac_dhw_novar$STAIRWELL_2_4)
```
```{r include=FALSE}
db_temp <- epw$data()$dry_bulb_temperature # degC
# SET PLOT DATA HERE
df <- cbind(db_temp, hvac_dhw_novar)
# how many zone variables?
x <- df %>%
select(-c(db_temp, day, hour, month))# %>% select(c(1:20))
num_var <- x %>% length() # 54 zone variables
# create db_temp in length of 54 x 8760
db_list <- rep(df$db_temp, times = num_var)
db_list %>% length() == num_var*8760
db_list[1+8760*0] == db_list[1+8760*4]
# concatenate all variables except db_temp
# use rbind() instead of append()
values <- vector(mode = "numeric")
zones <- vector(mode = "character")
type <- vector(mode = "character")
for (col in 1:ncol(x)) {
values <- append(values, x[,col])
zones <- append(zones, rep(colnames(x)[col], times = 8760))
type <- append(type, rep(type_f(colnames(x)[col]), times = 8760))
}
df <- data.frame(db_temp = as.double(db_list), values = as.integer(values), zones = as.factor(zones), type = as.factor(type))
```
```{r}
# have to tell aes that the variable is a discrete `factor`
p <- ggplot(df, aes(x = db_temp, y = values, color = type)) + geom_smooth(se = T, method = "glm", show.legend = T) + coord_cartesian(xlim = c(14,20))
p + facet_wrap(~zones) + scale_x_continuous(limits = quantile(df$db_temp,c(0.25,0.75))) + labs(x = "Drybulb Temperature (°C)", y = "Energy Use per Area (J/m^2)") + theme(strip.background = element_blank(), strip.text.x = element_blank(), axis.title.y = element_text(margin = margin(r=12)))
```
#### hvac_dhw_var
```{r include=FALSE}
db_temp <- epw$data()$dry_bulb_temperature # degC
# SET PLOT DATA HERE
df <- cbind(db_temp,hvac_dhw_var)
# how many zone variables?
x <- df %>%
select(-c(db_temp, day, hour, month)) #%>% select(c(1,48,54))
num_var <- x %>% length() # 54 zone variables
# create db_temp in length of 54 x 8760
db_list <- rep(df$db_temp, times = num_var)
db_list %>% length() == num_var*8760
db_list[1+8760*0] == db_list[1+8760*4]
# concatenate all variables except db_temp
# use rbind() instead of append()
values <- vector(mode = "numeric")
zones <- vector(mode = "character")
type <- vector(mode = "character")
for (col in 1:ncol(x)) {
values <- append(values, x[,col])
zones <- append(zones, rep(colnames(x)[col], times = 8760))
type <- append(type, rep(type_f(colnames(x)[col]), times = 8760))
}
df <- data.frame(db_temp = as.double(db_list), values = as.integer(values), zones = as.factor(zones), type = as.factor(type))
```
```{r}
# have to tell aes that the variable is a discrete `factor`
p <- ggplot(df, aes(x = db_temp, y = values, color = type)) + geom_smooth(se = T, method = "glm", show.legend = T) + coord_cartesian(xlim = c(14,20))
p + facet_wrap(~zones) + scale_x_continuous(limits = quantile(df$db_temp,c(0.25,0.75))) + labs(x = "Drybulb Temperature (°C)", y = "Energy Use per Area (J/m^2)") + theme(strip.background = element_blank(), strip.text.x = element_blank(), axis.title.y = element_text(margin = margin(r=12)))
```
### faceted ggplot for select 4 days
```{r RUN FOR 4 IMAGE, eval=FALSE, include=FALSE}
library(ggplot2)
# Jan, Apr, Aug, Oct
x <- sel_days_epw
# set resolution
png("4days.png", units = "in", width = 8, height = 5, res = 500)
# have to tell aes that the variable is a discrete `factor`
p <- ggplot(x, aes(x = hour, y = dry_bulb_temperature, color = factor(month))) + geom_smooth(se = F, span = 0.3)
# new facet label names for month variable
month.labs <- c("January 21","April 21","August 21","October 21")
names(month.labs) <- months_of_interest
p + facet_grid(rows = vars(month), labeller = labeller(month = month.labs)) + scale_color_discrete(name = "Month", labels = month.name[months_of_interest]) + scale_x_continuous(name = "Hour", breaks = c(1,6,12,18,24)) + theme_bw() + theme(legend.position = "none", axis.title.y = element_text(margin = margin(r=10))) + labs(y = "Dry Bulb Temperature (°C)")
dev.off()
```
Dry bulb temperature versus hour for four select days in the typical Los Angeles meteorological year: 
### Faceted climate ggplot for 365 days
```{r eval=FALSE, include=FALSE}
# make unique day index 1..365
d <- nrow(ilas_nodhw)/24
d_ind <- rep(1:d, each = 24)
# alternative 2 with iteration
# c_vec <- vector(mode = "numeric", nrow(ilas_nodhw)) # instantiate
# for (i in length(c_vec)) { # control flow
# if (logic) {i <<- i + 1} # iterator
# }
```
```{r RUN FOR 365 IMAGE, eval=FALSE, include=FALSE}
library(ggplot2)
# Jan 31 days
sel_days_epw <- cbind(d_ind, epw$data()) %>%
select(d_ind, month, day, hour, dry_bulb_temperature) %>%
filter(month %in% c(1:12))
x <- sel_days_epw
# set resolution
png("365.png", units = "in", width = 8, height = 5, res = 500)
# custom color palette named vector
colorv <- rep(
RColorBrewer::brewer.pal(8, name = "Dark2"),
length.out = 12)
names(colorv) <- 1:12
# have to tell aes that the variable is a discrete `factor`
p <- ggplot(x, aes(x = hour, y = dry_bulb_temperature, color = factor(month))) + geom_smooth(se = F, span = 0.35)
p + facet_wrap(~d_ind) + scale_color_manual(name = "Month", labels = month.abb, values = colorv) + scale_x_discrete(labels = NULL) + scale_y_discrete(labels = NULL) + theme(strip.background = element_blank(), strip.text.x = element_blank(), axis.title.y = element_text(margin = margin(r=12))) + labs(x = NULL, y = "Drybulb temperature")
dev.off()
```
Dry bulb temperature versus time of the full 365 days of the typical meteorological year for Los Angeles: 
<br><br><br>