[Project] Power Demand Forecast
Published in
6 min readAug 28, 2020
Subject : Prediction of Power Energy Consumption in Apartment and Shopping Districts in Korea
Language : R
Data : ‘국내 특정 지역 아파트와 상가의 전력에너지 사용량’ 데이터
1. Data preprocessing
2. EDA
3. Modeling
- 사용 모델 : ARIMA, Smoothing, Neural Network (TLFN, RNN)
setwd("C:\\r_temp\\result")
test <- read.csv("testfile.csv")
real <- read.csv("real_test.csv")library(tidyverse)
library(TTR)
library(forecast)
library(urca)
library(tseries)# 7, 37, 44, 61, 91, 94, 139, 143, 195, 235, 254
# --------------------------------------------------------------------------------------------------------------------------------------------# X125
tt <- test$X125[3470:8737]
tt <- ts(tt)decompose(tt, type = "multiplicative")models <- list (
mod_arima = auto.arima(tt, ic='aicc', stepwise=FALSE),
mod_exponential = ets(tt, ic='aicc', restrict=FALSE),
mod_neural = nnetar(tt, p=24, size=25),
mod_tbats = tbats(tt, ic='aicc', seasonal.periods=12),
mod_bats = bats(tt, ic='aicc', seasonal.periods=12),
mod_sts = StructTS(tt)
)forecasts <- lapply(models, forecast, 12)
forecasts$naive <- naive(ap_ts, 12)par(mfrow=c(4, 2))
par(mar=c(2, 2, 1.5, 2), xaxs='i', yaxs='i')for(f in forecasts){
plot(f, main="", xaxt="n")
lines(tt, col='red')
}forecasts$mod_arima
forecasts$mod_exponential
forecasts$mod_neural
forecasts$mod_tbats
forecasts$mod_bats
forecasts$mod_sts# best
au_arima <- c(0.2034369, 0.2116403, 0.2098514, 0.2055449, 0.2041134, 0.2040530, 0.2049593, 0.2057770, 0.2058540, 0.2055973, 0.2053625, 0.2052813)
sum(abs(real$X125 - au_arima)/(abs(au_arima) + abs(real$X125))/2) # 0.3817sts_ariam <- c(0.1960361, 0.1956386, 0.1952412, 0.1948437, 0.1944463, 0.1940488, 0.1936514, 0.1932540, 0.1928565, 0.1924591, 0.1920616, 0.1916642)
sum(abs(real$X125 - sts_ariam)/(abs(sts_ariam) + abs(real$X125))/2) # 0.2733# --------------------------------------------------------------------------------------------------------------------------------------------# X7
tt <- test$X7[7005:8737]
tt <- ts(tt)decompose(tt, type = "multiplicative")models <- list (
mod_arima = auto.arima(tt, ic='aicc', stepwise=FALSE),
mod_exponential = ets(tt, ic='aicc', restrict=FALSE),
mod_neural = nnetar(tt, p=24, size=25),
mod_tbats = tbats(tt, ic='aicc', seasonal.periods=12),
mod_bats = bats(tt, ic='aicc', seasonal.periods=12),
mod_sts = StructTS(tt)
)forecasts <- lapply(models, forecast, 12)
forecasts$naive <- naive(ap_ts, 12)par(mfrow=c(4, 2))
par(mar=c(2, 2, 1.5, 2), xaxs='i', yaxs='i')for(f in forecasts){
plot(f, main="", xaxt="n")
lines(tt, col='red')
}forecasts$mod_arima
forecasts$mod_exponential
forecasts$mod_neural
forecasts$mod_tbats
forecasts$mod_bats
forecasts$mod_stsneural <- c(1.1986645, 0.9640165, 0.8746475, 0.9872308, 0.8112574, 1.0184473, 1.0489223, 1.5837020, 1.7516105, 2.3812536, 2.3783198, 2.6844003)
sum(abs(real$X7 - neural)/(abs(neural) + abs(real$X7))/2) # 0.9367tbats <- c(0.8491555, 0.8736663, 0.9109060, 0.9575447, 1.0388069, 1.1976505, 1.3982813, 1.5429842, 1.6549981, 1.8567963, 2.1315271, 2.2883796)
sum(abs(real$X7 - tbats)/(abs(tbats) + abs(real$X7))/2) # 0.9454# --------------------------------------------------------------------------------------------------------------------------------------------# X37
tt <- test$X37[7005:8737]
tt <- ts(tt)models <- list (
mod_arima = auto.arima(tt, ic='aicc', stepwise=FALSE),
mod_exponential = ets(tt, ic='aicc', restrict=FALSE),
mod_neural = nnetar(tt, p=24, size=25),
mod_tbats = tbats(tt, ic='aicc', seasonal.periods=12),
mod_bats = bats(tt, ic='aicc', seasonal.periods=12),
mod_sts = StructTS(tt)
)forecasts <- lapply(models, forecast, 12)
forecasts$naive <- naive(ap_ts, 12)par(mfrow=c(4, 2))
par(mar=c(2, 2, 1.5, 2), xaxs='i', yaxs='i')for(f in forecasts){
plot(f, main="", xaxt="n")
lines(tt, col='red')
}forecasts$mod_arima
forecasts$mod_exponential
forecasts$mod_neural
forecasts$mod_tbats
forecasts$mod_bats
forecasts$mod_sts# best
neural <- c(9.309044, 7.527764, 4.935818, 3.411409, 3.477580, 2.845029, 2.523524, 2.626405, 2.295592, 2.353317, 2.536511, 2.568289)
sum(abs(real$X37 - neural)/(abs(neural) + abs(real$X37))/2) # 0.4152# --------------------------------------------------------------------------------------------------------------------------------------------# X44
tt <- test$X44[7005:8737]
tt <- ts(tt)models <- list (
mod_arima = auto.arima(tt, ic='aicc', stepwise=FALSE),
mod_exponential = ets(tt, ic='aicc', restrict=FALSE),
mod_neural = nnetar(tt, p=24, size=25),
mod_tbats = tbats(tt, ic='aicc', seasonal.periods=12),
mod_bats = bats(tt, ic='aicc', seasonal.periods=12),
mod_sts = StructTS(tt)
)forecasts <- lapply(models, forecast, 12)
forecasts$naive <- naive(ap_ts, 12)par(mfrow=c(4, 2))
par(mar=c(2, 2, 1.5, 2), xaxs='i', yaxs='i')for(f in forecasts){
plot(f, main="", xaxt="n")
lines(tt, col='red')
}forecasts$mod_arima
forecasts$mod_exponential
forecasts$mod_neural
forecasts$mod_tbats
forecasts$mod_bats
forecasts$mod_sts# best
neural <- c(0.4757844, 0.7905974, 0.4836154, 0.7460323, 0.9739690, 2.5869376, 4.6061767, 6.5247351, 7.1874966, 7.4428412, 8.0734227, 8.6956437)
sum(abs(real$X44 - neural)/(abs(neural) + abs(real$X44))/2) # 5.1536# --------------------------------------------------------------------------------------------------------------------------------------------# X61
tt <- test$X61[7005:8737]
tt <- ts(tt)models <- list (
mod_arima = auto.arima(tt, ic='aicc', stepwise=FALSE),
mod_exponential = ets(tt, ic='aicc', restrict=FALSE),
mod_neural = nnetar(tt, p=24, size=25),
mod_tbats = tbats(tt, ic='aicc', seasonal.periods=12),
mod_bats = bats(tt, ic='aicc', seasonal.periods=12),
mod_sts = StructTS(tt)
)forecasts <- lapply(models, forecast, 12)
forecasts$naive <- naive(ap_ts, 12)par(mfrow=c(4, 2))
par(mar=c(2, 2, 1.5, 2), xaxs='i', yaxs='i')for(f in forecasts){
plot(f, main="", xaxt="n")
lines(tt, col='red')
}forecasts$mod_arima
forecasts$mod_exponential
forecasts$mod_neural
forecasts$mod_tbats
forecasts$mod_bats
forecasts$mod_sts# best
arima <- c(1.1986645, 0.9640165, 0.8746475, 0.9872308, 0.8112574, 1.0184473, 1.0489223, 1.5837020, 1.7516105, 2.3812536, 2.3783198, 2.6844003)
sum(abs(real$X61 - arima)/(abs(arima) + abs(real$X61))/2) # 0.7931neural <- c(0.9696606, 1.0372245, 0.7473804, 0.6070154, 0.8501025, 1.0297103, 1.6316374, 3.6099130, 4.3194516, 5.1244841, 4.7744976, 4.1629140)
sum(abs(real$X61 - neural)/(abs(neural) + abs(real$X61))/2) # 1.6633# --------------------------------------------------------------------------------------------------------------------------------------------# X91
tt <- test$X91[3470:8737]
tt <- ts(tt)models <- list (
mod_arima = auto.arima(tt, ic='aicc', stepwise=FALSE),
mod_neural = nnetar(tt, p=24, size=25),
mod_tbats = tbats(tt, ic='aicc', seasonal.periods=12),
mod_sts = StructTS(tt)
)forecasts <- lapply(models, forecast, 12)
forecasts$naive <- naive(ap_ts, 12)par(mfrow=c(4, 2))
par(mar=c(2, 2, 1.5, 2), xaxs='i', yaxs='i')for(f in forecasts){
plot(f, main="", xaxt="n")
lines(tt, col='red')
}forecasts$mod_arima
forecasts$mod_neural
forecasts$mod_tbats
forecasts$mod_sts# best
sts <- c(0.3330221, 0.3330442, 0.3330662, 0.3330883, 0.3331104, 0.3331325, 0.3331546, 0.3331767, 0.3331987, 0.3332208, 0.3332429, 0.3332650)
sum(abs(real$X91 - sts)/(abs(sts) + abs(real$X91))/2) # 0.153# --------------------------------------------------------------------------------------------------------------------------------------------
0.48/3
# X94
tt <- test$X94[3470:8737]
tt <- ts(tt)models <- list (
mod_arima = auto.arima(tt, ic='aicc', stepwise=FALSE),
mod_neural = nnetar(tt, p=24, size=25),
mod_tbats = tbats(tt, ic='aicc', seasonal.periods=12),
mod_sts = StructTS(tt)
)forecasts <- lapply(models, forecast, 12)
forecasts$naive <- naive(ap_ts, 12)par(mfrow=c(4, 2))
par(mar=c(2, 2, 1.5, 2), xaxs='i', yaxs='i')for(f in forecasts){
plot(f, main="", xaxt="n")
lines(tt, col='red')
}forecasts$mod_arima
forecasts$mod_neural
forecasts$mod_tbats
forecasts$mod_sts# best
sts <- c( 0.002998293, 0.002996585, 0.002994878, 0.002993170, 0.002991463, 0.002989755, 0.002988048, 0.002986340, 0.002984633, 0.002982925, 0.002981218, 0.002979511)
sum(abs(real$X94 - sts)/(abs(sts) + abs(real$X94))/2) # 0.011# --------------------------------------------------------------------------------------------------------------------------------------------# X139
tt <- test$X139[1453:8737]
tt <- ts(tt)models <- list (
mod_arima = auto.arima(tt, ic='aicc', stepwise=FALSE),
mod_neural = nnetar(tt, p=24, size=25),
mod_tbats = tbats(tt, ic='aicc', seasonal.periods=12),
mod_sts = StructTS(tt)
)forecasts <- lapply(models, forecast, 12)
forecasts$naive <- naive(ap_ts, 12)par(mfrow=c(4, 2))
par(mar=c(2, 2, 1.5, 2), xaxs='i', yaxs='i')for(f in forecasts){
plot(f, main="", xaxt="n")
lines(tt, col='red')
}forecasts$mod_arima
forecasts$mod_neural
forecasts$mod_tbats
forecasts$mod_sts# best
sts <- c(0.3955141, 0.3460138, 0.3276476, 0.3245464, 0.3421545, 0.4298059, 0.4276677, 0.3736645, 0.3198994, 0.3315748, 0.3105598, 0.3119527)
sum(abs(real$X139 - sts)/(abs(sts) + abs(real$X139))/2) # 1.117381acc <- lapply(forecasts, function(f){
accuracy(f, real$X139)[2,,drop=FALSE]
})acc <- Reduce(rbind, acc)
row.names(acc) <- names(forecasts)
acc <- acc[order(acc[,'MASE']),]
round(acc, 2)# --------------------------------------------------------------------------------------------------------------------------------------------# X143
tt <- test$X143[1453:8737]
tt <- ts(tt)models <- list (
mod_arima = auto.arima(tt, ic='aicc', stepwise=FALSE),
mod_neural = nnetar(tt, p=24, size=25),
mod_tbats = tbats(tt, ic='aicc', seasonal.periods=12),
mod_sts = StructTS(tt)
)forecasts <- lapply(models, forecast, 12)
forecasts$naive <- naive(ap_ts, 12)par(mfrow=c(4, 2))
par(mar=c(2, 2, 1.5, 2), xaxs='i', yaxs='i')for(f in forecasts){
plot(f, main="", xaxt="n")
lines(tt, col='red')
}a<-forecasts$mod_arima
b<-forecasts$mod_neural
forecasts$mod_tbats
forecasts$mod_sts
a$mean
b$mean# best
arima <- c(0.1949489, 0.2175861, 0.2321352, 0.2417381, 0.2478593, 0.2516283, 0.2538345, 0.2550276, 0.2555838, 0.2557559, 0.2557104, 0.2555535)
sum(abs(real$X143 - arima)/(abs(arima) + abs(real$X143))/2) # 2.053488neural <- c(0.1638490, 0.1588621, 0.1832384, 0.2265225, 0.2154015, 0.1953377, 0.2475955, 0.3023202, 0.3022057, 0.2502210, 0.2503653, 0.3020956)
sum(abs(real$X143 - neural)/(abs(neural) + abs(real$X143))/2) # 1.774925acc <- lapply(forecasts, function(f){
accuracy(f, real$X143)[2,,drop=FALSE]
})acc <- Reduce(rbind, acc)
row.names(acc) <- names(forecasts)
acc <- acc[order(acc[,'MASE']),]
round(acc, 2)
4. Predict
- 실제 경진대회 3등 Model과 비교했을 때, 비슷한 성능을 보임을 알 수 있었다.