Fitting ARIMA model to multiple time series and storing forecast into a matrix
<p>As it is large I can't <code>dput</code> it here. But suppose the <code>realmatrix</code> is a &quot;mts&quot; with non-trivial values</p>
<pre><code>realmatrix &lt;- matrix(NA, ncol = 100, nrow = 138)
</code></pre>
<p>In fact it stores 100 time series with length (rows) = 138 (from Jan 2005 to June 2016).</p>
<p>I want to store the <code>Arima</code> forecasts (12 months ahead: that is, from July 2016 to June 2017) in another matrix <code>farimamatrix</code> (which should have 12 rows and 100 columns), via the following loop:</p>
<pre><code>farimamatrix &lt;- matrix(NA, nrow = 12, ncol = 100)

m &lt;- k &lt;- list()

for (i in 1:100) {
  try(m[[i]] &lt;- Arima(realmatrix[,i], order = c(0,1,0), seasonal = c(1,0,1)))
  k[[i]] &lt;- forecast.Arima(m[[i]], h=12)
  farimamatrix[,i] &lt;- fitted(k[[i]])
  }
</code></pre>
<p>But I am getting the following message:</p>
<blockquote>
<p>Error in farimamatrix[, i] &lt;- fitted(k[[i]]) :</p>
<p>incorrect number of subscripts on matrix</p>
</blockquote>
<p>What's wrong? Thanks in advance.</p>
<hr />
<p><strong>Edited (24/10): updated / corrected under Zheyuan's answer and previous problem gone</strong></p>
<p>Original data:</p>
<pre><code>tsdata &lt;- 
structure(c(28220L, 27699L, 28445L, 29207L, 28482L, 28326L, 28322L, 
28611L, 29187L, 29145L, 29288L, 29352L, 28881L, 29383L, 29898L, 
29888L, 28925L, 29069L, 29114L, 29886L, 29917L, 30144L, 30531L, 
30494L, 30700L, 30325L, 31313L, 32031L, 31383L, 30767L, 30500L, 
31181L, 31736L, 32136L, 32654L, 32305L, 31856L, 31731L, 32119L, 
31953L, 32300L, 31743L, 32150L, 33014L, 32964L, 33674L, 33410L, 
31559L, 30667L, 30495L, 31978L, 32043L, 30945L, 30715L, 31325L, 
32262L, 32717L, 33420L, 33617L, 34123L, 33362L, 33731L, 35118L, 
35027L, 34298L, 34171L, 33851L, 34715L, 35184L, 35190L, 35079L, 
35958L, 35875L, 35446L, 36352L, 36050L, 35567L, 35161L, 35419L, 
36337L, 36967L, 36745L, 36370L, 36744L, 36303L, 36899L, 38621L, 
37994L, 36809L, 36527L, 35916L, 37178L, 37661L, 37794L, 38642L, 
37763L, 38367L, 38006L, 38442L, 38654L, 38345L, 37628L, 37698L, 
38613L, 38525L, 39389L, 39920L, 39556L, 40280L, 41653L, 40269L, 
39592L, 39100L, 37726L, 37867L, 38551L, 38895L, 40100L, 40950L, 
39838L, 40643L, 40611L, 39611L, 39445L, 38059L, 37131L, 36697L, 
37746L, 37733L, 39188L, 39127L, 38554L, 38219L, 38497L, 39165L, 
40077L, 38370L, 37174L), .Dim = c(138L, 1L), .Dimnames = list(
    NULL, &quot;Data&quot;), .Tsp = c(2005, 2016.41666666667, 12), class = &quot;ts&quot;)
</code></pre>
<p>Code</p>
<pre><code>library(&quot;forecast&quot;)

z &lt;- stl(tsdata[, &quot;Data&quot;], s.window=&quot;periodic&quot;)

t &lt;- z$time.series[,&quot;trend&quot;]
s &lt;- z$time.series[,&quot;seasonal&quot;]
e &lt;- z$time.series[,&quot;remainder&quot;]

# error matrix
ematrix &lt;- matrix(rnorm(138 * 100, sd = 100), nrow = 138)

# generating a ts class error matrix
ematrixts &lt;- ts(ematrix, start=c(2005,1), freq=12)

# combining the trend + season + error matrix into a real matrix
realmatrix &lt;- t + s + ematrixts

# creating a (forecast) arima matrix
farimamatrix &lt;- matrix(NA, ncol = 100, nrow = 12)

m &lt;- k &lt;- vector(&quot;list&quot;, length = 100)

for (i in 1:100) {
  try(m[[i]] &lt;- Arima(realmatrix[,i], order = c(0,1,0), seasonal = c(1,0,1)))
  print(i)
  k[[i]] &lt;- forecast.Arima(m[[i]], h = 12)
  farimamatrix[,i] &lt;- k[[i]]$mean
  }

# ts.plot(farimamatrix[,1:100],col = c(rep(&quot;gray&quot;,100),rep(&quot;red&quot;,1)))
</code></pre>
<p>The loop seems to work, but breaks down after a few iterations due to failure of <code>Arima</code>:</p>
<blockquote>
<p>Error in stats::arima(x = x, order = order, seasonal = seasonal, include.mean = include.mean, : &quot; non-stationary seasonal AR part from CSS</p>
</blockquote>