# How to make a diurnal curve using splines
# Select first 54 hours from the load data
D <- subset(Dbuilding, 1:76, kseq=1:4)
# Make the hour of the day as a forecast input
D$tday <- make_tday(D$t, kseq=1:4)
D$tday
# Calculate the base splines for each column in tday
L <- bspline(D$tday)
# Now L holds a data.frame for each base spline
str(L)
# Hence this will result in four inputs for the regression model
# Plot (note that the splines period starts at tday=0)
plot(D$t, L$bs1$k1, type="s")
for(i in 2:length(L)){
lines(D$t, L[[i]]$k1, col=i, type="s")
}
# In a model formulation it will be:
model <- forecastmodel$new()
model$add_inputs(mutday = "bspline(tday)")
# We set the horizons (actually not needed for the transform, only required for data checks)
model$kseq <- 1:4
# Such that at the transform stage will give the same as above
model$transform_data(D)
# Periodic splines are useful for modelling a diurnal harmonical functions
L <- bspline(D$tday, bknots=c(0,24), df=4, periodic=TRUE)
# or
L <- pbspline(D$tday, bknots=c(0,24), df=4)
# Note, how it has to have high enough df, else it generates an error
# Plot
plot(D$t, L$bs1$k1, type="s")
for(i in 2:length(L)){
lines(D$t, L[[i]]$k1, col=i, type="s")
}
Run the code above in your browser using DataLab