0

I'm writing a function which forecasts some user-inputted data, by fitting an AR model. Outside the function, the code may look like

dat <- c(1,1.1,1,1.2)
print(forecast(ar(dat)))

This runs just fine.

If this is now put inside a function, like:

func <- function(data_input)
{
    temp <- forecast(ar(data_input))
    print(temp)
}
func(dat)

I get this error:

Error in ts(x) : 'ts' object must have one or more observations

Please could someone explain what's going on here?

2 Answers2

0

It works like this:

# library
library(forecast)

# data
dat <- c(1,1.1,1,1.2)

# function definition
func <- function(x){

  (temp <- forecast(ar(x)))

}

# usage
func(dat)

However, I do not know why it does not work in your case.

Miha Trošt
  • 2,002
  • 22
  • 25
0

I used the workaround suggested in Why can't I pass a dataset to a function?

This seems to work, so could the issue be

the definition of environments in the parse tree of S4 methods?

Community
  • 1
  • 1