R/arima.h.R

Defines functions arima

Documented in arima

# This file is automatically generated, you probably don't want to edit this

arimaOptions <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "arimaOptions",
    inherit = jmvcore::Options,
    public = list(
        initialize = function(
            mode = "simple",
            dep = NULL,
            dep1 = NULL,
            time1 = NULL,
            freq = 12,
            pred = 10,
            plot = FALSE,
            box = FALSE,
            plot1 = FALSE,
            plot2 = FALSE,
            plot3 = FALSE,
            coef = FALSE,
            fit = FALSE,
            point = FALSE,
            plot4 = FALSE,
            plot5 = FALSE,
            plot6 = FALSE,
            periods = 365,
            unit = "day",
            width1 = 500,
            height1 = 500,
            width2 = 500,
            height2 = 500,
            width3 = 500,
            height3 = 500,
            width4 = 500,
            height4 = 500,
            width5 = 500,
            height5 = 500,
            width6 = 500,
            height6 = 500,
            width7 = 500,
            height7 = 500,
            width8 = 500,
            height8 = 500, ...) {

            super$initialize(
                package="snowCluster",
                name="arima",
                requiresData=TRUE,
                ...)

            private$..mode <- jmvcore::OptionList$new(
                "mode",
                mode,
                options=list(
                    "simple",
                    "complex"),
                default="simple")
            private$..dep <- jmvcore::OptionVariable$new(
                "dep",
                dep,
                suggested=list(
                    "continuous"),
                permitted=list(
                    "numeric"))
            private$..dep1 <- jmvcore::OptionVariable$new(
                "dep1",
                dep1,
                suggested=list(
                    "continuous"),
                permitted=list(
                    "numeric"))
            private$..time1 <- jmvcore::OptionVariables$new(
                "time1",
                time1,
                rejectUnusedLevels=TRUE,
                suggested=list(
                    "nominal"),
                default=NULL)
            private$..freq <- jmvcore::OptionInteger$new(
                "freq",
                freq,
                min=1,
                default=12)
            private$..pred <- jmvcore::OptionInteger$new(
                "pred",
                pred,
                min=1,
                default=10)
            private$..plot <- jmvcore::OptionBool$new(
                "plot",
                plot,
                default=FALSE)
            private$..box <- jmvcore::OptionBool$new(
                "box",
                box,
                default=FALSE)
            private$..plot1 <- jmvcore::OptionBool$new(
                "plot1",
                plot1,
                default=FALSE)
            private$..plot2 <- jmvcore::OptionBool$new(
                "plot2",
                plot2,
                default=FALSE)
            private$..plot3 <- jmvcore::OptionBool$new(
                "plot3",
                plot3,
                default=FALSE)
            private$..coef <- jmvcore::OptionBool$new(
                "coef",
                coef,
                default=FALSE)
            private$..fit <- jmvcore::OptionBool$new(
                "fit",
                fit,
                default=FALSE)
            private$..point <- jmvcore::OptionBool$new(
                "point",
                point,
                default=FALSE)
            private$..plot4 <- jmvcore::OptionBool$new(
                "plot4",
                plot4,
                default=FALSE)
            private$..plot5 <- jmvcore::OptionBool$new(
                "plot5",
                plot5,
                default=FALSE)
            private$..plot6 <- jmvcore::OptionBool$new(
                "plot6",
                plot6,
                default=FALSE)
            private$..periods <- jmvcore::OptionInteger$new(
                "periods",
                periods,
                min=1,
                default=365)
            private$..unit <- jmvcore::OptionList$new(
                "unit",
                unit,
                options=list(
                    "hour",
                    "day",
                    "week",
                    "month",
                    "quarter",
                    "year"),
                default="day")
            private$..width1 <- jmvcore::OptionInteger$new(
                "width1",
                width1,
                default=500)
            private$..height1 <- jmvcore::OptionInteger$new(
                "height1",
                height1,
                default=500)
            private$..width2 <- jmvcore::OptionInteger$new(
                "width2",
                width2,
                default=500)
            private$..height2 <- jmvcore::OptionInteger$new(
                "height2",
                height2,
                default=500)
            private$..width3 <- jmvcore::OptionInteger$new(
                "width3",
                width3,
                default=500)
            private$..height3 <- jmvcore::OptionInteger$new(
                "height3",
                height3,
                default=500)
            private$..width4 <- jmvcore::OptionInteger$new(
                "width4",
                width4,
                default=500)
            private$..height4 <- jmvcore::OptionInteger$new(
                "height4",
                height4,
                default=500)
            private$..width5 <- jmvcore::OptionInteger$new(
                "width5",
                width5,
                default=500)
            private$..height5 <- jmvcore::OptionInteger$new(
                "height5",
                height5,
                default=500)
            private$..width6 <- jmvcore::OptionInteger$new(
                "width6",
                width6,
                default=500)
            private$..height6 <- jmvcore::OptionInteger$new(
                "height6",
                height6,
                default=500)
            private$..width7 <- jmvcore::OptionInteger$new(
                "width7",
                width7,
                default=500)
            private$..height7 <- jmvcore::OptionInteger$new(
                "height7",
                height7,
                default=500)
            private$..width8 <- jmvcore::OptionInteger$new(
                "width8",
                width8,
                default=500)
            private$..height8 <- jmvcore::OptionInteger$new(
                "height8",
                height8,
                default=500)

            self$.addOption(private$..mode)
            self$.addOption(private$..dep)
            self$.addOption(private$..dep1)
            self$.addOption(private$..time1)
            self$.addOption(private$..freq)
            self$.addOption(private$..pred)
            self$.addOption(private$..plot)
            self$.addOption(private$..box)
            self$.addOption(private$..plot1)
            self$.addOption(private$..plot2)
            self$.addOption(private$..plot3)
            self$.addOption(private$..coef)
            self$.addOption(private$..fit)
            self$.addOption(private$..point)
            self$.addOption(private$..plot4)
            self$.addOption(private$..plot5)
            self$.addOption(private$..plot6)
            self$.addOption(private$..periods)
            self$.addOption(private$..unit)
            self$.addOption(private$..width1)
            self$.addOption(private$..height1)
            self$.addOption(private$..width2)
            self$.addOption(private$..height2)
            self$.addOption(private$..width3)
            self$.addOption(private$..height3)
            self$.addOption(private$..width4)
            self$.addOption(private$..height4)
            self$.addOption(private$..width5)
            self$.addOption(private$..height5)
            self$.addOption(private$..width6)
            self$.addOption(private$..height6)
            self$.addOption(private$..width7)
            self$.addOption(private$..height7)
            self$.addOption(private$..width8)
            self$.addOption(private$..height8)
        }),
    active = list(
        mode = function() private$..mode$value,
        dep = function() private$..dep$value,
        dep1 = function() private$..dep1$value,
        time1 = function() private$..time1$value,
        freq = function() private$..freq$value,
        pred = function() private$..pred$value,
        plot = function() private$..plot$value,
        box = function() private$..box$value,
        plot1 = function() private$..plot1$value,
        plot2 = function() private$..plot2$value,
        plot3 = function() private$..plot3$value,
        coef = function() private$..coef$value,
        fit = function() private$..fit$value,
        point = function() private$..point$value,
        plot4 = function() private$..plot4$value,
        plot5 = function() private$..plot5$value,
        plot6 = function() private$..plot6$value,
        periods = function() private$..periods$value,
        unit = function() private$..unit$value,
        width1 = function() private$..width1$value,
        height1 = function() private$..height1$value,
        width2 = function() private$..width2$value,
        height2 = function() private$..height2$value,
        width3 = function() private$..width3$value,
        height3 = function() private$..height3$value,
        width4 = function() private$..width4$value,
        height4 = function() private$..height4$value,
        width5 = function() private$..width5$value,
        height5 = function() private$..height5$value,
        width6 = function() private$..width6$value,
        height6 = function() private$..height6$value,
        width7 = function() private$..width7$value,
        height7 = function() private$..height7$value,
        width8 = function() private$..width8$value,
        height8 = function() private$..height8$value),
    private = list(
        ..mode = NA,
        ..dep = NA,
        ..dep1 = NA,
        ..time1 = NA,
        ..freq = NA,
        ..pred = NA,
        ..plot = NA,
        ..box = NA,
        ..plot1 = NA,
        ..plot2 = NA,
        ..plot3 = NA,
        ..coef = NA,
        ..fit = NA,
        ..point = NA,
        ..plot4 = NA,
        ..plot5 = NA,
        ..plot6 = NA,
        ..periods = NA,
        ..unit = NA,
        ..width1 = NA,
        ..height1 = NA,
        ..width2 = NA,
        ..height2 = NA,
        ..width3 = NA,
        ..height3 = NA,
        ..width4 = NA,
        ..height4 = NA,
        ..width5 = NA,
        ..height5 = NA,
        ..width6 = NA,
        ..height6 = NA,
        ..width7 = NA,
        ..height7 = NA,
        ..width8 = NA,
        ..height8 = NA)
)

