Using apply (or sapply) in an mts object removes its time series properties when sent to the function. How should I apply the same function (with the input signal ts and ts output) in each of the time series in the mts object and return it (preferably in the form of mts) [I mean, except for use for loops]?
For example, suppose I write a function that returns the trend of a time series (using stl)
myfunc <- function(x) { return(stl(x,"per")$time.series[,2]) }
Now for sample mts
z <- ts(matrix(rnorm(90), 30, 3), start=c(1961, 1), frequency=4) class(z)
Sending only one of the time series works correctly:
myfunc(z[,1])
My function is not intended for multiple time series, therefore:
myfunc(z)
Using apply on an mts object, send each of the time series to the vector without saving its time series (tsp):
apply(z,2,myfunc)
Majid einian
source share