I'm trying to join two function results in one and trying to obtain results in one column.
My code
myfun <-function(x){
fit <-Arima(x, order = c(1,1,1), seasonal = list(order = c(0,1,0), period = 52),include.mean=TRUE,
include.constant = FALSE, method = 'CSS')
fit_a <- forecast(fit$fitted)
fit_a <- data.frame(fit_a$fitted)
colnames(fit_a)[1] <- "load"
fit_a$load <- as.data.frame(fit_a$load)
fit_b <- data.frame(forecast(fit,h=400))
fit_b <- data.frame(fit_b$Point.Forecast)
colnames(fit_b)[1] <- "load"
fit_b$load <- as.data.frame(fit_b$load)
return(rbind(fit_a,fit_b))
}
I'm getting values individually like return(fit_a)
and return(fit_b)
but while doing rbind() I can't because of individual time-series data.
Tried : c(fit_a,fit_b)
showing two different ts( which confirms we are having output and just failing over rbind()
).
Can someone help me, how to extract both fitted and forecasted values in same function.
Thanks in advance!
One way to return more than one object is to create a list of the objects, then return that. In your case you could use this at the end of your function:
fit <- list(fit_a, fit_b)
return(fit)
Then you can access the elements using fit[[1]]
or fit[[2]]
.
You also have the option of naming the elements so you can access them using the $
, like so:
fit <- list(fit_a = fit_a, fit_b = fit_b)
return(fit)
Then you can use fit$fit_a
and fit$fit_b