arimaResults <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "arimaResults",
    inherit = jmvcore::Group,
    active = list(
        instructions = function() private$.items[["instructions"]],
        text = function() private$.items[["text"]],
        coef = function() private$.items[["coef"]],
        fit = function() private$.items[["fit"]],
        point = function() private$.items[["point"]],
        plot = function() private$.items[["plot"]],
        box = function() private$.items[["box"]],
        plot1 = function() private$.items[["plot1"]],
        plot2 = function() private$.items[["plot2"]],
        plot3 = function() private$.items[["plot3"]],
        plot4 = function() private$.items[["plot4"]],
        plot5 = function() private$.items[["plot5"]],
        plot6 = function() private$.items[["plot6"]]),
    private = list(),
    public=list(
        initialize=function(options) {
            super$initialize(
                options=options,
                name="",
                title="Univariate time series",
                refs="snowCluster")
            self$add(jmvcore::Html$new(
                options=options,
                name="instructions",
                title="Instructions",
                visible=TRUE))
            self$add(jmvcore::Preformatted$new(
                options=options,
                name="text",
                title=""))
            self$add(jmvcore::Table$new(
                options=options,
                name="coef",
                title="Model coefficients",
                visible="(coef)",
                clearWith=list(
                    "dep",
                    "freq",
                    "pred"),
                columns=list(
                    list(
                        `name`="name", 
                        `title`="", 
                        `type`="text", 
                        `content`="($key)"),
                    list(
                        `name`="co", 
                        `title`="Estimate", 
                        `type`="number"),
                    list(
                        `name`="se", 
                        `title`="SE", 
                        `type`="number"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="fit",
                title="Model fit",
                visible="(fit)",
                clearWith=list(
                    "dep",
                    "freq",
                    "pred"),
                columns=list(
                    list(
                        `name`="name", 
                        `title`="Fit Measures", 
                        `type`="text", 
                        `content`="($key)"),
                    list(
                        `name`="value", 
                        `title`="Value"))))
            self$add(jmvcore::Table$new(
                options=options,
                name="point",
                title="Prediction intervals",
                visible="(point)",
                refs="forecast",
                clearWith=list(
                    "dep",
                    "freq",
                    "pred"),
                columns=list(
                    list(
                        `name`="name", 
                        `title`="", 
                        `type`="text", 
                        `content`="($key)"),
                    list(
                        `name`="po", 
                        `title`="Point forecast", 
                        `type`="number"),
                    list(
                        `name`="lower", 
                        `title`="Lower", 
                        `type`="number", 
                        `superTitle`="95% CI"),
                    list(
                        `name`="upper", 
                        `title`="Upper", 
                        `type`="number", 
                        `superTitle`="95% CI"))))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot",
                title="Decomposition",
                visible="(plot)",
                renderFun=".plot",
                clearWith=list(
                    "dep",
                    "freq",
                    "pred",
                    "width7",
                    "height7")))
            self$add(jmvcore::Image$new(
                options=options,
                name="box",
                title="Box plot",
                requiresData=TRUE,
                visible="(box)",
                renderFun=".box",
                clearWith=list(
                    "dep",
                    "freq",
                    "pred",
                    "width8",
                    "height8")))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot1",
                title="Automatic ARIMA forecasts",
                visible="(plot1)",
                width=500,
                height=500,
                renderFun=".plot1",
                clearWith=list(
                    "dep",
                    "freq",
                    "pred",
                    "width1",
                    "height1")))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot2",
                title="Residual plot",
                visible="(plot2)",
                renderFun=".plot2",
                clearWith=list(
                    "dep",
                    "freq",
                    "pred",
                    "width2",
                    "height2")))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot3",
                title="Prediction",
                visible="(plot3)",
                refs="forecast",
                renderFun=".plot3",
                clearWith=list(
                    "dep",
                    "freq",
                    "pred",
                    "width3",
                    "height3")))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot4",
                title="Forcast for Prophet",
                visible="(plot4)",
                refs="prophet",
                renderFun=".plot4",
                clearWith=list(
                    "dep1",
                    "time1",
                    "period",
                    "unit",
                    "width4",
                    "height4")))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot5",
                title="Model components for Prophet",
                visible="(plot5)",
                refs="prophet",
                renderFun=".plot5",
                clearWith=list(
                    "dep1",
                    "time1",
                    "period",
                    "unit",
                    "width5",
                    "height5")))
            self$add(jmvcore::Image$new(
                options=options,
                name="plot6",
                title="Actual vs. Forecasted Values for Variable y for Prophet",
                visible="(plot6)",
                refs="prophet",
                renderFun=".plot6",
                clearWith=list(
                    "dep1",
                    "time1",
                    "period",
                    "unit",
                    "width6",
                    "height6")))}))

