Hello,
I would like to build a parsnip model for a simple neural net as implemented in the nnetar() function from the forecast package.
I followed the offictial tutorial How to build a parsnip model as far as I could get. But after trying it out, it gives an error which does not surprise me. The code for building the model you find below. I guess that there are some errors in set_fit() and set_pred() and how the data is fed to the arguments (y and xreg) of the nnetar() function. I would be happy about any hints. This is my first time using tidymodels/parsnip and also building a parsnip model.
library(tidymodels)
set_new_model("narx_neuralnet")
set_model_mode(model = "narx_neuralnet", mode = "regression")
set_model_engine(
"narx_neuralnet",
mode = "regression",
eng = "nnetar"
)
set_dependency("narx_neuralnet", eng = "nnetar", pkg = "forecast")
show_model_info("narx_neuralnet")
set_model_arg(
"narx_neuralnet",
eng = "nnetar",
parsnip = "hidden_units",
original = "size",
func = list(fun = "hidden_units", pkg = "dials"),
has_submodel = FALSE
)
set_model_arg(
"narx_neuralnet",
eng = "nnetar",
parsnip = "epochs",
original = "repeats",
func = list(fun = "epochs", pkg = "dials"),
has_submodel = FALSE
)
set_model_arg(
"narx_neuralnet",
eng = "nnetar",
parsnip = "nonseas_lags",
original = "p",
func = list(fun = "sample_size", pkg = "dials"),
has_submodel = FALSE
)
set_model_arg(
"narx_neuralnet",
eng = "nnetar",
parsnip = "seas_lags",
original = "P",
func = list(fun = "sample_size", pkg = "dials"),
has_submodel = FALSE
)
set_model_arg(
"narx_neuralnet",
eng = "nnetar",
parsnip = "penalty",
original = "decay",
func = list(fun = "penalty", pkg = "dials"),
has_submodel = FALSE
)
narx_neuralnet <-
function(mode = "regression",
hidden_units = NULL,
epochs = NULL,
nonseas_lags = NULL,
seas_lags = NULL,
penalty = NULL) {
# Check for correct mode
if (mode != "regression") {
stop("`mode` should be 'regression'", call. = FALSE)
}
# Capture the arguments in quosures
args <- list(
hidden_units = rlang::enquo(hidden_units),
epochs = rlang::enquo(epochs),
nonseas_lags = rlang::enquo(nonseas_lags),
seas_lags = rlang::enquo(seas_lags),
penalty = rlang::enquo(penalty)
)
# Save some empty slots for future parts of the specification
out <- list(args = args,
eng_args = NULL,
mode = mode,
method = NULL,
engine = NULL)
# set classes in the correct order
class(out) <- make_classes("narx_neuralnet")
out
}
set_fit(
model = "narx_neuralnet",
eng = "nnetar",
mode = "regression",
value = list(
interface = "matrix",
protect = c("y", "xreg"),
func = c(pkg = "forecast", fun = "nnetar"),
defaults = list()
)
)
show_model_info("narx_neuralnet")
set_pred(
model = "narx_neuralnet",
eng = "nnetar",
mode = "regression",
type = "numeric",
value = list(
pre = NULL,
post = function(results, object) {
results$mean %>%
tibble::tibble(.pred = .)
},
func = c(pkg = "forecast", fun = "forecast"),
args =
list(
object = expr(object$fit),
xreg = expr(xreg),
type = "response"
)
)
)
show_model_info("narx_neuralnet")
narx_neuralnet(hidden_units = 5) %>%
translate(engine = "nnetar")