Strickland
Strickland

Reputation: 600

Can I prevent sarima.for from plotting?

I want to forecast a time-series with the function sarima.for from the astsa library. Yet, I would like to prevent the plot and just keep the data.

        require("astsa")
        set.seed(1)
        x<-ts(rnorm(31),start=1980,end=2010)
        sarima.for(x, n.ahead = 10, p=0, d=1, q=0)

I could not find an optionality here. Is there any? Or is there a way to define a proper wraper for the function?

enter image description here

Upvotes: 2

Views: 1443

Answers (2)

Jilber Urbina
Jilber Urbina

Reputation: 61204

you can also use the arima and predict functions from stats package to get the same results without plot.

You can read from the helpfile that sarima.for - is a wrapper for R's predict.Arima.

> n <- length(x)
> fit <- stats::arima(x, order = c(p=0, d=1, q=0), xreg=1:n)
> stats::predict(fit, n.ahead=10, newxreg = (n + 1):(n + 10))
$`pred`
Time Series:
Start = 2011 
End = 2020 
Frequency = 1 
 [1] 1.424851 1.491022 1.557193 1.623364 1.689535 1.755706 1.821877 1.888048 1.954220 2.020391

$se
Time Series:
Start = 2011 
End = 2020 
Frequency = 1 
 [1] 1.306001 1.846964 2.262060 2.612002 2.920307 3.199036 3.455353 3.693928 3.918002 4.129937

Upvotes: 1

dww
dww

Reputation: 31452

You can send the output to a temporary file, then delete the file:

sarima.noplot = function(x, ...) {
  png(tf<-tempfile())
  out <- sarima.for(x, ...)
  dev.off()
  file.remove(tf)
  return(out)
}

sarima.noplot(x, n.ahead = 10, p=0, d=1, q=0)
# $pred
# Time Series:
#   Start = 2011 
# End = 2020 
# Frequency = 1 
# [1] 1.424851 1.491022 1.557193 1.623364 1.689535 1.755706 1.821877
# [8] 1.888048 1.954220 2.020391

Upvotes: 1

Related Questions