title: Using Apply family of functions on mts objects
body: <p>Using apply (or sapply) on an mts object removes its time series properties when sending to function. How should I apply same function (with ts input and ts output) on each of times series in an mts object and return it (preferably as mts) [I mean besides using for loops]?</p>
  
  <p>For example suppose I write a function that returns the trend of a time series (using stl)</p>
  
  <pre><code>myfunc &lt;- function(x) {
        return(stl(x,"per")$time.series[,2])
  }
  </code></pre>
  
  <p>Now for a sample mts</p>
  
  <pre><code>z &lt;- ts(matrix(rnorm(90), 30, 3), start=c(1961, 1), frequency=4)
  class(z)
  </code></pre>
  
  <p>Sending only one of the time series works correct:</p>
  
  <pre><code>myfunc(z[,1]) # works correctly, returns the trend of first series
  </code></pre>
  
  <p>My function is not designed for multiple time series so:</p>
  
  <pre><code>myfunc(z) # will not work returning the error below
  
  Error in stl(x, "per") : only univariate series are allowed
  </code></pre>
  
  <p>Using apply on the mts object send each of the time series as a vector, not preserving its time series properties (tsp):</p>
  
  <pre><code>apply(z,2,myfunc) # will not work returning the error below
  
  Error in stl(x, "per") : 
  series is not periodic or has less than two periods
  </code></pre>