-
Notifications
You must be signed in to change notification settings - Fork 0
/
Building_Energy_Predictions.R
319 lines (241 loc) · 8.98 KB
/
Building_Energy_Predictions.R
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
#Predicting Buildings Energy Efficiency
#loading libraries
library(tidyverse)
library(dplyr)
library(ggplot2)
library(ggthemes)
library(caret)
library(elasticnet)
library(knitr)
library(matrixStats)
#loading the data
energy <- read.csv("../Building_Energy_Efficiency/ENB2012_data.csv")
#looking at the data
head(energy)
summary(energy)
class(energy)
str(energy)
names(energy)
#renaming column names to match the data set description
colnames(energy) <- c('Relative_Compactness', 'Surface_Area', 'Wall_Area',
'Roof_Area', 'Overall_Height', 'Orientation',
'Glazing_Area', 'Glazing_Area_Distribution',
'Heating_Load', 'Cooling_Load')
#Data cleaning
#finding NAs in the data
colSums(is.na(energy))
#checking to see if there are any blank observations
colSums(energy == "")
#scaling the features
energy_scaled <- scale(energy[,1:8])
energy[,1:8] <- scale(energy[,1:8])
#Data Visualization
#boxplot of the variables
boxplot(energy)
#Density of heating load
energy %>% ggplot(aes(Heating_Load)) +
geom_density(aes(fill = "red", color = "red")) +
xlab("heating lab") +
ggtitle("Density of Heating Load") +
theme_economist() +
theme(legend.position = "none")
#Density of Cooling load
energy %>% ggplot(aes(Cooling_Load)) +
geom_density(aes(fill = "blue", color = "blue")) +
xlab("cooling lab") +
ggtitle("Density of Cooling Load") +
theme_economist() +
theme(legend.position = "none")
#scatter plot of surface area and heating load
energy %>% ggplot(aes(Surface_Area,Heating_Load)) +
geom_point(aes(color = "red")) +
xlab("surface area") +
ylab("heating load")+
ggtitle("Surface area and heat") +
theme_economist() +
theme(legend.position = "none")
#scatter plot of roof area and heating load
energy %>% ggplot(aes(Roof_Area,Heating_Load)) +
geom_point(aes(color = "red")) +
xlab("roof area") +
ylab("heating load")+
ggtitle("Roof area and heat") +
theme_economist() +
theme(legend.position = "none")
#scatter plot of compactness and heating load
energy %>% ggplot(aes(Relative_Compactness,Heating_Load)) +
geom_point(aes(color = "red")) +
xlab("relative compactness") +
ylab("heating load") +
ggtitle("Relative Compactness and Heating Load") +
theme_economist() +
theme(legend.position = "none")
#scatter plot of surface area and cooling load
energy %>% ggplot(aes(Surface_Area,Cooling_Load)) +
geom_point(aes(color = "blue")) +
xlab("surface area") +
ylab("cooling load")+
ggtitle("Surface area and cooling") +
theme_economist() +
theme(legend.position = "none")
#scatter plot of roof area and cooling load
energy %>% ggplot(aes(Roof_Area,Cooling_Load)) +
geom_point(aes(color = "blue")) +
xlab("roof area") +
ylab("cooling load")+
ggtitle("Roof area and cooling") +
theme_economist() +
theme(legend.position = "none")
#scatter plot of compactness and cooling load
energy %>% ggplot(aes(Relative_Compactness,Cooling_Load)) +
geom_point(aes(color = "blue")) +
xlab("relative compactness") +
ylab("cooling load") +
ggtitle("Relative Compactness and Cooling Load") +
theme_economist() +
theme(legend.position = "none")
#Models
#first we will be predicting the heat load
#setting seed
set.seed(1, sample.kind = "Rounding")
#splitting data into test and train data sets
test_index <- createDataPartition(energy$Heating_Load, times = 1, p = 0.2, list = FALSE)
test <- energy[test_index,]
train <- energy[-test_index,]
#checking to see if the test and train have similar outcomes
mean(train$Heating_Load)
mean(test$Heating_Load)
#Will be using k-fold cross validation on all the algorithms
#creating the k-fold parameters, k is 10
set.seed(7, sample.kind = "Rounding")
control <- trainControl(method = "cv", number = 10, p = .9)
#linear regression for heating load
#training the model using train set
set.seed(9, sample.kind = "Rounding")
train_lm <- train(Heating_Load ~ .,
data = train,
method = "lm",
tuneGrid = data.frame(intercept = seq(-10,10,2)),
trControl = control)
#veiwing traing results
train_lm
#ploting training results
plot(train_lm)
#creating predictions
lm_preds <- predict(train_lm, test)
#calulating the RMSE for the linear regression model
lm_rmse_hl <- RMSE(lm_preds,test$Heating_Load)
#ridge regression for heating load
#training the model using train set
set.seed(10, sample.kind = "Rounding")
train_ridge <- train(Heating_Load ~ .,
data = train,
method = "ridge",
tuneGrid = data.frame(lambda = seq(.001,.005,.001)),
trControl = control)
#viewing training results
train_ridge
#plotting training results
plot(train_ridge)
#creating predictions
ridge_preds <- predict(train_ridge,test)
#creating RMSE for ridge regression model
ridge_rmse_hl <- RMSE(ridge_preds, test$Heating_Load)
#Random Forest for heating load
#training the model using training set
set.seed(12, sample.kind = "Rounding")
train_rf <- train(Heating_Load ~ .,
data = train,
method = "rf",
tuneGrid = data.frame(mtry = seq(2,10,2)),
trControl = control)
#veiwing training results
train_rf
#plotting training results
plot(train_rf)
#creating predictions
rf_preds <- predict(train_rf,test)
#creating RMSE for random forest model
rf_rmse_hl <- RMSE(rf_preds,test$Heating_Load)
#Ensemble for heating load
heating_preds <- data.frame("lm" = lm_preds,
"ridge" = ridge_preds,
"rf" = rf_preds)
ensemble_preds <- rowMeans(heating_preds)
heating_preds$ensemble <- ensemble_preds
ensemble_rmse_hl <- RMSE(ensemble_preds,test$Heating_Load)
#Now we will make predictions for the cooling load
#linear regression for cooling load
#training the model using train set
set.seed(9, sample.kind = "Rounding")
train_lm <- train(Cooling_Load ~ .,
data = train,
method = "lm",
tuneGrid = data.frame(intercept = seq(-10,10,2)),
trControl = control)
#veiwing traing results
train_lm
#ploting training results
plot(train_lm)
#creating predictions
lm_preds <- predict(train_lm, test)
#calulating the RMSE for the linear regression model
lm_rmse_cl <- RMSE(lm_preds,test$Cooling_Load)
#ridge regression for cooling load
#training the model using train set
set.seed(10, sample.kind = "Rounding")
train_ridge <- train(Cooling_Load ~ .,
data = train,
method = "ridge",
tuneGrid = data.frame(lambda = seq(.001,.005,.001)),
trControl = control)
#viewing training results
train_ridge
#plotting training results
plot(train_ridge)
#creating predictions
ridge_preds <- predict(train_ridge,test)
#creating RMSE for ridge regression model
ridge_rmse_cl <- RMSE(ridge_preds, test$Cooling_Load)
#Random Forest for cooling load
#training the model using training set
set.seed(12, sample.kind = "Rounding")
train_rf <- train(Cooling_Load ~ .,
data = train,
method = "rf",
tuneGrid = data.frame(mtry = seq(2,10,2)),
trControl = control)
#veiwing training results
train_rf
#plotting training results
plot(train_rf)
#creating predictions
rf_preds <- predict(train_rf,test)
#creating RMSE for random forest model
rf_rmse_cl <- RMSE(rf_preds,test$Cooling_Load)
#Ensemble for cooling load
cooling_preds <- data.frame("lm" = lm_preds,
"ridge" = ridge_preds,
"rf" = rf_preds)
ensemble_preds <- rowMeans(cooling_preds)
cooling_preds$ensemble <- ensemble_preds
ensemble_rmse_cl <- RMSE(ensemble_preds,test$Cooling_Load)
#Results
results_hl <- data.frame(Model = c("Linear Regression",
"Ridge Regression",
"Random Forest",
"Ensemble"),
RMSE = c(lm_rmse_hl,
ridge_rmse_hl,
rf_rmse_hl,
ensemble_rmse_hl))
kable(results_hl)
results_cl <- data.frame(Model = c("Linear Regression",
"Ridge Regression",
"Random Forest",
"Ensemble"),
RMSE = c(lm_rmse_cl,
ridge_rmse_cl,
rf_rmse_cl,
ensemble_rmse_cl))
kable(results_cl)