arimaBase <- if (requireNamespace("jmvcore", quietly=TRUE)) R6::R6Class(
    "arimaBase",
    inherit = jmvcore::Analysis,
    public = list(
        initialize = function(options, data=NULL, datasetId="", analysisId="", revision=0) {
            super$initialize(
                package = "snowCluster",
                name = "arima",
                version = c(1,0,0),
                options = options,
                results = arimaResults$new(options=options),
                data = data,
                datasetId = datasetId,
                analysisId = analysisId,
                revision = revision,
                pause = NULL,
                completeWhenFilled = FALSE,
                requiresMissings = FALSE,
                weightsSupport = 'auto')
        }))

#' Univariate time series
#'
#' 
#' @param data timeseries data format
#' @param mode .
#' @param dep the dependent variable from \code{data}, variable must be
#'   numeric
#' @param dep1 the dependent variable from \code{data}, variable must be
#'   numeric
#' @param time1 ts type called 'ds'
#' @param freq .
#' @param pred .
#' @param plot .
#' @param box .
#' @param plot1 .
#' @param plot2 .
#' @param plot3 .
#' @param coef .
#' @param fit .
#' @param point .
#' @param plot4 .
#' @param plot5 .
#' @param plot6 .
#' @param periods .
#' @param unit .
#' @param width1 .
#' @param height1 .
#' @param width2 .
#' @param height2 .
#' @param width3 .
#' @param height3 .
#' @param width4 .
#' @param height4 .
#' @param width5 .
#' @param height5 .
#' @param width6 .
#' @param height6 .
#' @param width7 .
#' @param height7 .
#' @param width8 .
#' @param height8 .
#' @return A results object containing:
#' \tabular{llllll}{
#'   \code{results$instructions} \tab \tab \tab \tab \tab a html \cr
#'   \code{results$text} \tab \tab \tab \tab \tab a preformatted \cr
#'   \code{results$coef} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$fit} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$point} \tab \tab \tab \tab \tab a table \cr
#'   \code{results$plot} \tab \tab \tab \tab \tab an image \cr
#'   \code{results$box} \tab \tab \tab \tab \tab an image \cr
#'   \code{results$plot1} \tab \tab \tab \tab \tab an image \cr
#'   \code{results$plot2} \tab \tab \tab \tab \tab an image \cr
#'   \code{results$plot3} \tab \tab \tab \tab \tab an image \cr
#'   \code{results$plot4} \tab \tab \tab \tab \tab an image \cr
#'   \code{results$plot5} \tab \tab \tab \tab \tab an image \cr
#'   \code{results$plot6} \tab \tab \tab \tab \tab an image \cr
#' }
#'
#' Tables can be converted to data frames with \code{asDF} or \code{\link{as.data.frame}}. For example:
#'
#' \code{results$coef$asDF}
#'
#' \code{as.data.frame(results$coef)}
#'
#' @export
arima <- function(
    data,
    mode = "simple",
    dep,
    dep1,
    time1 = NULL,
    freq = 12,
    pred = 10,
    plot = FALSE,
    box = FALSE,
    plot1 = FALSE,
    plot2 = FALSE,
    plot3 = FALSE,
    coef = FALSE,
    fit = FALSE,
    point = FALSE,
    plot4 = FALSE,
    plot5 = FALSE,
    plot6 = FALSE,
    periods = 365,
    unit = "day",
    width1 = 500,
    height1 = 500,
    width2 = 500,
    height2 = 500,
    width3 = 500,
    height3 = 500,
    width4 = 500,
    height4 = 500,
    width5 = 500,
    height5 = 500,
    width6 = 500,
    height6 = 500,
    width7 = 500,
    height7 = 500,
    width8 = 500,
    height8 = 500) {

    if ( ! requireNamespace("jmvcore", quietly=TRUE))
        stop("arima requires jmvcore to be installed (restart may be required)")

    if ( ! missing(dep)) dep <- jmvcore::resolveQuo(jmvcore::enquo(dep))
    if ( ! missing(dep1)) dep1 <- jmvcore::resolveQuo(jmvcore::enquo(dep1))
    if ( ! missing(time1)) time1 <- jmvcore::resolveQuo(jmvcore::enquo(time1))
    if (missing(data))
        data <- jmvcore::marshalData(
            parent.frame(),
            `if`( ! missing(dep), dep, NULL),
            `if`( ! missing(dep1), dep1, NULL),
            `if`( ! missing(time1), time1, NULL))


    options <- arimaOptions$new(
        mode = mode,
        dep = dep,
        dep1 = dep1,
        time1 = time1,
        freq = freq,
        pred = pred,
        plot = plot,
        box = box,
        plot1 = plot1,
        plot2 = plot2,
        plot3 = plot3,
        coef = coef,
        fit = fit,
        point = point,
        plot4 = plot4,
        plot5 = plot5,
        plot6 = plot6,
        periods = periods,
        unit = unit,
        width1 = width1,
        height1 = height1,
        width2 = width2,
        height2 = height2,
        width3 = width3,
        height3 = height3,
        width4 = width4,
        height4 = height4,
        width5 = width5,
        height5 = height5,
        width6 = width6,
        height6 = height6,
        width7 = width7,
        height7 = height7,
        width8 = width8,
        height8 = height8)

    analysis <- arimaClass$new(
        options = options,
        data = data)

    analysis$run()

    analysis$results
}
hyunsooseol/snowCluster documentation built on April 5, 2025, 2:06 a.m.