Home > database >  Find out function options in r
Find out function options in r

Time:02-05

I am curious about forecast() function in r:

https://www.rdocumentation.org/packages/forecast/versions/8.13/topics/forecast

I want to find out all possible options the "model" argument can take in this function (e.g. ets, stlf, etc.) Is there a way to do this in R?

I try this:

> formals(forecast)
$object


$...

But its not showing function options.

Anyone know about how I can do this?

CodePudding user response:

forecast is an S3 generic function, which means it will call a method depending on the class of the object argument. You can find out which classes are supported by calling methods():

library(forecast)   # attach the package
methods(forecast)   # show the current list of methods
#>  [1] forecast.ar*             forecast.Arima*          forecast.baggedModel*   
#>  [4] forecast.bats*           forecast.default*        forecast.ets            
#>  [7] forecast.forecast*       forecast.forecast_ARIMA* forecast.fracdiff*      
#> [10] forecast.HoltWinters*    forecast.lagwalk*        forecast.lm*            
#> [13] forecast.mlm*            forecast.modelAR*        forecast.mstl*          
#> [16] forecast.mts*            forecast.nnetar*         forecast.stl*           
#> [19] forecast.stlm*           forecast.StructTS*       forecast.tbats*         
#> [22] forecast.ts*             forecast.varest*        
#> see '?methods' for accessing help and source code

Created on 2023-02-04 with reprex v2.0.2

Those methods are not necessarily in the forecast package; any package can define methods for an S3 generic function. This means that your list of known methods may be different than mine, and it may change as you load other packages.

To get help on any of these, you can use the usual help system, e.g. ?forecast.mlm. Sometimes there is no documentation for a particular method if the author thought the documentation for the generic was sufficient.

To see the code for a method, you can just type the name, e.g. forecast.ets, but that won't work for the methods listed with asterisks, e.g. forecast.ar*, because they aren't exported as standalone functions, only as methods for the generic. For those, you can use code like getAnywhere("forecast.ar").

You were asking specifically about the model argument, which doesn't appear in the generic, but does appear in the forecast.ts method. The documentation ?forecast.ts says it is

An object describing a time series model; e.g., one of of class ets, Arima, bats, tbats, or nnetar.

That doesn't sound like a complete list, so you could look at the source code (using getAnywhere("forecast.ts")) to find out what's really going on. Part of the result looks like this:

if (!is.null(model)) {
    if (inherits(model, "forecast")) {
        model <- model$model
    }
    if (inherits(model, "ets")) {
        fit <- ets(object, model = model, ...)
    }
    else if (inherits(model, "Arima")) {
        fit <- Arima(object, model = model, ...)
    }
    else if (inherits(model, "tbats")) {
        fit <- tbats(object, model = model, ...)
    }
    else if (inherits(model, "bats")) {
        fit <- bats(object, model = model, ...)
    }
    else if (inherits(model, "nnetar")) {
        fit <- nnetar(object, model = model, ...)
    }
    else {
        stop("Unknown model class")
    }
    return(forecast(fit, h = h, level = level, fan = fan))
}

so in fact model must inherit from one of the classes listed in the documentation.

  • Related