bs: Compute base splines of a variable using the R function...

bsplineR Documentation

Compute base splines of a variable using the R function splines::bs, use in the transform stage.

Description

Simply wraps the splines::bs, such that it can be used in the transformation stage.

Usage

bspline(
  X,
  Boundary.knots = NA,
  intercept = FALSE,
  df = NULL,
  knots = NULL,
  degree = 3,
  bknots = NA,
  periodic = FALSE
)

Arguments

X

data.frame (as part of data.list) with horizons as columns named kxx (i.e. one for each horizon)

Boundary.knots

The value is NA: then the boundaries are set to the range of each horizons (columns in X). See ?splines::bs

intercept

See ?splines::bs.

df

See ?splines::bs

knots

See ?splines::bs

degree

See ?splines::bs

bknots

Is just a short for Boundary.knots and replace Boundary.knots (if Boundary.knots is not given)

periodic

Default FALSE. If TRUE, then pbs::pbs is called and periodic splines are generated.

Details

See the help for all arguments with ?splines::bs. NOTE that two arguments have different default values.

See the example https://onlineforecasting.org/examples/solar-power-forecasting.html where the function is used in a model.

Value

List of data frames with the computed base splines, each with columns for the same horizons as in X

See Also

Other Transform stage functions: pbspline()

Examples


# 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")
}


onlineforecast documentation built on Oct. 12, 2023, 5:15 p.m.